Score:0

Turn off caching for local tasks menu

au flag

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?

id flag
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.
id flag
The bug: https://www.drupal.org/project/drupal/issues/2352009
id flag
Query strings are an available cache context.
Score:1
cn flag

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.