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;
}
......
langcode: und
status: true
dependencies: { }
id: test_view_entity_test
label: ''
module: views
description: ''
tag: ''
base_table: entity_test_update
base_field: nid
core: '8'
display:
default:
display_options:
access:
type: none
cache:
type: none
exposed_form:
type: basic
fields:
id:
alter:
alter_text: false
ellipsis: true
html: false
make_link: false
strip_tags: false
trim: false
word_boundary: true
empty_zero: false
field: id
hide_empty: false
id: id
table: entity_test_update
entity_type: entity_test_update
entity_field: id
plugin_id: numeric
name:
alter:
alter_text: false
ellipsis: true
html: false
make_link: false
strip_tags: false
trim: false
word_boundary: true
empty_zero: false
field: name
hide_empty: false
id: name
table: entity_test_update
plugin_id: standard
entity_type: entity_test_update
entity_field: name
pager:
type: some
style:
type: default
row:
type: fields
field_langcode: '***LANGUAGE_language_content***'
field_langcode_add_to_query: null
display_plugin: default
display_title: Master
id: default
position: 0
langcode: und
status: true
dependencies: { }
id: test_view_entity_test_additional_base_field
label: ''
module: views
description: ''
tag: ''
base_table: entity_test_update
base_field: nid
core: '8'
display:
default:
display_options:
access:
type: none
cache:
type: none
exposed_form:
type: basic
fields:
id:
alter:
alter_text: false
ellipsis: true
html: false
make_link: false
strip_tags: false
trim: false
word_boundary: true
empty_zero: false
field: id
hide_empty: false
id: id
table: entity_test_update
entity_type: entity_test_update
entity_field: id
plugin_id: numeric
new_base_field:
alter:
alter_text: false
ellipsis: true
html: false
make_link: false
strip_tags: false
trim: false
word_boundary: true
empty_zero: false
field: new_base_field
hide_empty: false
id: new_base_field
table: entity_test_update
plugin_id: standard
entity_type: entity_test_update
entity_field: new_base_field
pager:
type: some
style:
type: default
row:
type: fields
field_langcode: '***LANGUAGE_language_content***'
field_langcode_add_to_query: null
display_plugin: default
display_title: Master
id: default
position: 0
langcode: und
status: true
dependencies: { }
id: test_view_entity_test_data
label: ''
module: views
description: ''
tag: ''
base_table: entity_test_update
base_field: nid
core: '8'
display:
default:
display_options:
access:
type: none
cache:
type: none
exposed_form:
type: basic
fields:
id:
alter:
alter_text: false
ellipsis: true
html: false
make_link: false
strip_tags: false
trim: false
word_boundary: true
empty_zero: false
field: id
hide_empty: false
id: id
table: entity_test_update
plugin_id: numeric
entity_type: entity_test_update
entity_field: id
name:
alter:
alter_text: false
ellipsis: true
html: false
make_link: false
strip_tags: false
trim: false
word_boundary: true
empty_zero: false
field: name
hide_empty: false
id: name
table: entity_test_update_data
plugin_id: standard
entity_type: entity_test_update
entity_field: id
pager:
type: some
style:
type: default
row:
type: fields
field_langcode: '***LANGUAGE_language_content***'
field_langcode_add_to_query: null
display_plugin: default
display_title: Master
id: default
position: 0
langcode: und
status: true
dependencies: { }
id: test_view_entity_test_revision
label: ''
module: views
description: ''
tag: ''
base_table: entity_test_update_revision
base_field: nid
core: '8'
display:
default:
display_options:
access:
type: none
cache:
type: none
exposed_form:
type: basic
fields:
id:
alter:
alter_text: false
ellipsis: true
html: false
make_link: false
strip_tags: false
trim: false
word_boundary: true
empty_zero: false
field: id
hide_empty: false
id: id
table: entity_test_update_revision
entity_type: entity_test_update
entity_field: id
plugin_id: numeric
name:
alter:
alter_text: false
ellipsis: true
html: false
make_link: false
strip_tags: false
trim: false
word_boundary: true
empty_zero: false
field: name
hide_empty: false
id: name
table: entity_test_update_revision
plugin_id: standard
entity_type: entity_test_update
entity_field: name
pager:
type: some
<