Loading modules/core_event_dispatcher/src/Event/Menu/MenuLocalTasksAlterEvent.php 0 → 100644 +90 −0 Original line number Diff line number Diff line <?php namespace Drupal\core_event_dispatcher\Event\Menu; use Drupal\Component\EventDispatcher\Event; use Drupal\Core\Cache\RefinableCacheableDependencyInterface; use Drupal\core_event_dispatcher\MenuHookEvents; use Drupal\hook_event_dispatcher\Event\EventInterface; /** * Class MenuLocalTasksAlterEvent. * * @HookEvent( * id = "menu_local_tasks_alter", * alter = "menu_local_tasks" * ) */ class MenuLocalTasksAlterEvent extends Event implements EventInterface { /** * An associative array of menu local tasks data. * * @var array */ protected array $data; /** * MenuLocalTaskAlterEvent constructor. * * @param array $data * An associative array containing list of (up to 2) tab levels that * contain a list of tabs keyed by their href, each one being an * associative array. * @param string $routeName * The route name of the page. * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $cacheability * The cacheability metadata for the current route's local tasks. */ public function __construct(array &$data, protected string $routeName, protected RefinableCacheableDependencyInterface $cacheability) { $this->data = &$data; } /** * {@inheritdoc} */ public function getDispatcherType(): string { return MenuHookEvents::MENU_LOCAL_TASKS_ALTER; } /** * Gets the menu local tasks data. * * @return array * An associative array containing list of (up to 2) tab levels. */ public function &getData(): array { return $this->data; } /** * Sets the menu local tasks data. * * @param array $data * The menu local tasks data. */ public function setData(array $data): void { $this->data = $data; } /** * Gets the route name of the page. * * @return string * The route name of the page. */ public function getRouteName(): string { return $this->routeName; } /** * Gets the cacheability metadata for the current route's local tasks. * * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface * The cacheability metadata. */ public function getCacheability(): RefinableCacheableDependencyInterface { return $this->cacheability; } } modules/core_event_dispatcher/src/MenuHookEvents.php 0 → 100644 +24 −0 Original line number Diff line number Diff line <?php namespace Drupal\core_event_dispatcher; use Drupal\hook_event_dispatcher\HookEventDispatcherInterface; /** * Define events for menu hooks. */ final class MenuHookEvents { /** * Alter local tasks displayed on the page before they are rendered. * * @Event * * @see \Drupal\core_event_dispatcher\Event\Menu\MenuLocalTasksAlterEvent * @see hook_menu_local_tasks_alter() * * @var string */ public const MENU_LOCAL_TASKS_ALTER = HookEventDispatcherInterface::PREFIX . 'menu_local_tasks.alter'; } modules/core_event_dispatcher/tests/src/Kernel/Menu/MenuLocalTasksAlterEventTest.php 0 → 100644 +81 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\core_event_dispatcher\Kernel\Menu; use Drupal\Core\Cache\CacheableMetadata; use Drupal\core_event_dispatcher\Event\Menu\MenuLocalTasksAlterEvent; use Drupal\core_event_dispatcher\MenuHookEvents; use Drupal\KernelTests\KernelTestBase; use Drupal\Tests\hook_event_dispatcher\Kernel\ListenerTrait; /** * Class MenuLocalTasksAlterEventTest. * * @group hook_event_dispatcher * @group core_event_dispatcher * * @see MenuLocalTasksAlterEvent */ class MenuLocalTasksAlterEventTest extends KernelTestBase { use ListenerTrait; /** * {@inheritdoc} */ protected static $modules = [ 'hook_event_dispatcher', 'core_event_dispatcher', ]; /** * Test MenuLocalTasksAlterEvent. * * @throws \Exception */ public function testMenuLocalTasksAlterEvent(): void { $this->listen(MenuHookEvents::MENU_LOCAL_TASKS_ALTER, 'onMenuLocalTasksAlter', $this->exactly(2)); $localTaskManager = $this->container->get('plugin.manager.menu.local_task'); $noneLocalTasks = $localTaskManager->getLocalTasks('<none>'); $this->assertArrayHasKey('tabs', $noneLocalTasks); $this->assertEmpty($noneLocalTasks['tabs']); $frontLocalTasks = $localTaskManager->getLocalTasks('<front>'); $this->assertArrayHasKey('tabs', $frontLocalTasks); $this->assertIsArray($frontLocalTasks['tabs']); $this->assertNotEmpty($frontLocalTasks['tabs']); $this->assertArrayHasKey('foo', $frontLocalTasks['tabs']); $this->assertTrue($frontLocalTasks['tabs']['foo']); $this->assertArrayHasKey('bar', $frontLocalTasks['tabs']); $this->assertTrue($frontLocalTasks['tabs']['bar']); $this->assertArrayHasKey('cacheability', $frontLocalTasks); $cacheability = $frontLocalTasks['cacheability']; $this->assertInstanceOf(CacheableMetadata::class, $cacheability); $this->assertContains('kittens:dwarf-cat', $cacheability->getCacheTags()); } /** * Callback for MenuLocalTasksAlterEvent. * * @param \Drupal\core_event_dispatcher\Event\Menu\MenuLocalTasksAlterEvent $event * The event. */ public function onMenuLocalTasksAlter(MenuLocalTasksAlterEvent $event): void { if ($event->getRouteName() === '<front>') { $data = $event->getData(); $this->assertArrayHasKey('tabs', $data); $this->assertEmpty($data['tabs']); $data['tabs'][0]['foo'] = TRUE; $event->setData($data); $refData = &$event->getData(); $refData['tabs'][0]['bar'] = TRUE; } $this->assertNotContains('kittens:dwarf-cat', $event->getCacheability()->getCacheTags()); $event->getCacheability()->addCacheTags(['kittens:dwarf-cat']); } } Loading
modules/core_event_dispatcher/src/Event/Menu/MenuLocalTasksAlterEvent.php 0 → 100644 +90 −0 Original line number Diff line number Diff line <?php namespace Drupal\core_event_dispatcher\Event\Menu; use Drupal\Component\EventDispatcher\Event; use Drupal\Core\Cache\RefinableCacheableDependencyInterface; use Drupal\core_event_dispatcher\MenuHookEvents; use Drupal\hook_event_dispatcher\Event\EventInterface; /** * Class MenuLocalTasksAlterEvent. * * @HookEvent( * id = "menu_local_tasks_alter", * alter = "menu_local_tasks" * ) */ class MenuLocalTasksAlterEvent extends Event implements EventInterface { /** * An associative array of menu local tasks data. * * @var array */ protected array $data; /** * MenuLocalTaskAlterEvent constructor. * * @param array $data * An associative array containing list of (up to 2) tab levels that * contain a list of tabs keyed by their href, each one being an * associative array. * @param string $routeName * The route name of the page. * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $cacheability * The cacheability metadata for the current route's local tasks. */ public function __construct(array &$data, protected string $routeName, protected RefinableCacheableDependencyInterface $cacheability) { $this->data = &$data; } /** * {@inheritdoc} */ public function getDispatcherType(): string { return MenuHookEvents::MENU_LOCAL_TASKS_ALTER; } /** * Gets the menu local tasks data. * * @return array * An associative array containing list of (up to 2) tab levels. */ public function &getData(): array { return $this->data; } /** * Sets the menu local tasks data. * * @param array $data * The menu local tasks data. */ public function setData(array $data): void { $this->data = $data; } /** * Gets the route name of the page. * * @return string * The route name of the page. */ public function getRouteName(): string { return $this->routeName; } /** * Gets the cacheability metadata for the current route's local tasks. * * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface * The cacheability metadata. */ public function getCacheability(): RefinableCacheableDependencyInterface { return $this->cacheability; } }
modules/core_event_dispatcher/src/MenuHookEvents.php 0 → 100644 +24 −0 Original line number Diff line number Diff line <?php namespace Drupal\core_event_dispatcher; use Drupal\hook_event_dispatcher\HookEventDispatcherInterface; /** * Define events for menu hooks. */ final class MenuHookEvents { /** * Alter local tasks displayed on the page before they are rendered. * * @Event * * @see \Drupal\core_event_dispatcher\Event\Menu\MenuLocalTasksAlterEvent * @see hook_menu_local_tasks_alter() * * @var string */ public const MENU_LOCAL_TASKS_ALTER = HookEventDispatcherInterface::PREFIX . 'menu_local_tasks.alter'; }
modules/core_event_dispatcher/tests/src/Kernel/Menu/MenuLocalTasksAlterEventTest.php 0 → 100644 +81 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\core_event_dispatcher\Kernel\Menu; use Drupal\Core\Cache\CacheableMetadata; use Drupal\core_event_dispatcher\Event\Menu\MenuLocalTasksAlterEvent; use Drupal\core_event_dispatcher\MenuHookEvents; use Drupal\KernelTests\KernelTestBase; use Drupal\Tests\hook_event_dispatcher\Kernel\ListenerTrait; /** * Class MenuLocalTasksAlterEventTest. * * @group hook_event_dispatcher * @group core_event_dispatcher * * @see MenuLocalTasksAlterEvent */ class MenuLocalTasksAlterEventTest extends KernelTestBase { use ListenerTrait; /** * {@inheritdoc} */ protected static $modules = [ 'hook_event_dispatcher', 'core_event_dispatcher', ]; /** * Test MenuLocalTasksAlterEvent. * * @throws \Exception */ public function testMenuLocalTasksAlterEvent(): void { $this->listen(MenuHookEvents::MENU_LOCAL_TASKS_ALTER, 'onMenuLocalTasksAlter', $this->exactly(2)); $localTaskManager = $this->container->get('plugin.manager.menu.local_task'); $noneLocalTasks = $localTaskManager->getLocalTasks('<none>'); $this->assertArrayHasKey('tabs', $noneLocalTasks); $this->assertEmpty($noneLocalTasks['tabs']); $frontLocalTasks = $localTaskManager->getLocalTasks('<front>'); $this->assertArrayHasKey('tabs', $frontLocalTasks); $this->assertIsArray($frontLocalTasks['tabs']); $this->assertNotEmpty($frontLocalTasks['tabs']); $this->assertArrayHasKey('foo', $frontLocalTasks['tabs']); $this->assertTrue($frontLocalTasks['tabs']['foo']); $this->assertArrayHasKey('bar', $frontLocalTasks['tabs']); $this->assertTrue($frontLocalTasks['tabs']['bar']); $this->assertArrayHasKey('cacheability', $frontLocalTasks); $cacheability = $frontLocalTasks['cacheability']; $this->assertInstanceOf(CacheableMetadata::class, $cacheability); $this->assertContains('kittens:dwarf-cat', $cacheability->getCacheTags()); } /** * Callback for MenuLocalTasksAlterEvent. * * @param \Drupal\core_event_dispatcher\Event\Menu\MenuLocalTasksAlterEvent $event * The event. */ public function onMenuLocalTasksAlter(MenuLocalTasksAlterEvent $event): void { if ($event->getRouteName() === '<front>') { $data = $event->getData(); $this->assertArrayHasKey('tabs', $data); $this->assertEmpty($data['tabs']); $data['tabs'][0]['foo'] = TRUE; $event->setData($data); $refData = &$event->getData(); $refData['tabs'][0]['bar'] = TRUE; } $this->assertNotContains('kittens:dwarf-cat', $event->getCacheability()->getCacheTags()); $event->getCacheability()->addCacheTags(['kittens:dwarf-cat']); } }