Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityTypeEventSubscriberTrait.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 /**
6  * Helper methods for EntityTypeListenerInterface.
7  *
8  * This allows a class implementing EntityTypeListenerInterface to subscribe and
9  * react to entity type events.
10  *
11  * @see \Symfony\Component\EventDispatcher\EventSubscriberInterface
12  * @see \Drupal\Core\Entity\EntityTypeListenerInterface
13  */
14 trait EntityTypeEventSubscriberTrait {
15
16   /**
17    * Gets the subscribed events.
18    *
19    * @return array
20    *   An array of subscribed event names.
21    *
22    * @see \Symfony\Component\EventDispatcher\EventSubscriberInterface::getSubscribedEvents()
23    */
24   public static function getEntityTypeEvents() {
25     $event = ['onEntityTypeEvent', 100];
26     $events[EntityTypeEvents::CREATE][] = $event;
27     $events[EntityTypeEvents::UPDATE][] = $event;
28     $events[EntityTypeEvents::DELETE][] = $event;
29     return $events;
30   }
31
32   /**
33    * Listener method for any entity type definition event.
34    *
35    * @param \Drupal\Core\Entity\EntityTypeEvent $event
36    *   The field storage definition event object.
37    * @param string $event_name
38    *   The event name.
39    */
40   public function onEntityTypeEvent(EntityTypeEvent $event, $event_name) {
41     switch ($event_name) {
42       case EntityTypeEvents::CREATE:
43         $this->onEntityTypeCreate($event->getEntityType());
44         break;
45
46       case EntityTypeEvents::UPDATE:
47         $this->onEntityTypeUpdate($event->getEntityType(), $event->getOriginal());
48         break;
49
50       case EntityTypeEvents::DELETE:
51         $this->onEntityTypeDelete($event->getEntityType());
52         break;
53     }
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
72   }
73
74 }