Using a deriver, I'm able to produce local tasks for each entity of my custom configuration entity type. So if I have two configuration entity labeled "Foo" and "Bar", I can have similarly labeled tabs on a report page.
However, when adding or removing one of my configuration entity, the local tasks are not updated (ie. the tab for the deleted entity is still visible). This is not a surprise as these things get cached.
I've been able to fix the issues using implementations of hook_ENTITY_TYPE_insert()
and hook_ENTITY_TYPE_delete()
calling drupal_flush_all_caches()
. But clearing all caches in order to only get the tabs on a single page updated seems a bit overkill. I already tried to call both menu_cache_clear_all()
and Drupal::cache('discovery')->invalidateAll()
without success
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 date 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();
}
\Drupal\Core\Cache\Cache::invalidateTags(['rendered']);
But with this vague context is hard to answer. Please can you share you code?