EntityTypeEventSubscriberTrait.php 1.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

namespace Drupal\Core\Entity;

/**
 * Helper methods for EntityTypeListenerInterface.
 *
 * This allows a class implementing EntityTypeListenerInterface to subscribe and
 * react to entity type events.
 *
 * @see \Symfony\Component\EventDispatcher\EventSubscriberInterface
 * @see \Drupal\Core\Entity\EntityTypeListenerInterface
 */
trait EntityTypeEventSubscriberTrait {

  /**
17
   * Gets the subscribed events.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
   *
   * @return array
   *   An array of subscribed event names.
   *
   * @see \Symfony\Component\EventDispatcher\EventSubscriberInterface::getSubscribedEvents()
   */
  public static function getEntityTypeEvents() {
    $event = array('onEntityTypeEvent', 100);
    $events[EntityTypeEvents::CREATE][] = $event;
    $events[EntityTypeEvents::UPDATE][] = $event;
    $events[EntityTypeEvents::DELETE][] = $event;
    return $events;
  }

  /**
   * Listener method for any entity type definition event.
   *
   * @param \Drupal\Core\Entity\EntityTypeEvent $event
   *   The field storage definition event object.
   * @param string $event_name
   *   The event name.
   */
  public function onEntityTypeEvent(EntityTypeEvent $event, $event_name) {
    switch ($event_name) {
      case EntityTypeEvents::CREATE:
        $this->onEntityTypeCreate($event->getEntityType());
        break;

      case EntityTypeEvents::UPDATE:
        $this->onEntityTypeUpdate($event->getEntityType(), $event->getOriginal());
        break;

      case EntityTypeEvents::DELETE:
        $this->onEntityTypeDelete($event->getEntityType());
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
59 60
  public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
  }
61 62 63 64

  /**
   * {@inheritdoc}
   */
65 66
  public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
  }
67 68 69 70

  /**
   * {@inheritdoc}
   */
71 72
  public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
  }
73 74

}