EntitySchemaSubscriber.php in Drupal 8
File
core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php
View source
<?php
namespace Drupal\workspaces\EventSubscriber;
use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface;
use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
use Drupal\Core\Entity\EntityTypeEventSubscriberTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeListenerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\workspaces\WorkspaceManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EntitySchemaSubscriber implements EntityTypeListenerInterface, EventSubscriberInterface {
use EntityTypeEventSubscriberTrait;
use StringTranslationTrait;
protected $entityDefinitionUpdateManager;
protected $entityLastInstalledSchemaRepository;
protected $workspaceManager;
public function __construct(EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager, EntityLastInstalledSchemaRepositoryInterface $entityLastInstalledSchemaRepository, WorkspaceManagerInterface $workspace_manager) {
$this->entityDefinitionUpdateManager = $entityDefinitionUpdateManager;
$this->entityLastInstalledSchemaRepository = $entityLastInstalledSchemaRepository;
$this->workspaceManager = $workspace_manager;
}
public static function getSubscribedEvents() {
return static::getEntityTypeEvents();
}
public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
if ($this->workspaceManager
->isEntityTypeSupported($entity_type)) {
$this
->addRevisionMetadataField($entity_type);
}
}
public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
$this
->onEntityTypeCreate($entity_type);
}
public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
if ($this->workspaceManager
->isEntityTypeSupported($entity_type) && !$this->workspaceManager
->isEntityTypeSupported($original)) {
$this
->addRevisionMetadataField($entity_type);
}
if ($this->workspaceManager
->isEntityTypeSupported($original) && !$this->workspaceManager
->isEntityTypeSupported($entity_type)) {
$revision_metadata_keys = $original
->get('revision_metadata_keys');
$field_storage_definition = $this->entityLastInstalledSchemaRepository
->getLastInstalledFieldStorageDefinitions($entity_type
->id())[$revision_metadata_keys['workspace']];
$this->entityDefinitionUpdateManager
->uninstallFieldStorageDefinition($field_storage_definition);
$entity_type
->setRevisionMetadataKey('workspace', NULL);
$this->entityLastInstalledSchemaRepository
->setLastInstalledDefinition($entity_type);
}
}
public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
$this
->onEntityTypeUpdate($entity_type, $original);
}
public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
}
protected function addRevisionMetadataField(EntityTypeInterface $entity_type) {
if (!$entity_type
->hasRevisionMetadataKey('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.");
}
$entity_type
->setRevisionMetadataKey('workspace', 'workspace');
$this->entityLastInstalledSchemaRepository
->setLastInstalledDefinition($entity_type);
}
$this->entityDefinitionUpdateManager
->installFieldStorageDefinition($entity_type
->getRevisionMetadataKey('workspace'), $entity_type
->id(), 'workspaces', $this
->getWorkspaceFieldDefinition());
}
protected function getWorkspaceFieldDefinition() {
return BaseFieldDefinition::create('entity_reference')
->setLabel($this
->t('Workspace'))
->setDescription($this
->t('Indicates the workspace that this revision belongs to.'))
->setSetting('target_type', 'workspace')
->setInternal(TRUE)
->setTranslatable(FALSE)
->setRevisionable(TRUE);
}
}