DevelEntityDefinitionUpdateManager.php in Devel Entity Updates 3.x
File
src/DevelEntityDefinitionUpdateManager.php
View source
<?php
namespace Drupal\devel_entity_updates;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
use Drupal\Core\Entity\EntityTypeListenerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Schema\EntityStorageSchemaInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionListenerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class DevelEntityDefinitionUpdateManager implements ContainerInjectionInterface {
private $entityDefinitionUpdateManager;
private $entityLastInstalledSchemaRepository;
private $entityTypeManager;
private $entityTypeListener;
private $entityFieldManager;
private $fieldStorageDefinitionListener;
public function __construct(EntityDefinitionUpdateManagerInterface $entity_definition_update_manager, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, EntityTypeManagerInterface $entity_type_manager, EntityTypeListenerInterface $entity_type_listener, EntityFieldManagerInterface $entity_field_manager, FieldStorageDefinitionListenerInterface $field_storage_definition_listener) {
$this->entityDefinitionUpdateManager = $entity_definition_update_manager;
$this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeListener = $entity_type_listener;
$this->entityFieldManager = $entity_field_manager;
$this->fieldStorageDefinitionListener = $field_storage_definition_listener;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.definition_update_manager'), $container
->get('entity.last_installed_schema.repository'), $container
->get('entity_type.manager'), $container
->get('entity_type.listener'), $container
->get('entity_field.manager'), $container
->get('field_storage_definition.listener'));
}
public function applyUpdates() {
$reflector = new \ReflectionMethod($this->entityDefinitionUpdateManager, 'getChangeList');
$reflector
->setAccessible(TRUE);
$complete_change_list = $reflector
->invoke($this->entityDefinitionUpdateManager);
if ($complete_change_list) {
$this->entityTypeManager
->clearCachedDefinitions();
$this->entityFieldManager
->clearCachedFieldDefinitions();
}
foreach ($complete_change_list as $entity_type_id => $change_list) {
if (!empty($change_list['entity_type'])) {
$this
->doEntityUpdate($change_list['entity_type'], $entity_type_id);
}
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) {
$storage_definition = isset($storage_definitions[$field_name]) ? $storage_definitions[$field_name] : NULL;
$original_storage_definition = isset($original_storage_definitions[$field_name]) ? $original_storage_definitions[$field_name] : NULL;
$this
->doFieldUpdate($change, $storage_definition, $original_storage_definition);
}
}
}
}
private function doEntityUpdate($op, $entity_type_id) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
switch ($op) {
case EntityDefinitionUpdateManagerInterface::DEFINITION_CREATED:
$this->entityTypeListener
->onEntityTypeCreate($entity_type);
break;
case EntityDefinitionUpdateManagerInterface::DEFINITION_UPDATED:
$original = $this->entityLastInstalledSchemaRepository
->getLastInstalledDefinition($entity_type_id);
$storage = $this->entityTypeManager
->getStorage($entity_type
->id());
if ($storage instanceof EntityStorageSchemaInterface && $storage
->requiresEntityDataMigration($entity_type, $original)) {
throw new \InvalidArgumentException('The entity schema update for the ' . $entity_type
->id() . ' entity type requires a data migration.');
}
$field_storage_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($entity_type_id);
$original_field_Storage_definitions = $this->entityLastInstalledSchemaRepository
->getLastInstalledFieldStorageDefinitions($entity_type_id);
$this->entityTypeListener
->onFieldableEntityTypeUpdate($entity_type, $original, $field_storage_definitions, $original_field_Storage_definitions);
break;
}
}
private function doFieldUpdate($op, FieldStorageDefinitionInterface $storage_definition = NULL, FieldStorageDefinitionInterface $original_storage_definition = NULL) {
switch ($op) {
case EntityDefinitionUpdateManagerInterface::DEFINITION_CREATED:
$this->fieldStorageDefinitionListener
->onFieldStorageDefinitionCreate($storage_definition);
break;
case EntityDefinitionUpdateManagerInterface::DEFINITION_UPDATED:
if ($storage_definition && $original_storage_definition) {
$this->fieldStorageDefinitionListener
->onFieldStorageDefinitionUpdate($storage_definition, $original_storage_definition);
}
break;
case EntityDefinitionUpdateManagerInterface::DEFINITION_DELETED:
if ($original_storage_definition) {
$this->fieldStorageDefinitionListener
->onFieldStorageDefinitionDelete($original_storage_definition);
}
break;
}
}
}