Problem/Motivation
In hook_menu_local_tasks_alter() there is an example of adding a local task, depending on the user's access:
function hook_menu_local_tasks_alter(&$data, $route_name, \Drupal\Core\Cache\RefinableCacheableDependencyInterface &$cacheability) {
// Add a tab linking to node/add to all pages.
$data['tabs'][0]['node.add_page'] = [
'#theme' => 'menu_local_task',
'#link' => [
'title' => t('Example tab'),
'url' => Url::fromRoute('node.add_page'),
'localized_options' => [
'attributes' => [
'title' => t('Add content'),
],
],
],
];
// The tab we're adding is dependent on a user's access to add content.
$cacheability->addCacheTags(['user.permissions']);
}
Link to GitLab: https://git.drupalcode.org/project/drupal/-/blob/9.1.x/core/lib/Drupal/C...
It looks like there is a typo because the cache context is added as a cache tag.
Proposed resolution
Replace $cacheability->addCacheTags(['user.permissions']); with $cacheability->addCacheContexts(['user.permissions']);
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
matroskeenComment #3
andypostNice catch
Comment #4
jungleRTBC+1, yes,
user.permissionsis a cache context, see https://www.drupal.org/docs/drupal-apis/cache-api/cache-contexts#core-co...Comment #6
jungleRe-queued, A random testing failure.
Comment #10
catchCommitted/pushed to 9.1.x and cherry-picked back to 8.9.x, thanks!