Score:0

Set no cache when generating dynamic local action via derivatives

vn flag

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:

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

Here's my derivative:

<?php

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];
    }
    else{
      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;
    $this->getCacheMaxAge();
    
    return parent::getDerivativeDefinitions($base_plugin_definition);
  }

  public function getCacheMaxAge() {
    return 0;
  }

}
Score:2
cn flag

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

Score:1
vn flag

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

leads.new_deal:
  route_name: entity.group_content.create_form
  title: 'New Deal'
  route_parameters:
    plugin_id: group_node:deals
    group: 0
  class: 'Drupal\leads\Plugin\Menu\DynamicLocalTasks'
  appears_on:
    - 'leads.deals'
<?php

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,
    ]; 
  }
}
vn flag
Worth noting that I didn't need to change the cache behavior with this method.
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.