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\StringTranslation\StringTranslationTrait;
class EntityDefinitionUpdateManager implements EntityDefinitionUpdateManagerInterface {
use StringTranslationTrait;
protected $entityManager;
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
public function needsUpdates() {
return (bool) $this
->getChangeList();
}
public function getChangeSummary() {
$summary = array();
foreach ($this
->getChangeList() as $entity_type_id => $change_list) {
if (!empty($change_list['entity_type'])) {
$entity_type = $this->entityManager
->getDefinition($entity_type_id);
$t_args = array(
'%entity_type' => $entity_type
->getLabel(),
);
switch ($change_list['entity_type']) {
case static::DEFINITION_CREATED:
$summary[$entity_type_id][] = $this
->t('Create the %entity_type entity type.', $t_args);
break;
case static::DEFINITION_UPDATED:
$summary[$entity_type_id][] = $this
->t('Update the %entity_type entity type.', $t_args);
break;
}
}
if (!empty($change_list['field_storage_definitions'])) {
$storage_definitions = $this->entityManager
->getFieldStorageDefinitions($entity_type_id);
$original_storage_definitions = $this->entityManager
->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('Create the %field_name field.', array(
'%field_name' => $storage_definitions[$field_name]
->getLabel(),
));
break;
case static::DEFINITION_UPDATED:
$summary[$entity_type_id][] = $this
->t('Update the %field_name field.', array(
'%field_name' => $storage_definitions[$field_name]
->getLabel(),
));
break;
case static::DEFINITION_DELETED:
$summary[$entity_type_id][] = $this
->t('Delete the %field_name field.', array(
'%field_name' => $original_storage_definitions[$field_name]
->getLabel(),
));
break;
}
}
}
}
return $summary;
}
public function applyUpdates() {
$complete_change_list = $this
->getChangeList();
if ($complete_change_list) {
$this->entityManager
->clearCachedDefinitions();
}
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->entityManager
->getFieldStorageDefinitions($entity_type_id);
$original_storage_definitions = $this->entityManager
->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);
}
}
}
}
public function getEntityType($entity_type_id) {
$entity_type = $this->entityManager
->getLastInstalledDefinition($entity_type_id);
return $entity_type ? clone $entity_type : NULL;
}
public function installEntityType(EntityTypeInterface $entity_type) {
$this->entityManager
->clearCachedDefinitions();
$this->entityManager
->onEntityTypeCreate($entity_type);
}
public function updateEntityType(EntityTypeInterface $entity_type) {
$original = $this
->getEntityType($entity_type
->id());
$this->entityManager
->clearCachedDefinitions();
$this->entityManager
->onEntityTypeUpdate($entity_type, $original);
}
public function uninstallEntityType(EntityTypeInterface $entity_type) {
$this->entityManager
->clearCachedDefinitions();
$this->entityManager
->onEntityTypeDelete($entity_type);
}
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->entityManager
->clearCachedDefinitions();
$this->entityManager
->onFieldStorageDefinitionCreate($storage_definition);
}
public function getFieldStorageDefinition($name, $entity_type_id) {
$storage_definitions = $this->entityManager
->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->entityManager
->clearCachedDefinitions();
$this->entityManager
->onFieldStorageDefinitionUpdate($storage_definition, $original);
}
public function uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
$this->entityManager
->clearCachedDefinitions();
$this->entityManager
->onFieldStorageDefinitionDelete($storage_definition);
}
protected function doEntityUpdate($op, $entity_type_id) {
$entity_type = $this->entityManager
->getDefinition($entity_type_id);
switch ($op) {
case static::DEFINITION_CREATED:
$this->entityManager
->onEntityTypeCreate($entity_type);
break;
case static::DEFINITION_UPDATED:
$original = $this->entityManager
->getLastInstalledDefinition($entity_type_id);
$this->entityManager
->onEntityTypeUpdate($entity_type, $original);
break;
}
}
protected function doFieldUpdate($op, $storage_definition = NULL, $original_storage_definition = NULL) {
switch ($op) {
case static::DEFINITION_CREATED:
$this->entityManager
->onFieldStorageDefinitionCreate($storage_definition);
break;
case static::DEFINITION_UPDATED:
$this->entityManager
->onFieldStorageDefinitionUpdate($storage_definition, $original_storage_definition);
break;
case static::DEFINITION_DELETED:
$this->entityManager
->onFieldStorageDefinitionDelete($original_storage_definition);
break;
}
}
protected function getChangeList() {
$this->entityManager
->useCaches(FALSE);
$change_list = array();
foreach ($this->entityManager
->getDefinitions() as $entity_type_id => $entity_type) {
$original = $this->entityManager
->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->entityManager
->getStorage($entity_type_id) instanceof DynamicallyFieldableEntityStorageInterface) {
$field_changes = array();
$storage_definitions = $this->entityManager
->getFieldStorageDefinitions($entity_type_id);
$original_storage_definitions = $this->entityManager
->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->entityManager
->useCaches(TRUE);
return array_filter($change_list);
}
protected function requiresEntityStorageSchemaChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
$storage = $this->entityManager
->getStorage($entity_type
->id());
return $storage instanceof EntityStorageSchemaInterface && $storage
->requiresEntityStorageSchemaChanges($entity_type, $original);
}
protected function requiresFieldStorageSchemaChanges(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
$storage = $this->entityManager
->getStorage($storage_definition
->getTargetEntityTypeId());
return $storage instanceof DynamicallyFieldableEntityStorageSchemaInterface && $storage
->requiresFieldStorageSchemaChanges($storage_definition, $original);
}
}