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;
}
}