Commit 5602d055 authored by Luhur Abdi Rizal's avatar Luhur Abdi Rizal
Browse files

Issue #3266483 by el7cosmos: Move toolbar hook event constants to a class

parent 0caae7d2
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -2,9 +2,9 @@

namespace Drupal\toolbar_event_dispatcher\Event\Toolbar;

use Drupal\hook_event_dispatcher\Event\EventInterface;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Component\EventDispatcher\Event;
use Drupal\hook_event_dispatcher\Event\EventInterface;
use Drupal\toolbar_event_dispatcher\ToolbarHookEvents;

/**
 * Class ToolbarAlterEvent.
@@ -42,7 +42,7 @@ class ToolbarAlterEvent extends Event implements EventInterface {
   * {@inheritdoc}
   */
  public function getDispatcherType(): string {
    return HookEventDispatcherInterface::TOOLBAR_ALTER;
    return ToolbarHookEvents::TOOLBAR_ALTER;
  }

}
+3 −3
Original line number Diff line number Diff line
@@ -2,9 +2,9 @@

namespace Drupal\toolbar_event_dispatcher\Event\Toolbar;

use Drupal\hook_event_dispatcher\Event\EventInterface;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Component\EventDispatcher\Event;
use Drupal\hook_event_dispatcher\Event\EventInterface;
use Drupal\toolbar_event_dispatcher\ToolbarHookEvents;

/**
 * Class ToolbarEvent.
@@ -15,7 +15,7 @@ final class ToolbarEvent extends Event implements EventInterface {
   * {@inheritdoc}
   */
  public function getDispatcherType(): string {
    return HookEventDispatcherInterface::TOOLBAR;
    return ToolbarHookEvents::TOOLBAR;
  }

}
+38 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\toolbar_event_dispatcher;

use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;

/**
 * Defines events for toolbar hooks.
 */
final class ToolbarHookEvents {

  /**
   * Alter the toolbar menu after hook_toolbar() is invoked.
   *
   * @Event
   *
   * @see \Drupal\toolbar_event_dispatcher\Event\Toolbar\ToolbarEvent
   * @see toolbar_event_dispatcher_toolbar()
   * @see hook_toolbar()
   *
   * @var string
   */
  public const TOOLBAR = HookEventDispatcherInterface::PREFIX . 'toolbar';

  /**
   * Alter the toolbar menu after hook_toolbar() is invoked.
   *
   * @Event
   *
   * @see \Drupal\toolbar_event_dispatcher\Event\Toolbar\ToolbarAlterEvent
   * @see toolbar_event_dispatcher_toolbar_alter()
   * @see hook_toolbar_alter()
   *
   * @var string
   */
  public const TOOLBAR_ALTER = HookEventDispatcherInterface::PREFIX . 'toolbar.alter';

}
+5 −5
Original line number Diff line number Diff line
@@ -2,11 +2,11 @@

namespace Drupal\Tests\toolbar_event_dispatcher\Kernel;

use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\hook_event_dispatcher\Kernel\ListenerTrait;
use Drupal\toolbar\Element\Toolbar;
use Drupal\toolbar_event_dispatcher\Event\Toolbar\ToolbarAlterEvent;
use Drupal\toolbar_event_dispatcher\ToolbarHookEvents;

/**
 * Class ToolbarAlterEventTest.
@@ -38,8 +38,8 @@ class ToolbarEventTest extends KernelTestBase {
   * @throws \Exception
   */
  public function testToolbarAlterEvent(): void {
    $this->listen(HookEventDispatcherInterface::TOOLBAR, 'onToolbar');
    $this->listen(HookEventDispatcherInterface::TOOLBAR_ALTER, 'onToolbarAlter');
    $this->listen(ToolbarHookEvents::TOOLBAR, 'onToolbar');
    $this->listen(ToolbarHookEvents::TOOLBAR_ALTER, 'onToolbarAlter');

    $toolbar = Toolbar::preRenderToolbar([]);
    $this->assertArrayHasKey('test', $toolbar);
@@ -49,7 +49,7 @@ class ToolbarEventTest extends KernelTestBase {
  /**
   * Callback for ToolbarEvent.
   */
  public function onToolbar() {
  public function onToolbar(): void {
  }

  /**
@@ -58,7 +58,7 @@ class ToolbarEventTest extends KernelTestBase {
   * @param \Drupal\toolbar_event_dispatcher\Event\Toolbar\ToolbarAlterEvent $event
   *   The event.
   */
  public function onToolbarAlter(ToolbarAlterEvent $event) {
  public function onToolbarAlter(ToolbarAlterEvent $event): void {
    $items = &$event->getItems();
    $items['test'] = ['item'];
  }
+13 −0
Original line number Diff line number Diff line
@@ -1345,6 +1345,12 @@ interface HookEventDispatcherInterface {
   *
   * @Event
   *
   * @deprecated in hook_event_dispatcher:3.1.0 and is removed from
   *   hook_event_dispatcher:4.0.0. Use
   *   \Drupal\toolbar_event_dispatcher\ToolbarHookEvents::TOOLBAR instead.
   *
   * @see https://www.drupal.org/node/3263301
   * @see \Drupal\toolbar_event_dispatcher\Event\Toolbar\ToolbarEvent
   * @see toolbar_event_dispatcher_toolbar()
   * @see hook_toolbar()
   *
@@ -1357,6 +1363,13 @@ interface HookEventDispatcherInterface {
   *
   * @Event
   *
   * @deprecated in hook_event_dispatcher:3.1.0 and is removed from
   *   hook_event_dispatcher:4.0.0. Use
   *   \Drupal\toolbar_event_dispatcher\ToolbarHookEvents::TOOLBAR_ALTER
   *   instead.
   *
   * @see https://www.drupal.org/node/3263301
   * @see \Drupal\toolbar_event_dispatcher\Event\Toolbar\ToolbarAlterEvent
   * @see toolbar_event_dispatcher_toolbar_alter()
   * @see hook_toolbar_alter()
   *