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

CommentFileSizeAuthor
#2 3144046-2.patch533 bytesmatroskeen

Comments

Matroskeen created an issue. See original summary.

matroskeen’s picture

Assigned: matroskeen » Unassigned
Status: Active » Needs review
StatusFileSize
new533 bytes
andypost’s picture

Status: Needs review » Reviewed & tested by the community

Nice catch

jungle’s picture

RTBC+1, yes, user.permissions is a cache context, see https://www.drupal.org/docs/drupal-apis/cache-api/cache-contexts#core-co...

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 2: 3144046-2.patch, failed testing. View results

jungle’s picture

Status: Needs work » Reviewed & tested by the community

Re-queued, A random testing failure.

  • catch committed 78cc0f2 on 9.1.x
    Issue #3144046 by Matroskeen: Incorrect example of adding Cache metadata...

  • catch committed 35d3a84 on 9.0.x
    Issue #3144046 by Matroskeen: Incorrect example of adding Cache metadata...

  • catch committed c7a563c on 8.9.x
    Issue #3144046 by Matroskeen: Incorrect example of adding Cache metadata...
catch’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.1.x and cherry-picked back to 8.9.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.