I'm trying to generate a dynamic local action via a derivative. The derivative works and returns a link generated dynamically but the cache seems to maintain its value.

As per this Drupal core change log, I understood I could set cache parameters on the derivative, which I tried but in vain so far - I'm not very familiar with these concepts yet so appreciate any guidance on how to prevent $gid from being cached.

Here's my local action leads.links.action.yml:

  deriver: 'Drupal\leads\Plugin\Derivative\DynamicLocalTasks'

Here's my derivative:


namespace Drupal\leads\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableDependencyTrait;

* Defines dynamic local tasks.
class DynamicLocalTasks extends DeriverBase {
  use CacheableDependencyTrait;
  * {@inheritdoc}
  public function getDerivativeDefinitions($base_plugin_definition) {
    $groupLead = getGroupsUserIsLead();

    if (isset($groupLead[0])){
      $gid = $groupLead[0];
      return \Drupal\Core\Access\AccessResult::forbidden();

    // Implement dynamic logic to provide values for the same keys as in example.links.task.yml.
    $this->derivatives['leads.new_deal'] = $base_plugin_definition;
    $this->derivatives['leads.new_deal']['title'] = "New Deal";
    $this->derivatives['leads.new_deal']['route_name'] = 'entity.group_content.create_form';
    $this->derivatives['leads.new_deal']['appears_on'] = ['leads.deals'];
    $this->derivatives['leads.new_deal']['route_parameters'] = ['plugin_id' => 'group_node:deals','group' => $gid];
    $this->cacheMaxAge = 0;
    return parent::getDerivativeDefinitions($base_plugin_definition);

  public function getCacheMaxAge() {
    return 0;


2 Answers 2


Adding the CacheableDependencyTrait to a deriver doesn't make it fully cacheable. There is no code in Drupal checking for cacheable dependencies of derivers in a normal request. You have to return the title, route name and so on from a local task plugin where you can also use getCache*() methods. See https://drupal.stackexchange.com/a/273293/47547


As suggested by @4uk4, here's what I ended up doing:

  route_name: entity.group_content.create_form
  title: 'New Deal'
    plugin_id: group_node:deals
    group: 0
  class: 'Drupal\leads\Plugin\Menu\DynamicLocalTasks'
    - 'leads.deals'

namespace Drupal\leads\Plugin\Menu;

use Drupal\Core\Menu\LocalActionDefault;
use Drupal\Core\Routing\RouteMatchInterface;

class DynamicLocalTasks extends LocalActionDefault {
  public function getRouteParameters(RouteMatchInterface $route_match) {
    $groupLead = getGroupsUserIsLead();

    $gid = $groupLead[0] ?? NULL;

    return [
      'plugin_id' => 'group_node:deals',
      'group' => $gid,
  • Worth noting that I didn't need to change the cache behavior with this method.
    – jozi
    Commented May 30, 2022 at 11:34

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.