The tabs are build using the following code:
MODULE.tasks.yml
:
MODULE.settings.webhooks_list:
deriver: 'Drupal\MODULE\Plugin\Menu\LocalTask\ReportLocalTasks'
base_route: MODULE.settings.list
src/Plugin/Menu/LocalTasks/ReportLocalTasks.php
:
namespace Drupal\MODULE\Plugin\Menu\LocalTask;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\MODULE\CustomEntityTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ReportLocalTasks extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $entityManager;
/**
* Constructor.
*
* @param $entityManager
* @param $stringTranslation;
*/
public function __construct(EntityTypeManagerInterface $entityManager, TranslationInterface $stringTranslation) {
$this->entityManager = $entityManager;
$this->stringTranslation = $stringTranslation;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity_type.manager'),
$container->get('string_translation')
);
}
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [];
/** @var EntityStorageInterface $entityStorage */
$entityStorage = $this->entityManager->getStorage('custom_type');
/** @var CustomEntityTypeInterface $connection */
foreach ($entityStorage->loadMultiple() as $entity) {
$this->derivatives["MODULE.settings.list.{$entity->id()}"] = [
'title' => $entity->label(),
'route_name' => 'MODULE_ecom.settings.list',
'route_parameters' => [
'connection' => $entity->id(),
],
];
}
foreach ($this->derivatives as &$entry) {
$entry += $base_plugin_definition;
}
return $this->derivatives;
}
}
In order to have up to dates tabs when adding or removing custom entity, I'm using the following code. What I'm trying to do is to have something more specific than the call the drupal_flush_all_caches()
.
MODULE.module
/**
* Implements hook_ENTITY_TYPE_insert();
*/
function MODULE_CUSTOM_ENTITY_ETYPE_insert(\Drupal\MODULE\CustomEntityTypeInterface $connection) {
drupal_flush_all_caches();
}
/**
* Implements hook_ENTITY_TYPE_delete();
*/
function lMODULE_CUSTOM_ENTITY_ETYPE_delete(\Drupal\MODULE\CustomEntityTypeInterface $connection) {
drupal_flush_all_caches();
}