Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 292776

Incorrect example of adding Cache metadata in hook_menu_local_tasks_alter()

$
0
0

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


Viewing all articles
Browse latest Browse all 292776

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>