View source
<?php
namespace Drupal\Core\Entity;
use Drupal\Core\Entity\Schema\DynamicallyFieldableEntityStorageSchemaInterface;
use Drupal\Core\Entity\Schema\EntityStorageSchemaInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionListenerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class EntityDefinitionUpdateManager implements EntityDefinitionUpdateManagerInterface {
use StringTranslationTrait;
protected $entityFieldManager;
protected $entityTypeListener;
protected $entityTypeManager;
protected $fieldStorageDefinitionListener;
protected $entityLastInstalledSchemaRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, EntityFieldManagerInterface $entity_field_manager, EntityTypeListenerInterface $entity_type_listener, FieldStorageDefinitionListenerInterface $field_storage_definition_listener) {
$this->entityTypeManager = $entity_type_manager;
$this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeListener = $entity_type_listener;
$this->fieldStorageDefinitionListener = $field_storage_definition_listener;
}
public function needsUpdates() {
return (bool) $this
->getChangeList();
}
public function getChangeSummary() {
$summary = [];
foreach ($this
->getChangeList() as $entity_type_id => $change_list) {
if (!empty($change_list['entity_type'])) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
switch ($change_list['entity_type']) {
case static::DEFINITION_CREATED:
$summary[$entity_type_id][] = $this
->t('The %entity_type entity type needs to be installed.', [
'%entity_type' => $entity_type
->getLabel(),
]);
break;
case static::DEFINITION_UPDATED:
$summary[$entity_type_id][] = $this
->t('The %entity_type entity type needs to be updated.', [
'%entity_type' => $entity_type
->getLabel(),
]);
break;
}
}
if (!empty($change_list['field_storage_definitions'])) {
$storage_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($entity_type_id);
$original_storage_definitions = $this->entityLastInstalledSchemaRepository
->getLastInstalledFieldStorageDefinitions($entity_type_id);
foreach ($change_list['field_storage_definitions'] as $field_name => $change) {
switch ($change) {
case static::DEFINITION_CREATED:
$summary[$entity_type_id][] = $this
->t('The %field_name field needs to be installed.', [
'%field_name' => $storage_definitions[$field_name]
->getLabel() ?: $field_name,
]);
break;
case static::DEFINITION_UPDATED:
$summary[$entity_type_id][] = $this
->t('The %field_name field needs to be updated.', [
'%field_name' => $storage_definitions[$field_name]
->getLabel() ?: $field_name,
]);
break;
case static::DEFINITION_DELETED:
$summary[$entity_type_id][] = $this
->t('The %field_name field needs to be uninstalled.', [
'%field_name' => $original_storage_definitions[$field_name]
->getLabel() ?: $field_name,
]);
break;
}
}
}
}
return $summary;
}
public function getEntityType($entity_type_id) {
$entity_type = $this->entityLastInstalledSchemaRepository
->getLastInstalledDefinition($entity_type_id);
return $entity_type ? clone $entity_type : NULL;
}
public function getEntityTypes() {
return $this->entityLastInstalledSchemaRepository
->getLastInstalledDefinitions();
}
public function installEntityType(EntityTypeInterface $entity_type) {
$this
->clearCachedDefinitions();
$this->entityTypeListener
->onEntityTypeCreate($entity_type);
}
public function updateEntityType(EntityTypeInterface $entity_type) {
$original = $this
->getEntityType($entity_type
->id());
$this
->clearCachedDefinitions();
$this->entityTypeListener
->onEntityTypeUpdate($entity_type, $original);
}
public function uninstallEntityType(EntityTypeInterface $entity_type) {
$this
->clearCachedDefinitions();
$this->entityTypeListener
->onEntityTypeDelete($entity_type);
}
public function installFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions) {
$this
->clearCachedDefinitions();
foreach ($field_storage_definitions as $name => $field_storage_definition) {
if ($field_storage_definition instanceof BaseFieldDefinition) {
$field_storage_definition
->setName($name)
->setTargetEntityTypeId($entity_type
->id())
->setProvider($entity_type
->getProvider())
->setTargetBundle(NULL);
}
}
$this->entityTypeListener
->onFieldableEntityTypeCreate($entity_type, $field_storage_definitions);
}
public function updateFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions, array &$sandbox = NULL) {
$original = $this
->getEntityType($entity_type
->id());
if ($this
->requiresEntityDataMigration($entity_type, $original) && $sandbox === NULL) {
throw new \InvalidArgumentException('The entity schema update for the ' . $entity_type
->id() . ' entity type requires a data migration.');
}
$original_field_storage_definitions = $this->entityLastInstalledSchemaRepository
->getLastInstalledFieldStorageDefinitions($entity_type
->id());
$this->entityTypeListener
->onFieldableEntityTypeUpdate($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox);
$this
->clearCachedDefinitions();
}
public function installFieldStorageDefinition($name, $entity_type_id, $provider, FieldStorageDefinitionInterface $storage_definition) {
if ($storage_definition instanceof BaseFieldDefinition) {
$storage_definition
->setName($name)
->setTargetEntityTypeId($entity_type_id)
->setProvider($provider)
->setTargetBundle(NULL);
}
$this
->clearCachedDefinitions();
$this->fieldStorageDefinitionListener
->onFieldStorageDefinitionCreate($storage_definition);
}
public function getFieldStorageDefinition($name, $entity_type_id) {
$storage_definitions = $this->entityLastInstalledSchemaRepository
->getLastInstalledFieldStorageDefinitions($entity_type_id);
return isset($storage_definitions[$name]) ? clone $storage_definitions[$name] : NULL;
}
public function updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
$original = $this
->getFieldStorageDefinition($storage_definition
->getName(), $storage_definition
->getTargetEntityTypeId());
$this
->clearCachedDefinitions();
$this->fieldStorageDefinitionListener
->onFieldStorageDefinitionUpdate($storage_definition, $original);
}
public function uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
$this
->clearCachedDefinitions();
$this->fieldStorageDefinitionListener
->onFieldStorageDefinitionDelete($storage_definition);
}
public function getChangeList() {
$this->entityTypeManager
->useCaches(FALSE);
$this->entityFieldManager
->useCaches(FALSE);
$change_list = [];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
$original = $this->entityLastInstalledSchemaRepository
->getLastInstalledDefinition($entity_type_id);
if (!$original) {
$change_list[$entity_type_id]['entity_type'] = static::DEFINITION_CREATED;
}
else {
if ($this
->requiresEntityStorageSchemaChanges($entity_type, $original)) {
$change_list[$entity_type_id]['entity_type'] = static::DEFINITION_UPDATED;
}
if ($this->entityTypeManager
->getStorage($entity_type_id) instanceof DynamicallyFieldableEntityStorageInterface) {
$field_changes = [];
$storage_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($entity_type_id);
$original_storage_definitions = $this->entityLastInstalledSchemaRepository
->getLastInstalledFieldStorageDefinitions($entity_type_id);
foreach (array_diff_key($storage_definitions, $original_storage_definitions) as $field_name => $storage_definition) {
$field_changes[$field_name] = static::DEFINITION_CREATED;
}
foreach (array_diff_key($original_storage_definitions, $storage_definitions) as $field_name => $original_storage_definition) {
$field_changes[$field_name] = static::DEFINITION_DELETED;
}
foreach (array_intersect_key($storage_definitions, $original_storage_definitions) as $field_name => $storage_definition) {
if (!isset($change_list[$entity_type_id]['entity_type']) && $this
->requiresFieldStorageSchemaChanges($storage_definition, $original_storage_definitions[$field_name])) {
$field_changes[$field_name] = static::DEFINITION_UPDATED;
}
}
if ($field_changes) {
$change_list[$entity_type_id]['field_storage_definitions'] = $field_changes;
}
}
}
}
$this->entityTypeManager
->useCaches(TRUE);
$this->entityFieldManager
->useCaches(TRUE);
return array_filter($change_list);
}
protected function requiresEntityStorageSchemaChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
$storage = $this->entityTypeManager
->getStorage($entity_type
->id());
return $storage instanceof EntityStorageSchemaInterface && $storage
->requiresEntityStorageSchemaChanges($entity_type, $original);
}
protected function requiresFieldStorageSchemaChanges(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
$storage = $this->entityTypeManager
->getStorage($storage_definition
->getTargetEntityTypeId());
return $storage instanceof DynamicallyFieldableEntityStorageSchemaInterface && $storage
->requiresFieldStorageSchemaChanges($storage_definition, $original);
}
protected function requiresEntityDataMigration(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
$storage = $this->entityTypeManager
->getStorage($entity_type
->id());
return $storage instanceof EntityStorageSchemaInterface && $storage
->requiresEntityDataMigration($entity_type, $original);
}
protected function clearCachedDefinitions() {
$this->entityTypeManager
->clearCachedDefinitions();
$this->entityFieldManager
->clearCachedFieldDefinitions();
}
}