You are here

class EntitySchemaSubscriber in Drupal 8

Same name in this branch
  1. 8 core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber
  2. 8 core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\entity_test_update\EventSubscriber\EntitySchemaSubscriber
Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\entity_test_update\EventSubscriber\EntitySchemaSubscriber

Defines a class for listening to entity schema changes.

Hierarchy

Expanded class hierarchy of EntitySchemaSubscriber

1 string reference to 'EntitySchemaSubscriber'
entity_test_update.services.yml in core/modules/system/tests/modules/entity_test_update/entity_test_update.services.yml
core/modules/system/tests/modules/entity_test_update/entity_test_update.services.yml
1 service uses EntitySchemaSubscriber
entity_test_update.entity_schema_listener in core/modules/system/tests/modules/entity_test_update/entity_test_update.services.yml
Drupal\entity_test_update\EventSubscriber\EntitySchemaSubscriber

File

core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php, line 17

Namespace

Drupal\entity_test_update\EventSubscriber
View source
class EntitySchemaSubscriber implements EntityTypeListenerInterface, EventSubscriberInterface {
  use EntityTypeEventSubscriberTrait;

  /**
   * The entity definition update manager.
   *
   * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
   */
  protected $entityDefinitionUpdateManager;

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

  /**
   * Constructs a new EntitySchemaSubscriber.
   *
   * @param \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager
   *   The entity definition update manager.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   */
  public function __construct(EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager, StateInterface $state) {
    $this->entityDefinitionUpdateManager = $entityDefinitionUpdateManager;
    $this->state = $state;
  }

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

  /**
   * {@inheritdoc}
   */
  public function onEntityTypeCreate(EntityTypeInterface $entity_type) {

    // Only add the new base field when a test needs it.
    if (!$this->state
      ->get('entity_test_update.install_new_base_field_during_create', FALSE)) {
      return;
    }

    // Add a new base field when the entity type is created.
    $definitions = $this->state
      ->get('entity_test_update.additional_base_field_definitions', []);
    $definitions['new_base_field'] = BaseFieldDefinition::create('string')
      ->setName('new_base_field')
      ->setLabel(new TranslatableMarkup('A new base field'));
    $this->state
      ->set('entity_test_update.additional_base_field_definitions', $definitions);
    $this->entityDefinitionUpdateManager
      ->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test_update', $definitions['new_base_field']);
  }

  /**
   * {@inheritdoc}
   */
  public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {

    // Only add the new base field when a test needs it.
    if (!$this->state
      ->get('entity_test_update.install_new_base_field_during_update', FALSE)) {
      return;
    }

    // Add a new base field when the entity type is updated.
    $definitions = $this->state
      ->get('entity_test_update.additional_base_field_definitions', []);
    $definitions['new_base_field'] = BaseFieldDefinition::create('string')
      ->setName('new_base_field')
      ->setLabel(new TranslatableMarkup('A new base field'));
    $this->state
      ->set('entity_test_update.additional_base_field_definitions', $definitions);
    $this->entityDefinitionUpdateManager
      ->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test_update', $definitions['new_base_field']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntitySchemaSubscriber::$entityDefinitionUpdateManager protected property The entity definition update manager.
EntitySchemaSubscriber::$state protected property The state service.
EntitySchemaSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntitySchemaSubscriber::onEntityTypeCreate public function Reacts to the creation of the entity type. Overrides EntityTypeEventSubscriberTrait::onEntityTypeCreate
EntitySchemaSubscriber::onEntityTypeUpdate public function Reacts to the update of the entity type. Overrides EntityTypeEventSubscriberTrait::onEntityTypeUpdate
EntitySchemaSubscriber::__construct public function Constructs a new EntitySchemaSubscriber.
EntityTypeEventSubscriberTrait::getEntityTypeEvents public static function Gets the subscribed events.
EntityTypeEventSubscriberTrait::onEntityTypeDelete public function 3
EntityTypeEventSubscriberTrait::onEntityTypeEvent public function Listener method for any entity type definition event.
EntityTypeEventSubscriberTrait::onFieldableEntityTypeCreate public function 2
EntityTypeEventSubscriberTrait::onFieldableEntityTypeUpdate public function 2