You are here

class EntityTestDefinitionSubscriber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php \Drupal\entity_test\EntityTestDefinitionSubscriber

Test entity type and field storage definition event subscriber.

Hierarchy

Expanded class hierarchy of EntityTestDefinitionSubscriber

1 string reference to 'EntityTestDefinitionSubscriber'
entity_test.services.yml in core/modules/system/tests/modules/entity_test/entity_test.services.yml
core/modules/system/tests/modules/entity_test/entity_test.services.yml
1 service uses EntityTestDefinitionSubscriber
entity_test.definition.subscriber in core/modules/system/tests/modules/entity_test/entity_test.services.yml
Drupal\entity_test\EntityTestDefinitionSubscriber

File

core/modules/system/tests/modules/entity_test/src/EntityTestDefinitionSubscriber.php, line 24
Contains \Drupal\entity_test\EntityTestDefinitionSubscriber.

Namespace

Drupal\entity_test
View source
class EntityTestDefinitionSubscriber implements EventSubscriberInterface, EntityTypeListenerInterface, FieldStorageDefinitionListenerInterface {
  use EntityTypeEventSubscriberTrait;
  use FieldStorageDefinitionEventSubscriberTrait;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Flag determining whether events should be tracked.
   *
   * @var bool
   */
  protected $trackEvents = FALSE;

  /**
   * {@inheritdoc}
   */
  function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return static::getEntityTypeEvents() + static::getFieldStorageDefinitionEvents();
  }

  /**
   * {@inheritdoc}
   */
  public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
    $this
      ->storeEvent(EntityTypeEvents::CREATE);
  }

  /**
   * {@inheritdoc}
   */
  public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    $this
      ->storeEvent(EntityTypeEvents::UPDATE);
  }

  /**
   * {@inheritdoc}
   */
  public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
    $this
      ->storeEvent(EntityTypeEvents::DELETE);
  }

  /**
   * {@inheritdoc}
   */
  public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
    $this
      ->storeEvent(FieldStorageDefinitionEvents::CREATE);
  }

  /**
   * {@inheritdoc}
   */
  public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
    $this
      ->storeEvent(FieldStorageDefinitionEvents::UPDATE);
  }

  /**
   * {@inheritdoc}
   */
  public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {
    $this
      ->storeEvent(FieldStorageDefinitionEvents::DELETE);
  }

  /**
   * Enables event tracking.
   */
  public function enableEventTracking() {
    $this->trackEvents = TRUE;
  }

  /**
   * Checks whether an event has been dispatched.
   *
   * @param string $event_name
   *   The event name.
   *
   * @return bool
   *   TRUE if the event has been dispatched, FALSE otherwise.
   */
  public function hasEventFired($event_name) {
    return (bool) $this->state
      ->get($event_name);
  }

  /**
   * Stores the specified event.
   *
   * @param string $event_name
   *   The event name.
   */
  protected function storeEvent($event_name) {
    if ($this->trackEvents) {
      $this->state
        ->set($event_name, TRUE);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityTestDefinitionSubscriber::$state protected property The state service.
EntityTestDefinitionSubscriber::$trackEvents protected property Flag determining whether events should be tracked.
EntityTestDefinitionSubscriber::enableEventTracking public function Enables event tracking.
EntityTestDefinitionSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
EntityTestDefinitionSubscriber::hasEventFired public function Checks whether an event has been dispatched.
EntityTestDefinitionSubscriber::onEntityTypeCreate public function Reacts to the creation of the entity type. Overrides EntityTypeEventSubscriberTrait::onEntityTypeCreate
EntityTestDefinitionSubscriber::onEntityTypeDelete public function Reacts to the deletion of the entity type. Overrides EntityTypeEventSubscriberTrait::onEntityTypeDelete
EntityTestDefinitionSubscriber::onEntityTypeUpdate public function Reacts to the update of the entity type. Overrides EntityTypeEventSubscriberTrait::onEntityTypeUpdate
EntityTestDefinitionSubscriber::onFieldStorageDefinitionCreate public function Reacts to the creation of a field storage definition. Overrides FieldStorageDefinitionEventSubscriberTrait::onFieldStorageDefinitionCreate
EntityTestDefinitionSubscriber::onFieldStorageDefinitionDelete public function Reacts to the deletion of a field storage definition. Overrides FieldStorageDefinitionEventSubscriberTrait::onFieldStorageDefinitionDelete
EntityTestDefinitionSubscriber::onFieldStorageDefinitionUpdate public function Reacts to the update of a field storage definition. Overrides FieldStorageDefinitionEventSubscriberTrait::onFieldStorageDefinitionUpdate
EntityTestDefinitionSubscriber::storeEvent protected function Stores the specified event.
EntityTestDefinitionSubscriber::__construct function
EntityTypeEventSubscriberTrait::getEntityTypeEvents public static function Gets the subscribed events.
EntityTypeEventSubscriberTrait::onEntityTypeEvent public function Listener method for any entity type definition event.
FieldStorageDefinitionEventSubscriberTrait::getFieldStorageDefinitionEvents public static function Returns the subscribed events.
FieldStorageDefinitionEventSubscriberTrait::onFieldStorageDefinitionEvent public function Listener method for any field storage definition event.