0

I have a somewhat complicated view that uses multiple displays for showing events. It uses a datepager (https://www.drupal.org/project/date_pager), exposed filters (using better exposed filters), and a local task menu.

The overall issue I'm having is that I want any event view link, be it a local task tab, a pagination link, or activating an exposed filter, to preserve any arguments in the URL. And because of a currently known bug for BEF, I can't even use the remember filters option.

So I've decided my best route is to go into the twig templates for these various pagers and menus, get the current arguments from the URL, and append or merge them as needed on the render links.

So far this seems to be working, but I'm running into an issue getting it to work well with the local task menu tabs.

I have a preprocess function in my theme file (using template_preprocess_menu_local_task) to get the arguments from the URL but the issue is that its caching and so if someone changes what filters they have selected the arguments don't update unless the cache is cleared.

I tried doing this: $variables['element']['#cache']['max-age'] = 0; but it didn't change anything.

Is there a way to turn off caching for local tasks from my custom theme?

3
  • 1
    The answer is pretty much always to use cache contexts and cache tags. There is a bug with max-age where it doesn’t bubble and anyway setting 0 makes pages uncacheable.
    – mona lisa
    Jun 9, 2023 at 2:30
  • The bug: drupal.org/project/drupal/issues/2352009
    – mona lisa
    Jun 9, 2023 at 2:31
  • Query strings are an available cache context.
    – mona lisa
    Jun 9, 2023 at 2:45

1 Answer 1

1

The only officially supported way to add cache metadata in a preprocess hook is to #cache in the top level of $variables:

$variables['#cache']['tags'][] = 'foo';
$variables['#cache']['contexts'][] = 'url.query_args';
$variables['#cache'] = ['max-age' => 0];

Be careful not to overwrite existing tags or contexts.

If you want to use cache contexts or max-age for anonymous users you have to uninstall the Internal Page Cache module, this module works only with cache tags. The exception is the context for query arguments, these are part of the URL, which is used as storage key for the Internal Page Cache module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.