I am creating a drupal module and here in the module file i created a check on hook_toolbar() function that if there are credentials in the database it will show 4 links in dropdown otherwise it will show only 2 links. Initially with no credentials it shows 2 links but whenever i get credentials and update them in the database i have to clear cache in order to update the tray in the hook_toolbar() in order to show 4 links.
function try_toolbar() {
$items = [];
\Drupal::service('page_cache_kill_switch')->trigger();
$items['TRY'] = [
'#cache' => [
'contexts' => ['user.permissions'],
],
];
if (!\Drupal::currentUser()->hasPermission('Access the Commande overview page')) {
return $items;
}
$credentials = db_select('config', 'n')
->fields('n')
->condition('collection', 'TRY','=')
->execute()
->fetchAssoc();
$opValue = $credentials['data'];
if($credentials) {
$items['TRY'] += [
'#type' => 'toolbar_item',
'#weight' => 1,
'tab' => [
'#type' => 'link',
'#title' => 'TRY',
'#url' => \Drupal\Core\Url::fromUri("internal:/admin/try"),
'#attributes' => [
'title' => 'try menu',
'class' => ['toolbar-icon', 'toolbar-icon-try'],
],
],
'tray' => [
'configuration' => [
'#type' => 'link',
'#title' => 'Dashboard',
'#url' => \Drupal\Core\Url::fromUri("internal:/admin/try_dashboard"),
],[
'#type' => 'link',
'#title' => 'Notifications',
'#url' => \Drupal\Core\Url::fromUri("internal:/admin/try_notifications"),
],[
'#type' => 'link',
'#title' => 'Settings',
'#url' => \Drupal\Core\Url::fromUri("internal:/admin/try_settings"),
],[
'#type' => 'link',
'#title' => 'Help',
'#url' => \Drupal\Core\Url::fromUri("internal:/admin/try_help"),
],
],
'#attached' => array(
'library' => array(
'try/admin',
),
),
];
return $items;
} else {
$items['TRY'] += [
'#type' => 'toolbar_item',
'#weight' => 1,
'tab' => [
'#type' => 'link',
'#title' => 'TRY',
'#url' => \Drupal\Core\Url::fromUri("internal:/admin/try"),
'#attributes' => [
'title' => 'try menu',
'class' => ['toolbar-icon', 'toolbar-icon-try'],
],
],
'tray' => [
'configuration' => [
'#type' => 'link',
'#title' => 'On Board',
'#url' => \Drupal\Core\Url::fromUri("internal:/admin/onBoard"),
],[
'#type' => 'link',
'#title' => 'Verify Credentials',
'#url' => \Drupal\Core\Url::fromUri("internal:/admin/try_settings/verifyCredentials"),
]
],
'#attached' => array(
'library' => array(
'try/admin',
),
),
];
return $items;
}
}
Here i used the following code to disable the cache
\Drupal::service('page_cache_kill_switch')->trigger();
But it is not working thus forcing me to manually clear the cache.
Please tell me a way to bust the cache depending on that database result.