You are here

class ExampleEntityEventSubscribers in Hook Event Dispatcher 8

Class ExampleEntityEventSubscribers.

Don't forget to define your class as a service and tag it as an "event_subscriber":

services: hook_event_dispatcher.example_entity_subscribers: class:'\Drupal\hook_event_dispatcher\Example\ExampleEntityEventSubscribers' tags:

  • { name: 'event_subscriber' }

Hierarchy

  • class \Drupal\hook_event_dispatcher\Example\ExampleEntityEventSubscribers implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ExampleEntityEventSubscribers

File

src/Example/ExampleEntityEventSubscribers.php, line 27

Namespace

Drupal\hook_event_dispatcher\Example
View source
class ExampleEntityEventSubscribers implements EventSubscriberInterface {

  /**
   * Alter entity view.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Entity\EntityViewEvent $event
   *   The event.
   */
  public function alterEntityView(EntityViewEvent $event) {
    $entity = $event
      ->getEntity();

    // Only do this for entities of type Node.
    if ($entity instanceof NodeInterface) {
      $build =& $event
        ->getBuild();
      $build['extra_markup'] = [
        '#markup' => 'this is extra markup',
      ];
    }
  }

  /**
   * Entity pre save.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Entity\EntityPresaveEvent $event
   *   The event.
   */
  public function entityPreSave(EntityPresaveEvent $event) {
    $entity = $event
      ->getEntity();
    $entity->special_field->value = 'PreSave!';
  }

  /**
   * Entity insert.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Entity\EntityInsertEvent $event
   *   The event.
   */
  public function entityInsert(EntityInsertEvent $event) {

    // Do some fancy stuff with new entity.
    $entity = $event
      ->getEntity();
    $entity->special_field->value = 'Insert!';
  }

  /**
   * Entity update.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Entity\EntityUpdateEvent $event
   *   The event.
   */
  public function entityUpdate(EntityUpdateEvent $event) {

    // Do some fancy stuff, when entity is updated.
    $entity = $event
      ->getEntity();
    $entity->special_field->value = 'Update!';
  }

  /**
   * Entity pre delete.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Entity\EntityPredeleteEvent $event
   *   The event.
   */
  public function entityPreDelete(EntityPredeleteEvent $event) {

    // Do something before entity is deleted.
    $entity = $event
      ->getEntity();
    $entity->special_field->value = 'PreDelete!';
  }

  /**
   * Entity delete.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Entity\EntityDeleteEvent $event
   *   The event.
   */
  public function entityDelete(EntityDeleteEvent $event) {

    // Do some fancy stuff, after entity is deleted.
    $entity = $event
      ->getEntity();
    $entity->special_field->value = 'Deleted!';
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      HookEventDispatcherInterface::ENTITY_VIEW => 'alterEntityView',
      HookEventDispatcherInterface::ENTITY_PRE_SAVE => 'entityPreSave',
      HookEventDispatcherInterface::ENTITY_INSERT => 'entityInsert',
      HookEventDispatcherInterface::ENTITY_UPDATE => 'entityUpdate',
      HookEventDispatcherInterface::ENTITY_PRE_DELETE => 'entityPreDelete',
      HookEventDispatcherInterface::ENTITY_DELETE => 'entityDelete',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExampleEntityEventSubscribers::alterEntityView public function Alter entity view.
ExampleEntityEventSubscribers::entityDelete public function Entity delete.
ExampleEntityEventSubscribers::entityInsert public function Entity insert.
ExampleEntityEventSubscribers::entityPreDelete public function Entity pre delete.
ExampleEntityEventSubscribers::entityPreSave public function Entity pre save.
ExampleEntityEventSubscribers::entityUpdate public function Entity update.
ExampleEntityEventSubscribers::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.