class EntitySchemaSubscriber
Same name in this branch
- 11.x core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\entity_test_update\EventSubscriber\EntitySchemaSubscriber
Same name in other branches
- 9 core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber
- 9 core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\entity_test_update\EventSubscriber\EntitySchemaSubscriber
- 8.9.x core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber
- 8.9.x core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\entity_test_update\EventSubscriber\EntitySchemaSubscriber
- 10 core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber
- 10 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
- class \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber implements \Drupal\Core\Entity\EntityTypeListenerInterface, \Symfony\Component\EventDispatcher\EventSubscriberInterface uses \Drupal\Core\Entity\EntityTypeEventSubscriberTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of EntitySchemaSubscriber
1 string reference to 'EntitySchemaSubscriber'
- workspaces.services.yml in core/
modules/ workspaces/ workspaces.services.yml - core/modules/workspaces/workspaces.services.yml
1 service uses EntitySchemaSubscriber
- workspaces.entity_schema_listener in core/
modules/ workspaces/ workspaces.services.yml - Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber
File
-
core/
modules/ workspaces/ src/ EventSubscriber/ EntitySchemaSubscriber.php, line 18
Namespace
Drupal\workspaces\EventSubscriberView source
class EntitySchemaSubscriber implements EntityTypeListenerInterface, EventSubscriberInterface {
use EntityTypeEventSubscriberTrait;
use StringTranslationTrait;
/**
* The definition update manager.
*
* @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
*/
protected $entityDefinitionUpdateManager;
/**
* The last installed schema definitions.
*
* @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
*/
protected $entityLastInstalledSchemaRepository;
/**
* The workspace information service.
*
* @var \Drupal\workspaces\WorkspaceInformationInterface
*/
protected $workspaceInfo;
/**
* Constructs a new EntitySchemaSubscriber.
*
* @param \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager
* Definition update manager.
* @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entityLastInstalledSchemaRepository
* Last definitions.
* @param \Drupal\workspaces\WorkspaceInformationInterface $workspace_information
* The workspace information service.
*/
public function __construct(EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager, EntityLastInstalledSchemaRepositoryInterface $entityLastInstalledSchemaRepository, WorkspaceInformationInterface $workspace_information) {
$this->entityDefinitionUpdateManager = $entityDefinitionUpdateManager;
$this->entityLastInstalledSchemaRepository = $entityLastInstalledSchemaRepository;
$this->workspaceInfo = $workspace_information;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() : array {
return static::getEntityTypeEvents();
}
/**
* {@inheritdoc}
*/
public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
// If the entity type is supported by Workspaces, add the revision metadata
// field.
if ($this->workspaceInfo
->isEntityTypeSupported($entity_type)) {
$this->addRevisionMetadataField($entity_type);
}
}
/**
* {@inheritdoc}
*/
public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
$this->onEntityTypeCreate($entity_type);
}
/**
* {@inheritdoc}
*/
public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
// If the entity type is now supported by Workspaces, add the revision
// metadata field.
if ($this->workspaceInfo
->isEntityTypeSupported($entity_type) && !$this->workspaceInfo
->isEntityTypeSupported($original)) {
$this->addRevisionMetadataField($entity_type);
}
// If the entity type is no longer supported by Workspaces, remove the
// revision metadata field.
if ($this->workspaceInfo
->isEntityTypeSupported($original) && !$this->workspaceInfo
->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);
// We are only removing a revision metadata key so we don't need to go
// through the entity update process.
$entity_type->setRevisionMetadataKey('workspace', NULL);
$this->entityLastInstalledSchemaRepository
->setLastInstalledDefinition($entity_type);
}
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
// Nothing to do here.
}
/**
* Adds the 'workspace' revision metadata field to an entity type.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type that has been installed or updated.
*/
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());
}
/**
* Gets the base field definition for the 'workspace' revision metadata field.
*
* @return \Drupal\Core\Field\BaseFieldDefinition
* The base field definition.
*/
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);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
EntitySchemaSubscriber::$entityDefinitionUpdateManager | protected | property | The definition update manager. | ||
EntitySchemaSubscriber::$entityLastInstalledSchemaRepository | protected | property | The last installed schema definitions. | ||
EntitySchemaSubscriber::$workspaceInfo | protected | property | The workspace information service. | ||
EntitySchemaSubscriber::addRevisionMetadataField | protected | function | Adds the 'workspace' revision metadata field to an entity type. | ||
EntitySchemaSubscriber::getSubscribedEvents | public static | function | |||
EntitySchemaSubscriber::getWorkspaceFieldDefinition | protected | function | Gets the base field definition for the 'workspace' revision metadata field. | ||
EntitySchemaSubscriber::onEntityTypeCreate | public | function | Reacts to the creation of the entity type. | Overrides EntityTypeEventSubscriberTrait::onEntityTypeCreate | |
EntitySchemaSubscriber::onEntityTypeDelete | public | function | Reacts to the deletion of the entity type. | Overrides EntityTypeEventSubscriberTrait::onEntityTypeDelete | |
EntitySchemaSubscriber::onEntityTypeUpdate | public | function | Reacts to the update of the entity type. | Overrides EntityTypeEventSubscriberTrait::onEntityTypeUpdate | |
EntitySchemaSubscriber::onFieldableEntityTypeCreate | public | function | Reacts to the creation of the fieldable entity type. | Overrides EntityTypeEventSubscriberTrait::onFieldableEntityTypeCreate | |
EntitySchemaSubscriber::onFieldableEntityTypeUpdate | public | function | Reacts to the update of a fieldable entity type. | Overrides EntityTypeEventSubscriberTrait::onFieldableEntityTypeUpdate | |
EntitySchemaSubscriber::__construct | public | function | Constructs a new EntitySchemaSubscriber. | ||
EntityTypeEventSubscriberTrait::getEntityTypeEvents | public static | function | Gets the subscribed events. | ||
EntityTypeEventSubscriberTrait::onEntityTypeEvent | public | function | Listener method for any entity type definition event. | ||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.