Commit 23e9d7f1 authored by alexpott's avatar alexpott

Issue #2341323 by dawehner, plach: Adapt the references field / table names in...

Issue #2341323 by dawehner, plach: Adapt the references field / table names in views, when corresponding entity schema changes
parent ea568f1d
......@@ -130,10 +130,30 @@ public function requiresEntityStorageSchemaChanges(EntityTypeInterface $entity_t
$entity_type->getStorageClass() != $original->getStorageClass() ||
$entity_type->isRevisionable() != $original->isRevisionable() ||
$entity_type->isTranslatable() != $original->isTranslatable() ||
$this->hasSharedTableNameChanges($entity_type, $original) ||
// Detect changes in key or index definitions.
$this->getEntitySchemaData($entity_type, $this->getEntitySchema($entity_type, TRUE)) != $this->loadEntitySchemaData($original);
}
/**
* Detects whether any table name got renamed in an entity type update.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The new entity type.
* @param \Drupal\Core\Entity\EntityTypeInterface $original
* The origin entity type.
*
* @return bool
* Returns TRUE if there have been changes.
*/
protected function hasSharedTableNameChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
return
$entity_type->getBaseTable() != $original->getBaseTable() ||
$entity_type->getDataTable() != $original->getDataTable() ||
$entity_type->getRevisionTable() != $original->getRevisionTable() ||
$entity_type->getRevisionDataTable() != $original->getRevisionDataTable();
}
/**
* {@inheritdoc}
*/
......
<?php
/**
* @file
* Contains \Drupal\system\Tests\Entity\EntityDefinitionTestTrait.
*/
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_test\FieldStorageDefinition;
/**
* Provides some test methods used to update existing entity definitions.
*/
trait EntityDefinitionTestTrait {
/**
* Resets the entity type definition.
*/
protected function resetEntityType() {
$this->state->set('entity_test_update.entity_type', NULL);
$this->entityManager->clearCachedDefinitions();
$this->entityDefinitionUpdateManager->applyUpdates();
}
/**
* Updates the 'entity_test_update' entity type to revisionable.
*/
protected function updateEntityTypeToRevisionable() {
$entity_type = clone $this->entityManager->getDefinition('entity_test_update');
$keys = $entity_type->getKeys();
$keys['revision'] = 'revision_id';
$entity_type->set('entity_keys', $keys);
$this->state->set('entity_test_update.entity_type', $entity_type);
$this->entityManager->clearCachedDefinitions();
}
/**
* Updates the 'entity_test_update' entity type not revisionable.
*/
protected function updateEntityTypeToNotRevisionable() {
$entity_type = clone $this->entityManager->getDefinition('entity_test_update');
$keys = $entity_type->getKeys();
unset($keys['revision']);
$entity_type->set('entity_keys', $keys);
$this->state->set('entity_test_update.entity_type', $entity_type);
$this->entityManager->clearCachedDefinitions();
}
/**
* Updates the 'entity_test_update' entity type to translatable.
*/
protected function updateEntityTypeToTranslatable() {
$entity_type = clone $this->entityManager->getDefinition('entity_test_update');
$entity_type->set('translatable', TRUE);
$entity_type->set('data_table', 'entity_test_update_data');
if ($entity_type->isRevisionable()) {
$entity_type->set('revision_data_table', 'entity_test_update_revision_data');
}
$this->state->set('entity_test_update.entity_type', $entity_type);
$this->entityManager->clearCachedDefinitions();
}
/**
* Updates the 'entity_test_update' entity type to not translatable.
*/
protected function updateEntityTypeToNotTranslatable() {
$entity_type = clone $this->entityManager->getDefinition('entity_test_update');
$entity_type->set('translatable', FALSE);
$entity_type->set('data_table', NULL);
if ($entity_type->isRevisionable()) {
$entity_type->set('revision_data_table', NULL);
}
$this->state->set('entity_test_update.entity_type', $entity_type);
$this->entityManager->clearCachedDefinitions();
}
/**
* Adds a new base field to the 'entity_test_update' entity type.
*
* @param string $type
* (optional) The field type for the new field. Defaults to 'string'.
*/
protected function addBaseField($type = 'string') {
$definitions['new_base_field'] = BaseFieldDefinition::create($type)
->setName('new_base_field')
->setLabel(t('A new base field'));
$this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
$this->entityManager->clearCachedDefinitions();
}
/**
* Modifies the new base field from 'string' to 'text'.
*/
protected function modifyBaseField() {
$this->addBaseField('text');
}
/**
* Removes the new base field from the 'entity_test_update' entity type.
*/
protected function removeBaseField() {
$this->state->delete('entity_test_update.additional_base_field_definitions');
$this->entityManager->clearCachedDefinitions();
}
/**
* Adds a single-field index to the base field.
*/
protected function addBaseFieldIndex() {
$this->state->set('entity_test_update.additional_field_index.entity_test_update.new_base_field', TRUE);
$this->entityManager->clearCachedDefinitions();
}
/**
* Removes the index added in addBaseFieldIndex().
*/
protected function removeBaseFieldIndex() {
$this->state->delete('entity_test_update.additional_field_index.entity_test_update.new_base_field');
$this->entityManager->clearCachedDefinitions();
}
/**
* Adds a new bundle field to the 'entity_test_update' entity type.
*
* @param string $type
* (optional) The field type for the new field. Defaults to 'string'.
*/
protected function addBundleField($type = 'string') {
$definitions['new_bundle_field'] = FieldStorageDefinition::create($type)
->setName('new_bundle_field')
->setLabel(t('A new bundle field'))
->setTargetEntityTypeId('entity_test_update');
$this->state->set('entity_test_update.additional_field_storage_definitions', $definitions);
$this->state->set('entity_test_update.additional_bundle_field_definitions.test_bundle', $definitions);
$this->entityManager->clearCachedDefinitions();
}
/**
* Modifies the new bundle field from 'string' to 'text'.
*/
protected function modifyBundleField() {
$this->addBundleField('text');
}
/**
* Removes the new bundle field from the 'entity_test_update' entity type.
*/
protected function removeBundleField() {
$this->state->delete('entity_test_update.additional_field_storage_definitions');
$this->state->delete('entity_test_update.additional_bundle_field_definitions.test_bundle');
$this->entityManager->clearCachedDefinitions();
}
/**
* Adds an index to the 'entity_test_update' entity type's base table.
*
* @see \Drupal\entity_test\EntityTestStorageSchema::getEntitySchema().
*/
protected function addEntityIndex() {
$indexes = array(
'entity_test_update__new_index' => array('name', 'user_id'),
);
$this->state->set('entity_test_update.additional_entity_indexes', $indexes);
}
/**
* Removes the index added in addEntityIndex().
*/
protected function removeEntityIndex() {
$this->state->delete('entity_test_update.additional_entity_indexes');
}
/**
* Renames the base table to 'entity_test_update_new'.
*/
protected function renameBaseTable() {
$entity_type = clone $this->entityManager->getDefinition('entity_test_update');
$entity_type->set('base_table', 'entity_test_update_new');
$this->state->set('entity_test_update.entity_type', $entity_type);
$this->entityManager->clearCachedDefinitions();
}
/**
* Renames the data table to 'entity_test_update_data_new'.
*/
protected function renameDataTable() {
$entity_type = clone $this->entityManager->getDefinition('entity_test_update');
$entity_type->set('data_table', 'entity_test_update_data_new');
$this->state->set('entity_test_update.entity_type', $entity_type);
$this->entityManager->clearCachedDefinitions();
}
/**
* Renames the revision table to 'entity_test_update_revision_new'.
*/
protected function renameRevisionBaseTable() {
$entity_type = clone $this->entityManager->getDefinition('entity_test_update');
$entity_type->set('revision_table', 'entity_test_update_revision_new');
$this->state->set('entity_test_update.entity_type', $entity_type);
$this->entityManager->clearCachedDefinitions();
}
/**
* Renames the revision data table to 'entity_test_update_revision_data_new'.
*/
protected function renameRevisionDataTable() {
$entity_type = clone $this->entityManager->getDefinition('entity_test_update');
$entity_type->set('revision_data_table', 'entity_test_update_revision_data_new');
$this->state->set('entity_test_update.entity_type', $entity_type);
$this->entityManager->clearCachedDefinitions();
}
/**
* Removes the entity type.
*/
protected function deleteEntityType() {
$this->state->set('entity_test_update.entity_type', 'null');
$this->entityManager->clearCachedDefinitions();
}
}
......@@ -23,6 +23,8 @@
*/
class EntityDefinitionUpdateTest extends EntityUnitTestBase {
use EntityDefinitionTestTrait;
/**
* The entity definition update manager.
*
......@@ -557,112 +559,4 @@ public function testEntityTypeSchemaUpdateAndBaseFieldCreateWithoutData() {
}
}
/**
* Updates the 'entity_test_update' entity type to revisionable.
*/
protected function updateEntityTypeToRevisionable() {
$entity_type = clone $this->entityManager->getDefinition('entity_test_update');
$keys = $entity_type->getKeys();
$keys['revision'] = 'revision_id';
$entity_type->set('entity_keys', $keys);
$this->state->set('entity_test_update.entity_type', $entity_type);
$this->entityManager->clearCachedDefinitions();
}
/**
* Adds a new base field to the 'entity_test_update' entity type.
*
* @param string $type
* (optional) The field type for the new field. Defaults to 'string'.
*/
protected function addBaseField($type = 'string') {
$definitions['new_base_field'] = BaseFieldDefinition::create($type)
->setName('new_base_field')
->setLabel(t('A new base field'));
$this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
$this->entityManager->clearCachedDefinitions();
}
/**
* Modifies the new base field from 'string' to 'text'.
*/
protected function modifyBaseField() {
$this->addBaseField('text');
}
/**
* Removes the new base field from the 'entity_test_update' entity type.
*/
protected function removeBaseField() {
$this->state->delete('entity_test_update.additional_base_field_definitions');
$this->entityManager->clearCachedDefinitions();
}
/**
* Adds a single-field index to the base field.
*/
protected function addBaseFieldIndex() {
$this->state->set('entity_test_update.additional_field_index.entity_test_update.new_base_field', TRUE);
$this->entityManager->clearCachedDefinitions();
}
/**
* Removes the index added in addBaseFieldIndex().
*/
protected function removeBaseFieldIndex() {
$this->state->delete('entity_test_update.additional_field_index.entity_test_update.new_base_field');
$this->entityManager->clearCachedDefinitions();
}
/**
* Adds a new bundle field to the 'entity_test_update' entity type.
*
* @param string $type
* (optional) The field type for the new field. Defaults to 'string'.
*/
protected function addBundleField($type = 'string') {
$definitions['new_bundle_field'] = FieldStorageDefinition::create($type)
->setName('new_bundle_field')
->setLabel(t('A new bundle field'))
->setTargetEntityTypeId('entity_test_update');
$this->state->set('entity_test_update.additional_field_storage_definitions', $definitions);
$this->state->set('entity_test_update.additional_bundle_field_definitions.test_bundle', $definitions);
$this->entityManager->clearCachedDefinitions();
}
/**
* Modifies the new bundle field from 'string' to 'text'.
*/
protected function modifyBundleField() {
$this->addBundleField('text');
}
/**
* Removes the new bundle field from the 'entity_test_update' entity type.
*/
protected function removeBundleField() {
$this->state->delete('entity_test_update.additional_field_storage_definitions');
$this->state->delete('entity_test_update.additional_bundle_field_definitions.test_bundle');
$this->entityManager->clearCachedDefinitions();
}
/**
* Adds an index to the 'entity_test_update' entity type's base table.
*
* @see \Drupal\entity_test\EntityTestStorageSchema::getEntitySchema().
*/
protected function addEntityIndex() {
$indexes = array(
'entity_test_update__new_index' => array('name', 'user_id'),
);
$this->state->set('entity_test_update.additional_entity_indexes', $indexes);
}
/**
* Removes the index added in addEntityIndex().
*/
protected function removeEntityIndex() {
$this->state->delete('entity_test_update.additional_entity_indexes');
}
}
......@@ -29,7 +29,8 @@
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type",
* "label" = "name"
* "label" = "name",
* "langcode" = "langcode",
* }
* )
*/
......
......@@ -21,7 +21,10 @@ class EntityTestStorageSchema extends SqlContentEntityStorageSchema {
*/
protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
$schema = parent::getEntitySchema($entity_type, $reset);
$schema['entity_test_update']['indexes'] += \Drupal::state()->get('entity_test_update.additional_entity_indexes', array());
if ($entity_type->id() == 'entity_test_update') {
$schema[$entity_type->getBaseTable()]['indexes'] += \Drupal::state()->get('entity_test_update.additional_entity_indexes', array());
}
return $schema;
}
......
<?php
/**
* @file
* Contains \Drupal\views\EventSubscriber\ViewsEntitySchemaSubscriber.
*/
namespace Drupal\views\EventSubscriber;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeEventSubscriberTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeListenerInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\views\Views;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Reacts to changes on entity types to update all views entities.
*/
class ViewsEntitySchemaSubscriber implements EntityTypeListenerInterface, EventSubscriberInterface {
use EntityTypeEventSubscriberTrait;
/**
* Indicates that a base table got renamed.
*/
const BASE_TABLE_RENAME = 0;
/**
* Indicates that a data table got renamed.
*/
const DATA_TABLE_RENAME = 1;
/**
* Indicates that a data table got added.
*/
const DATA_TABLE_ADDITION = 2;
/**
* Indicates that a data table got removed.
*/
const DATA_TABLE_REMOVAL = 3;
/**
* Indicates that a revision table got renamed.
*/
const REVISION_TABLE_RENAME = 4;
/**
* Indicates that a revision table got added.
*/
const REVISION_TABLE_ADDITION = 5;
/**
* Indicates that a revision table got removed.
*/
const REVISION_TABLE_REMOVAL = 6;
/**
* Indicates that a revision data table got renamed.
*/
const REVISION_DATA_TABLE_RENAME = 7;
/**
* Indicates that a revision data table got added.
*/
const REVISION_DATA_TABLE_ADDITION = 8;
/**
* Indicates that a revision data table got removed.
*/
const REVISION_DATA_TABLE_REMOVAL = 9;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Constructs a ViewsEntitySchemaSubscriber.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return static::getEntityTypeEvents();
}
/**
* {@inheritdoc}
*/
public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
}
/**
* {@inheritdoc}
*/
public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
$changes = [];
// We implement a specific logic for table updates, which is bound to the
// default sql content entity storage.
if (!$this->entityManager->getStorage($entity_type->id()) instanceof SqlContentEntityStorage) {
return;
}
if ($entity_type->getBaseTable() != $original->getBaseTable()) {
$changes[] = static::BASE_TABLE_RENAME;
}
$revision_add = $entity_type->isRevisionable() && !$original->isRevisionable();
$revision_remove = !$entity_type->isRevisionable() && $original->isRevisionable();
$translation_add = $entity_type->isTranslatable() && !$original->isTranslatable();
$translation_remove = !$entity_type->isTranslatable() && $original->isTranslatable();
if ($revision_add) {
$changes[] = static::REVISION_TABLE_ADDITION;
}
elseif ($revision_remove) {
$changes[] = static::REVISION_TABLE_REMOVAL;
}
elseif ($entity_type->isRevisionable() && $entity_type->getRevisionTable() != $original->getRevisionTable()) {
$changes[] = static::REVISION_TABLE_RENAME;
}
if ($translation_add) {
$changes[] = static::DATA_TABLE_ADDITION;
}
elseif ($translation_remove) {
$changes[] = static::DATA_TABLE_REMOVAL;
}
elseif ($entity_type->isTranslatable() && $entity_type->getDataTable() != $original->getDataTable()) {
$changes[] = static::DATA_TABLE_RENAME;
}
if ($entity_type->isRevisionable() && $entity_type->isTranslatable()) {
if ($revision_add || $translation_add) {
$changes[] = static::REVISION_DATA_TABLE_ADDITION;
}
elseif ($entity_type->getRevisionDataTable() != $original->getRevisionDataTable()) {
$changes[] = static::REVISION_DATA_TABLE_RENAME;
}
}
elseif ($original->isRevisionable() && $original->isTranslatable() && ($revision_remove || $translation_remove)) {
$changes[] = static::REVISION_DATA_TABLE_REMOVAL;
}
/** @var \Drupal\views\Entity\View[] $all_views */
$all_views = $this->entityManager->getStorage('view')->loadMultiple(NULL);
foreach ($changes as $change) {
switch ($change) {
case static::BASE_TABLE_RENAME:
$this->baseTableRename($all_views, $entity_type->id(), $original->getBaseTable(), $entity_type->getBaseTable());
break;
case static::DATA_TABLE_RENAME:
$this->dataTableRename($all_views, $entity_type->id(), $original->getDataTable(), $entity_type->getDataTable());
break;
case static::DATA_TABLE_ADDITION:
$this->dataTableAddition($all_views, $entity_type, $entity_type->getDataTable(), $entity_type->getBaseTable());
break;
case static::DATA_TABLE_REMOVAL:
$this->dataTableRemoval($all_views, $entity_type->id(), $original->getDataTable(), $entity_type->getBaseTable());
break;
case static::REVISION_TABLE_RENAME:
$this->baseTableRename($all_views, $entity_type->id(), $original->getRevisionTable(), $entity_type->getRevisionTable());
break;
case static::REVISION_TABLE_ADDITION:
// If we add revision support we don't have to do anything.
break;
case static::REVISION_TABLE_REMOVAL:
$this->revisionRemoval($all_views, $original);
break;
case static::REVISION_DATA_TABLE_RENAME:
$this->dataTableRename($all_views, $entity_type->id(), $original->getRevisionDataTable(), $entity_type->getRevisionDataTable());
break;
case static::REVISION_DATA_TABLE_ADDITION:
$this->dataTableAddition($all_views, $entity_type, $entity_type->getRevisionDataTable(), $entity_type->getRevisionTable());
break;
case static::REVISION_DATA_TABLE_REMOVAL:
$this->dataTableRemoval($all_views, $entity_type->id(), $original->getRevisionDataTable(), $entity_type->getRevisionTable());
break;
}
}
foreach ($all_views as $view) {
$view->save();
}
}
/**
* {@inheritdoc}
*/
public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
$tables = [
$entity_type->getBaseTable(),
$entity_type->getDataTable(),
$entity_type->getRevisionTable(),
$entity_type->getRevisionDataTable(),
];
$all_views = $this->entityManager->getStorage('view')->loadMultiple(NULL);
/** @var \Drupal\views\Entity\View $view */
foreach ($all_views as $id => $view) {
// First check just the base table.
if (in_array($view->get('base_table'), $tables)) {
$view->disable();
$view->save();
}
}
}
/**
* Applies a callable onto all handlers of all passed in views.
*
* @param \Drupal\views\Entity\View[] $all_views
* All views entities.
* @param callable $process
* A callable which retrieves a handler config array.
*/
protected function processHandlers(array $all_views, callable $process) {
foreach ($all_views as $view) {
foreach (array_keys($view->get('display')) as $display_id) {
$display = &$view->getDisplay($display_id);
foreach (Views::getHandlerTypes() as $handler_type) {
$handler_type = $handler_type['plural'];
if (!isset($display['display_options'][$handler_type])) {
continue;
}
foreach ($display['display_options'][$handler_type] as $id => &$handler_config) {
$process($handler_config);
if ($handler_config === NULL) {
unset($display['display_options'][$handler_type][$id]);
}
}
}
}
}
}
/**
* Updates views if a base table is renamed.
*
* @param \Drupal\views\Entity\View[] $all_views
* All views.
* @param string $entity_type_id
* The entity type ID.
* @param string $old_base_table
* The old base table name.
* @param string $new_base_table
* The new base table name.
*/
protected function baseTableRename($all_views, $entity_type_id, $old_base_table, $new_base_table) {
foreach ($all_views as $view) {
if ($view->get('base_table') == $old_base_table) {
$view->set('base_table', $new_base_table);
}
}
$this->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $old_base_table, $new_base_table) {
if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id && $handler_config['table'] == $old_base_table) {
$handler_config['table'] = $new_base_table;
}