I try to create some local second level tasks depending on some settings for a custom entity. But unfortunately sometimes these local tasks do not appear - they appear again when rebuilding caching and first level tabs appear but not these dynamically created second level tabs.
What I have done (the entity type name is "parliament_period"):
In mymodule.links.task.yml
entity.parliament_period.canonical:
route_name: entity.parliament_period.canonical
base_route: entity.parliament_period.canonical
title: 'Overview'
entity.parliament_period.edit_form:
route_name: entity.parliament_period.edit_form
base_route: entity.parliament_period.canonical
title: 'Edit'
entity.parliament_period.local_tasks:
deriver: 'Drupal\mymodule\Plugin\Derivative\ParliamentPeriodDynamicLocalTasks'
weight: 100
The class looks the following:
class ParliamentPeriodDynamicLocalTasks extends DeriverBase implements ContainerDeriverInterface {
/**
* @var \Drupal\Core\Routing\CurrentRouteMatch
*/
protected $currentRouteMatch;
/**
* @var ParliamentPeriod|null
*/
protected $parliamentPeriod;
/**
* DynamicLocalTasks constructor.
*
* @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match
*/
public function __construct(CurrentRouteMatch $current_route_match) {
$this->currentRouteMatch = $current_route_match;
$this->parliamentPeriod = $this->currentRouteMatch->getParameter('parliament_period');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('current_route_match')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
if (!$this->parliamentPeriod instanceof ParliamentPeriod) {
return [];
}
// Implement dynamic logic to provide values for the same keys as in example.links.task.yml.
$this->derivatives['entity.parliament_period.default']['title'] = 'Details';
$this->derivatives['entity.parliament_period.default']['provider'] = $base_plugin_definition["provider"];
$this->derivatives['entity.parliament_period.default']['route_name'] = 'entity.parliament_period.canonical';
$this->derivatives['entity.parliament_period.default']['parent_id'] = "entity.parliament_period.canonical";
if ($this->parliamentPeriod->isElection()) {
$this->derivatives['entity.parliament_period.candidacies']['title'] = "Candidacies";
$this->derivatives['entity.parliament_period.candidacies']['provider'] = $base_plugin_definition["provider"];
$this->derivatives['entity.parliament_period.candidacies']['route_name'] = 'entity.parliament_period.candidacies';
$this->derivatives['entity.parliament_period.candidacies']['parent_id'] = "entity.parliament_period.canonical";
$this->derivatives['entity.parliament_period.constituencies']['title'] = "Constituencies";
$this->derivatives['entity.parliament_period.constituencies']['provider'] = $base_plugin_definition["provider"];
$this->derivatives['entity.parliament_period.constituencies']['route_name'] = 'entity.parliament_period.constituencies';
$this->derivatives['entity.parliament_period.constituencies']['parent_id'] = "entity.parliament_period.canonical";
}
if ($this->parliamentPeriod->isLegislature()) {
$this->derivatives['entity.parliament_period.committees']['title'] = "Committees";
$this->derivatives['entity.parliament_period.committees']['provider'] = $base_plugin_definition["provider"];
$this->derivatives['entity.parliament_period.committees']['route_name'] = 'entity.parliament_period.committees';
$this->derivatives['entity.parliament_period.committees']['parent_id'] = "entity.parliament_period.canonical";
$this->derivatives['entity.parliament_period.committees']['title'] = "Mandates";
$this->derivatives['entity.parliament_period.committees']['provider'] = $base_plugin_definition["provider"];
$this->derivatives['entity.parliament_period.committees']['route_name'] = 'entity.parliament_period.mandates';
$this->derivatives['entity.parliament_period.committees']['parent_id'] = "entity.parliament_period.canonical";
}
return $this->derivatives;
}
}
MenuLinkDefault
, you need to extendLocalTaskDefault
.getCacheTags()
of the plugin. For the dynamic behavior, I think you can combine all three options, see the answer.