function EntitySchemaSubscriber::addRevisionMetadataField

Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber::addRevisionMetadataField()
  2. 8.9.x core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber::addRevisionMetadataField()
  3. 10 core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber::addRevisionMetadataField()

Adds the 'workspace' revision metadata field to an entity type.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type that has been installed or updated.

2 calls to EntitySchemaSubscriber::addRevisionMetadataField()
EntitySchemaSubscriber::onEntityTypeCreate in core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php
Reacts to the creation of the entity type.
EntitySchemaSubscriber::onEntityTypeUpdate in core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php
Reacts to the update of the entity type.

File

core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php, line 129

Class

EntitySchemaSubscriber
Defines a class for listening to entity schema changes.

Namespace

Drupal\workspaces\EventSubscriber

Code

protected function addRevisionMetadataField(EntityTypeInterface $entity_type) {
    if (!$entity_type->hasRevisionMetadataKey('workspace')) {
        // Bail out if there's an existing field called 'workspace'.
        if ($this->entityDefinitionUpdateManager
            ->getFieldStorageDefinition('workspace', $entity_type->id())) {
            throw new \RuntimeException("An existing 'workspace' field was found for the '{$entity_type->id()}' entity type. Set the 'workspace' revision metadata key to use a different field name and run this update function again.");
        }
        // We are only adding a revision metadata key so we don't need to go
        // through the entity update process.
        $entity_type->setRevisionMetadataKey('workspace', 'workspace');
        $this->entityLastInstalledSchemaRepository
            ->setLastInstalledDefinition($entity_type);
    }
    $this->entityDefinitionUpdateManager
        ->installFieldStorageDefinition($entity_type->getRevisionMetadataKey('workspace'), $entity_type->id(), 'workspaces', $this->getWorkspaceFieldDefinition());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.