class EntitySchemaSubscriber in Drupal 10
Same name in this branch
- 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
Same name and namespace in other branches
- 8 core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber
- 9 core/modules/workspaces/src/EventSubscriber/EntitySchemaSubscriber.php \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber
Defines a class for listening to entity schema changes.
Hierarchy
- class \Drupal\workspaces\EventSubscriber\EntitySchemaSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface, EntityTypeListenerInterface uses EntityTypeEventSubscriberTrait, 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 manager.
*
* @var \Drupal\workspaces\WorkspaceManagerInterface
*/
protected $workspaceManager;
/**
* Constructs a new EntitySchemaSubscriber.
*
* @param \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager
* Definition update manager.
* @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entityLastInstalledSchemaRepository
* Last definitions.
* @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
* The workspace manager.
*/
public function __construct(EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager, EntityLastInstalledSchemaRepositoryInterface $entityLastInstalledSchemaRepository, WorkspaceManagerInterface $workspace_manager) {
$this->entityDefinitionUpdateManager = $entityDefinitionUpdateManager;
$this->entityLastInstalledSchemaRepository = $entityLastInstalledSchemaRepository;
$this->workspaceManager = $workspace_manager;
}
/**
* {@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->workspaceManager
->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->workspaceManager
->isEntityTypeSupported($entity_type) && !$this->workspaceManager
->isEntityTypeSupported($original)) {
$this
->addRevisionMetadataField($entity_type);
}
// If the entity type is no longer supported by Workspaces, remove the
// revision metadata field.
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);
// 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntitySchemaSubscriber:: |
protected | property | The definition update manager. | |
EntitySchemaSubscriber:: |
protected | property | The last installed schema definitions. | |
EntitySchemaSubscriber:: |
protected | property | The workspace manager. | |
EntitySchemaSubscriber:: |
protected | function | Adds the 'workspace' revision metadata field to an entity type. | |
EntitySchemaSubscriber:: |
public static | function | ||
EntitySchemaSubscriber:: |
protected | function | Gets the base field definition for the 'workspace' revision metadata field. | |
EntitySchemaSubscriber:: |
public | function |
Reacts to the creation of the entity type. Overrides EntityTypeEventSubscriberTrait:: |
|
EntitySchemaSubscriber:: |
public | function |
Reacts to the deletion of the entity type. Overrides EntityTypeEventSubscriberTrait:: |
|
EntitySchemaSubscriber:: |
public | function |
Reacts to the update of the entity type. Overrides EntityTypeEventSubscriberTrait:: |
|
EntitySchemaSubscriber:: |
public | function |
Reacts to the creation of the fieldable entity type. Overrides EntityTypeEventSubscriberTrait:: |
|
EntitySchemaSubscriber:: |
public | function |
Reacts to the update of a fieldable entity type. Overrides EntityTypeEventSubscriberTrait:: |
|
EntitySchemaSubscriber:: |
public | function | Constructs a new EntitySchemaSubscriber. | |
EntityTypeEventSubscriberTrait:: |
public static | function | Gets the subscribed events. | |
EntityTypeEventSubscriberTrait:: |
public | function | Listener method for any entity type definition event. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 3 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 1 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |