diff --git a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php index f68a0da..505f997 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php @@ -106,7 +106,7 @@ class ContentEntityDatabaseStorage extends ContentEntityStorageBase implements S /** * The entity schema handler. * - * @var \Drupal\Core\Entity\Schema\EntitySchemaHandlerInterface + * @var \Drupal\Core\Entity\Schema\ContentEntitySchemaHandlerInterface */ protected $schemaHandler; @@ -220,13 +220,6 @@ public function getRevisionDataTable() { } /** - * {@inheritdoc} - */ - public function getSchema() { - return $this->schemaHandler()->getSchema(); - } - - /** * Gets the schema handler for this entity storage. * * @return \Drupal\Core\Entity\Schema\ContentEntitySchemaHandler @@ -234,7 +227,7 @@ public function getSchema() { */ protected function schemaHandler() { if (!isset($this->schemaHandler)) { - $this->schemaHandler = new ContentEntitySchemaHandler($this->entityManager, $this->entityType, $this); + $this->schemaHandler = new ContentEntitySchemaHandler($this->entityManager, $this->entityType, $this, $this->database); } return $this->schemaHandler; } @@ -242,6 +235,12 @@ protected function schemaHandler() { /** * {@inheritdoc} */ + public function customizeSchema(&$schema) { + } + + /** + * {@inheritdoc} + */ public function getTableMapping() { if (!isset($this->tableMapping)) { @@ -1152,6 +1151,7 @@ protected function loadFieldItems(array $entities) { // Collect impacted fields. $storage_definitions = array(); $definitions = array(); + $table_mapping = $this->getTableMapping(); foreach ($bundles as $bundle => $v) { $definitions[$bundle] = $this->entityManager->getFieldDefinitions($this->entityTypeId, $bundle); foreach ($definitions[$bundle] as $field_name => $field_definition) { @@ -1165,7 +1165,7 @@ protected function loadFieldItems(array $entities) { // Load field data. $langcodes = array_keys(language_list(LanguageInterface::STATE_ALL)); foreach ($storage_definitions as $field_name => $storage_definition) { - $table = $load_current ? static::_fieldTableName($storage_definition) : static::_fieldRevisionTableName($storage_definition); + $table = $load_current ? $table_mapping->getDedicatedDataTableName($storage_definition) : $table_mapping->getDedicatedRevisionTableName($storage_definition); // Ensure that only values having valid languages are retrieved. Since we // are loading values for multiple entities, we cannot limit the query to @@ -1194,7 +1194,7 @@ protected function loadFieldItems(array $entities) { // For each column declared by the field, populate the item from the // prefixed database column. foreach ($storage_definition->getColumns() as $column => $attributes) { - $column_name = static::_fieldColumnName($storage_definition, $column); + $column_name = $table_mapping->getFieldColumnName($storage_definition, $column); // Unserialize the value if specified in the column schema. $item[$column] = (!empty($attributes['serialize'])) ? unserialize($row->$column_name) : $row->$column_name; } @@ -1223,6 +1223,7 @@ protected function saveFieldItems(EntityInterface $entity, $update = TRUE) { $entity_type = $entity->getEntityTypeId(); $default_langcode = $entity->getUntranslated()->language()->id; $translation_langcodes = array_keys($entity->getTranslationLanguages()); + $table_mapping = $this->getTableMapping(); if (!isset($vid)) { $vid = $id; @@ -1233,8 +1234,8 @@ protected function saveFieldItems(EntityInterface $entity, $update = TRUE) { if (!$this->usesDedicatedTable($storage_definition)) { continue; } - $table_name = static::_fieldTableName($storage_definition); - $revision_name = static::_fieldRevisionTableName($storage_definition); + $table_name = $table_mapping->getDedicatedDataTableName($storage_definition); + $revision_name = $table_mapping->getDedicatedRevisionTableName($storage_definition); // Delete and insert, rather than update, in case a value was added. if ($update) { @@ -1255,7 +1256,7 @@ protected function saveFieldItems(EntityInterface $entity, $update = TRUE) { $do_insert = FALSE; $columns = array('entity_id', 'revision_id', 'bundle', 'delta', 'langcode'); foreach ($storage_definition->getColumns() as $column => $attributes) { - $columns[] = static::_fieldColumnName($storage_definition, $column); + $columns[] = $table_mapping->getFieldColumnName($storage_definition, $column); } $query = $this->database->insert($table_name)->fields($columns); $revision_query = $this->database->insert($revision_name)->fields($columns); @@ -1276,7 +1277,7 @@ protected function saveFieldItems(EntityInterface $entity, $update = TRUE) { 'langcode' => $langcode, ); foreach ($storage_definition->getColumns() as $column => $attributes) { - $column_name = static::_fieldColumnName($storage_definition, $column); + $column_name = $table_mapping->getFieldColumnName($storage_definition, $column); // Serialize the value if specified in the column schema. $record[$column_name] = !empty($attributes['serialize']) ? serialize($item->$column) : $item->$column; } @@ -1308,13 +1309,14 @@ protected function saveFieldItems(EntityInterface $entity, $update = TRUE) { * The entity. */ protected function deleteFieldItems(EntityInterface $entity) { + $table_mapping = $this->getTableMapping(); foreach ($this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle()) as $field_definition) { $storage_definition = $field_definition->getFieldStorageDefinition(); if (!$this->usesDedicatedTable($storage_definition)) { continue; } - $table_name = static::_fieldTableName($storage_definition); - $revision_name = static::_fieldRevisionTableName($storage_definition); + $table_name = $table_mapping->getDedicatedDataTableName($storage_definition); + $revision_name = $table_mapping->getDedicatedRevisionTableName($storage_definition); $this->database->delete($table_name) ->condition('entity_id', $entity->id()) ->execute(); @@ -1333,12 +1335,13 @@ protected function deleteFieldItems(EntityInterface $entity) { protected function deleteFieldItemsRevision(EntityInterface $entity) { $vid = $entity->getRevisionId(); if (isset($vid)) { + $table_mapping = $this->getTableMapping(); foreach ($this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle()) as $field_definition) { $storage_definition = $field_definition->getFieldStorageDefinition(); if (!$this->usesDedicatedTable($storage_definition)) { continue; } - $revision_name = static::_fieldRevisionTableName($storage_definition); + $revision_name = $table_mapping->getDedicatedRevisionTableName($storage_definition); $this->database->delete($revision_name) ->condition('entity_id', $entity->id()) ->condition('revision_id', $vid) @@ -1365,6 +1368,27 @@ protected function usesDedicatedTable(FieldStorageDefinitionInterface $definitio /** * {@inheritdoc} */ + public function onEntityTypeDefinitionCreate() { + $this->schemaHandler()->createEntitySchema($this->entityType); + } + + /** + * {@inheritdoc} + */ + public function onEntityTypeDefinitionUpdate(EntityTypeInterface $original) { + $this->schemaHandler()->updateEntitySchema($this->entityType, $original); + } + + /** + * {@inheritdoc} + */ + public function onEntityTypeDefinitionDelete() { + $this->schemaHandler()->dropEntitySchema($this->entityType); + } + + /** + * {@inheritdoc} + */ public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) { $schema = $this->_fieldSqlSchema($storage_definition); foreach ($schema as $name => $table) { @@ -1418,8 +1442,9 @@ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $ // There is data, so there are no column changes. Drop all the prior // indexes and create all the new ones, except for all the priors that // exist unchanged. - $table = static::_fieldTableName($original); - $revision_table = static::_fieldRevisionTableName($original); + $table_mapping = $this->getTableMapping(); + $table = $table_mapping->getDedicatedDataTableName($original); + $revision_table = $table_mapping->getDedicatedRevisionTableName($original); $schema = $storage_definition->getSchema(); $original_schema = $original->getSchema(); @@ -1431,8 +1456,8 @@ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $ $this->database->schema()->dropIndex($revision_table, $real_name); } } - $table = static::_fieldTableName($storage_definition); - $revision_table = static::_fieldRevisionTableName($storage_definition); + $table = $table_mapping->getDedicatedDataTableName($storage_definition); + $revision_table = $table_mapping->getDedicatedRevisionTableName($storage_definition); foreach ($schema['indexes'] as $name => $columns) { if (!isset($original_schema['indexes'][$name]) || $columns != $original_schema['indexes'][$name]) { $real_name = static::_fieldIndexName($storage_definition, $name); @@ -1442,12 +1467,12 @@ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $ // column name and length. Allow for either case. if (is_array($column_name)) { $real_columns[] = array( - static::_fieldColumnName($storage_definition, $column_name[0]), + $table_mapping->getFieldColumnName($storage_definition, $column_name[0]), $column_name[1], ); } else { - $real_columns[] = static::_fieldColumnName($storage_definition, $column_name); + $real_columns[] = $table_mapping->getFieldColumnName($storage_definition, $column_name); } } $this->database->schema()->addIndex($table, $real_name, $real_columns); @@ -1461,17 +1486,19 @@ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $ * {@inheritdoc} */ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) { + $table_mapping = $this->getTableMapping(); + // Mark all data associated with the field for deletion. - $table = static::_fieldTableName($storage_definition); - $revision_table = static::_fieldRevisionTableName($storage_definition); + $table = $table_mapping->getDedicatedDataTableName($storage_definition); + $revision_table = $table_mapping->getDedicatedRevisionTableName($storage_definition); $this->database->update($table) ->fields(array('deleted' => 1)) ->execute(); // Move the table to a unique name while the table contents are being // deleted. - $new_table = static::_fieldTableName($storage_definition, TRUE); - $revision_new_table = static::_fieldRevisionTableName($storage_definition, TRUE); + $new_table = $table_mapping->getDedicatedDataTableName($storage_definition, TRUE); + $revision_new_table = $table_mapping->getDedicatedRevisionTableName($storage_definition, TRUE); $this->database->schema()->renameTable($table, $new_table); $this->database->schema()->renameTable($revision_table, $revision_new_table); } @@ -1480,11 +1507,11 @@ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $ * {@inheritdoc} */ public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition) { + $table_mapping = $this->getTableMapping(); $storage_definition = $field_definition->getFieldStorageDefinition(); - $table_name = static::_fieldTableName($storage_definition); - $revision_name = static::_fieldRevisionTableName($storage_definition); - // Mark field data as deleted. + $table_name = $table_mapping->getDedicatedDataTableName($storage_definition); + $revision_name = $table_mapping->getDedicatedRevisionTableName($storage_definition); $this->database->update($table_name) ->fields(array('deleted' => 1)) ->condition('bundle', $field_definition->getBundle()) @@ -1507,13 +1534,14 @@ public function onBundleRename($bundle, $bundle_new) { // @todo Use the unified store of deleted field definitions instead in // https://www.drupal.org/node/2282119 $field_definitions += entity_load_multiple_by_properties('field_instance_config', array('entity_type' => $this->entityTypeId, 'bundle' => $bundle, 'deleted' => TRUE, 'include_deleted' => TRUE)); + $table_mapping = $this->getTableMapping(); foreach ($field_definitions as $field_definition) { $storage_definition = $field_definition->getFieldStorageDefinition(); if ($this->usesDedicatedTable($storage_definition)) { $is_deleted = $this->storageDefinitionIsDeleted($storage_definition); - $table_name = static::_fieldTableName($storage_definition, $is_deleted); - $revision_name = static::_fieldRevisionTableName($storage_definition, $is_deleted); + $table_name = $table_mapping->getDedicatedDataTableName($storage_definition, $is_deleted); + $revision_name = $table_mapping->getDedicatedRevisionTableName($storage_definition, $is_deleted); $this->database->update($table_name) ->fields(array('bundle' => $bundle_new)) ->condition('bundle', $bundle) @@ -1534,13 +1562,14 @@ protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definit // bundle fields. $storage_definition = $field_definition->getFieldStorageDefinition(); $is_deleted = $this->storageDefinitionIsDeleted($storage_definition); - $table_name = static::_fieldTableName($storage_definition, $is_deleted); + $table_mapping = $this->getTableMapping(); + $table_name = $table_mapping->getDedicatedDataTableName($storage_definition, $is_deleted); // Get the entities which we want to purge first. $entity_query = $this->database->select($table_name, 't', array('fetch' => \PDO::FETCH_ASSOC)); $or = $entity_query->orConditionGroup(); foreach ($storage_definition->getColumns() as $column_name => $data) { - $or->isNotNull(static::_fieldColumnName($storage_definition, $column_name)); + $or->isNotNull($table_mapping->getFieldColumnName($storage_definition, $column_name)); } $entity_query ->distinct(TRUE) @@ -1551,7 +1580,7 @@ protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definit // Create a map of field data table column names to field column names. $column_map = array(); foreach ($storage_definition->getColumns() as $column_name => $data) { - $column_map[static::_fieldColumnName($storage_definition, $column_name)] = $column_name; + $column_map[$table_mapping->getFieldColumnName($storage_definition, $column_name)] = $column_name; } $entities = array(); @@ -1591,8 +1620,9 @@ protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definit protected function purgeFieldItems(ContentEntityInterface $entity, FieldDefinitionInterface $field_definition) { $storage_definition = $field_definition->getFieldStorageDefinition(); $is_deleted = $this->storageDefinitionIsDeleted($storage_definition); - $table_name = static::_fieldTableName($storage_definition, $is_deleted); - $revision_name = static::_fieldRevisionTableName($storage_definition, $is_deleted); + $table_mapping = $this->getTableMapping(); + $table_name = $table_mapping->getDedicatedDataTableName($storage_definition, $is_deleted); + $revision_name = $table_mapping->getDedicatedRevisionTableName($storage_definition, $is_deleted); $revision_id = $this->entityType->isRevisionable() ? $entity->getRevisionId() : $entity->id(); $this->database->delete($table_name) ->condition('revision_id', $revision_id) @@ -1606,8 +1636,9 @@ protected function purgeFieldItems(ContentEntityInterface $entity, FieldDefiniti * {@inheritdoc} */ public function finalizePurge(FieldStorageDefinitionInterface $storage_definition) { - $table_name = static::_fieldTableName($storage_definition, TRUE); - $revision_name = static::_fieldRevisionTableName($storage_definition, TRUE); + $table_mapping = $this->getTableMapping(); + $table_name = $table_mapping->getDedicatedDataTableName($storage_definition, TRUE); + $revision_name = $table_mapping->getDedicatedRevisionTableName($storage_definition, TRUE); $this->database->schema()->dropTable($table_name); $this->database->schema()->dropTable($revision_name); } @@ -1616,13 +1647,14 @@ public function finalizePurge(FieldStorageDefinitionInterface $storage_definitio * {@inheritdoc} */ public function countFieldData($storage_definition, $as_bool = FALSE) { - $is_deleted = $this->storageDefinitionIsDeleted($storage_definition); - $table_name = static::_fieldTableName($storage_definition, $is_deleted); + $table_mapping = $this->getTableMapping(); + $is_deleted = $this->storageDefinitionIsDeleted($storage_definition); + $table_name = $table_mapping->getDedicatedDataTableName($storage_definition, $is_deleted); $query = $this->database->select($table_name, 't'); $or = $query->orConditionGroup(); foreach ($storage_definition->getColumns() as $column_name => $data) { - $or->isNotNull(static::_fieldColumnName($storage_definition, $column_name)); + $or->isNotNull($table_mapping->getFieldColumnName($storage_definition, $column_name)); } $query ->condition($or) @@ -1672,6 +1704,8 @@ protected function storageDefinitionIsDeleted(FieldStorageDefinitionInterface $s * @see hook_schema() */ public static function _fieldSqlSchema(FieldStorageDefinitionInterface $storage_definition, array $schema = NULL, $deleted = FALSE) { + $table_mapping = new DefaultTableMapping(array()); + $description_current = "Data storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}."; $description_revision = "Revision archive storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}."; @@ -1768,7 +1802,7 @@ public static function _fieldSqlSchema(FieldStorageDefinitionInterface $storage_ // Add field columns. foreach ($schema['columns'] as $column_name => $attributes) { - $real_name = static::_fieldColumnName($storage_definition, $column_name); + $real_name = $table_mapping->getFieldColumnName($storage_definition, $column_name); $current['fields'][$real_name] = $attributes; } @@ -1776,7 +1810,7 @@ public static function _fieldSqlSchema(FieldStorageDefinitionInterface $storage_ foreach ($schema['unique keys'] as $unique_key_name => $columns) { $real_name = static::_fieldIndexName($storage_definition, $unique_key_name); foreach ($columns as $column_name) { - $current['unique keys'][$real_name][] = static::_fieldColumnName($storage_definition, $column_name); + $current['unique keys'][$real_name][] = $table_mapping->getFieldColumnName($storage_definition, $column_name); } } @@ -1788,12 +1822,12 @@ public static function _fieldSqlSchema(FieldStorageDefinitionInterface $storage_ // column name and length. Allow for either case. if (is_array($column_name)) { $current['indexes'][$real_name][] = array( - static::_fieldColumnName($storage_definition, $column_name[0]), + $table_mapping->getFieldColumnName($storage_definition, $column_name[0]), $column_name[1], ); } else { - $current['indexes'][$real_name][] = static::_fieldColumnName($storage_definition, $column_name); + $current['indexes'][$real_name][] = $table_mapping->getFieldColumnName($storage_definition, $column_name); } } } @@ -1803,7 +1837,7 @@ public static function _fieldSqlSchema(FieldStorageDefinitionInterface $storage_ $real_name = static::_fieldIndexName($storage_definition, $specifier); $current['foreign keys'][$real_name]['table'] = $specification['table']; foreach ($specification['columns'] as $column_name => $referenced) { - $sql_storage_column = static::_fieldColumnName($storage_definition, $column_name); + $sql_storage_column = $table_mapping->getFieldColumnName($storage_definition, $column_name); $current['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced; } } @@ -1816,109 +1850,12 @@ public static function _fieldSqlSchema(FieldStorageDefinitionInterface $storage_ $revision['fields']['revision_id']['description'] = 'The entity revision id this data is attached to'; return array( - static::_fieldTableName($storage_definition) => $current, - static::_fieldRevisionTableName($storage_definition) => $revision, + $table_mapping->getDedicatedDataTableName($storage_definition) => $current, + $table_mapping->getDedicatedRevisionTableName($storage_definition) => $revision, ); } /** - * Generates a table name for a field data table. - * - * @private Calling this function circumvents the entity system and is - * strongly discouraged. This function is not considered part of the public - * API and modules relying on it might break even in minor releases. Only - * call this function to write a query that \Drupal::entityQuery() does not - * support. Always call entity_load() before using the data found in the - * table. - * - * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition - * The field storage definition. - * @param bool $is_deleted - * (optional) Whether the table name holding the values of a deleted field - * should be returned. - * - * @return string - * A string containing the generated name for the database table. - */ - public static function _fieldTableName(FieldStorageDefinitionInterface $storage_definition, $is_deleted = FALSE) { - if ($is_deleted) { - // When a field is a deleted, the table is renamed to - // {field_deleted_data_FIELD_UUID}. To make sure we don't end up with - // table names longer than 64 characters, we hash the unique storage - // identifier and return the first 10 characters so we end up with a short - // unique ID. - return "field_deleted_data_" . substr(hash('sha256', $storage_definition->getUniqueStorageIdentifier()), 0, 10); - } - else { - return static::_generateFieldTableName($storage_definition, FALSE); - } - } - - /** - * Generates a table name for a field revision archive table. - * - * @private Calling this function circumvents the entity system and is - * strongly discouraged. This function is not considered part of the public - * API and modules relying on it might break even in minor releases. Only - * call this function to write a query that \Drupal::entityQuery() does not - * support. Always call entity_load() before using the data found in the - * table. - * - * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition - * The field storage definition. - * @param bool $is_deleted - * (optional) Whether the table name holding the values of a deleted field - * should be returned. - * - * @return string - * A string containing the generated name for the database table. - */ - public static function _fieldRevisionTableName(FieldStorageDefinitionInterface $storage_definition, $is_deleted = FALSE) { - if ($is_deleted) { - // When a field is a deleted, the table is renamed to - // {field_deleted_revision_FIELD_UUID}. To make sure we don't end up with - // table names longer than 64 characters, we hash the unique storage - // identifier and return the first 10 characters so we end up with a short - // unique ID. - return "field_deleted_revision_" . substr(hash('sha256', $storage_definition->getUniqueStorageIdentifier()), 0, 10); - } - else { - return static::_generateFieldTableName($storage_definition, TRUE); - } - } - - /** - * Generates a safe and unanbiguous field table name. - * - * The method accounts for a maximum table name length of 64 characters, and - * takes care of disambiguation. - * - * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition - * The field storage definition. - * @param bool $revision - * TRUE for revision table, FALSE otherwise. - * - * @return string - * The final table name. - */ - protected static function _generateFieldTableName(FieldStorageDefinitionInterface $storage_definition, $revision) { - $separator = $revision ? '_revision__' : '__'; - $table_name = $storage_definition->getTargetEntityTypeId() . $separator . $storage_definition->getName(); - // Limit the string to 48 characters, keeping a 16 characters margin for db - // prefixes. - if (strlen($table_name) > 48) { - // Use a shorter separator, a truncated entity_type, and a hash of the - // field UUID. - $separator = $revision ? '_r__' : '__'; - // Truncate to the same length for the current and revision tables. - $entity_type = substr($storage_definition->getTargetEntityTypeId(), 0, 34); - $field_hash = substr(hash('sha256', $storage_definition->getUniqueStorageIdentifier()), 0, 10); - $table_name = $entity_type . $separator . $field_hash; - } - return $table_name; - } - - /** * Generates an index name for a field data table. * * @private Calling this function circumvents the entity system and is @@ -1938,27 +1875,4 @@ public static function _fieldIndexName(FieldStorageDefinitionInterface $storage_ return $storage_definition->getName() . '_' . $index; } - /** - * Generates a column name for a field data table. - * - * @private Calling this function circumvents the entity system and is - * strongly discouraged. This function is not considered part of the public - * API and modules relying on it might break even in minor releases. Only - * call this function to write a query that \Drupal::entityQuery() does not - * support. Always call entity_load() before using the data found in the - * table. - * - * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition - * The field storage definition. - * @param string $column - * The name of the column. - * - * @return string - * A string containing a generated column name for a field data table that is - * unique among all other fields. - */ - public static function _fieldColumnName(FieldStorageDefinitionInterface $storage_definition, $column) { - return in_array($column, FieldStorageConfig::getReservedColumns()) ? $column : $storage_definition->getName() . '_' . $column; - } - } diff --git a/core/lib/Drupal/Core/Entity/EntityStorageBase.php b/core/lib/Drupal/Core/Entity/EntityStorageBase.php index b181bb4..815b91f 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -463,4 +463,22 @@ public function getQuery($conjunction = 'AND') { return \Drupal::entityQuery($this->getEntityTypeId(), $conjunction); } + /** + * {@inheritdoc} + */ + public function onEntityTypeDefinitionCreate() { + } + + /** + * {@inheritdoc} + */ + public function onEntityTypeDefinitionUpdate(EntityTypeInterface $original) { + } + + /** + * {@inheritdoc} + */ + public function onEntityTypeDefinitionDelete() { + } + } diff --git a/core/lib/Drupal/Core/Entity/EntityStorageInterface.php b/core/lib/Drupal/Core/Entity/EntityStorageInterface.php index 32867f4..590f1ad 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageInterface.php @@ -186,4 +186,22 @@ public function getEntityTypeId(); */ public function getEntityType(); + /** + * Reacts to the creation of the entity type definition. + */ + public function onEntityTypeDefinitionCreate(); + + /** + * Reacts to the update of the entity type definition. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $original + * The original entity type definition. + */ + public function onEntityTypeDefinitionUpdate(EntityTypeInterface $original); + + /** + * Reacts to the deletion of the entity type definition. + */ + public function onEntityTypeDefinitionDelete(); + } diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php b/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php index 23ce020..b1c1a54 100644 --- a/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php +++ b/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php @@ -10,10 +10,8 @@ use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\Core\Entity\EntityStorageInterface; -use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\Core\Entity\Query\QueryException; use Drupal\Core\Entity\Sql\SqlEntityStorageInterface; -use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\FieldStorageConfigInterface; /** @@ -116,11 +114,14 @@ public function addField($field, $type, $langcode) { if ($field_storage instanceof FieldStorageConfigInterface) { // Find the field column. $column = $field_storage->getMainPropertyName(); + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = $this->entityManager->getStorage($entity_type_id)->getTableMapping(); + if ($key < $count) { $next = $specifiers[$key + 1]; // Is this a field column? $columns = $field_storage->getColumns(); - if (isset($columns[$next]) || in_array($next, FieldStorageConfig::getReservedColumns())) { + if (isset($columns[$next]) || in_array($next, $table_mapping->getReservedColumns())) { // Use it. $column = $next; // Do not process it again. @@ -142,7 +143,7 @@ public function addField($field, $type, $langcode) { } } $table = $this->ensureFieldTable($index_prefix, $field_storage, $type, $langcode, $base_table, $entity_id_field, $field_id_field); - $sql_column = ContentEntityDatabaseStorage::_fieldColumnName($field_storage, $column); + $sql_column = $table_mapping->getFieldColumnName($field_storage, $column); } // This is an entity base field (non-configurable field). else { @@ -220,11 +221,13 @@ protected function ensureEntityTable($index_prefix, $property, $type, $langcode, protected function ensureFieldTable($index_prefix, &$field, $type, $langcode, $base_table, $entity_id_field, $field_id_field) { $field_name = $field->getName(); if (!isset($this->fieldTables[$index_prefix . $field_name])) { - $table = $this->sqlQuery->getMetaData('age') == EntityStorageInterface::FIELD_LOAD_CURRENT ? ContentEntityDatabaseStorage::_fieldTableName($field) : ContentEntityDatabaseStorage::_fieldRevisionTableName($field); + $entity_type_id = $this->sqlQuery->getMetaData('entity_type'); + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = $this->entityManager->getStorage($entity_type_id)->getTableMapping(); + $table = $this->sqlQuery->getMetaData('age') == EntityStorageInterface::FIELD_LOAD_CURRENT ? $table_mapping->getDedicatedDataTableName($field) : $table_mapping->getDedicatedRevisionTableName($field); if ($field->getCardinality() != 1) { $this->sqlQuery->addMetaData('simple_query', FALSE); } - $entity_type = $this->sqlQuery->getMetaData('entity_type'); $this->fieldTables[$index_prefix . $field_name] = $this->addJoin($type, $table, "%alias.$field_id_field = $base_table.$entity_id_field", $langcode); } return $this->fieldTables[$index_prefix . $field_name]; diff --git a/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandler.php b/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandler.php index 4f30d47..90d3afc 100644 --- a/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandler.php +++ b/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandler.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Entity\Schema; +use Drupal\Core\Database\Connection; use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\Core\Entity\EntityManagerInterface; @@ -14,7 +15,7 @@ /** * Defines a schema handler that supports revisionable, translatable entities. */ -class ContentEntitySchemaHandler implements EntitySchemaHandlerInterface { +class ContentEntitySchemaHandler implements ContentEntitySchemaHandlerInterface { /** * The entity type this schema builder is responsible for. @@ -45,6 +46,13 @@ class ContentEntitySchemaHandler implements EntitySchemaHandlerInterface { protected $schema; /** + * The database connection to be used. + * + * @var \Drupal\Core\Database\Connection + */ + protected $database; + + /** * Constructs a ContentEntitySchemaHandler. * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager @@ -53,22 +61,69 @@ class ContentEntitySchemaHandler implements EntitySchemaHandlerInterface { * The entity type. * @param \Drupal\Core\Entity\ContentEntityDatabaseStorage $storage * The storage of the entity type. This must be an SQL-based storage. + * @param \Drupal\Core\Database\Connection $database + * The database connection to be used. */ - public function __construct(EntityManagerInterface $entity_manager, ContentEntityTypeInterface $entity_type, ContentEntityDatabaseStorage $storage) { + public function __construct(EntityManagerInterface $entity_manager, ContentEntityTypeInterface $entity_type, ContentEntityDatabaseStorage $storage, Connection $database) { $this->entityType = $entity_type; $this->fieldStorageDefinitions = $entity_manager->getFieldStorageDefinitions($entity_type->id()); $this->storage = $storage; + $this->database = $database; + } + + /** + * {@inheritdoc} + */ + public function createEntitySchema(ContentEntityTypeInterface $entity_type) { + $schema_handler = $this->database->schema(); + $schema = $this->getEntitySchema($entity_type, TRUE); + foreach ($schema as $table_name => $table_schema) { + if (!$schema_handler->tableExists($table_name)) { + $schema_handler->createTable($table_name, $table_schema); + } + } } /** * {@inheritdoc} */ - public function getSchema() { - // Prepare basic information about the entity type. - $tables = $this->getTables(); + public function dropEntitySchema(ContentEntityTypeInterface $entity_type) { + $schema_handler = $this->database->schema(); + $schema = $this->getEntitySchema($entity_type, TRUE); + foreach ($schema as $table_name => $table_schema) { + if ($schema_handler->tableExists($table_name)) { + $schema_handler->dropTable($table_name); + } + } + } - if (!isset($this->schema[$this->entityType->id()])) { + /** + * {@inheritdoc} + */ + public function updateEntitySchema(ContentEntityTypeInterface $entity_type, ContentEntityTypeInterface $original) { + // @todo Implement proper updates: https://www.drupal.org/node/1498720 + $this->createEntitySchema($entity_type); + } + + /** + * Returns the entity schema for the specified entity type. + * + * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type + * The entity type definition. + * @param bool $reset + * (optional) If set to TRUE static cache will be ignored and a new schema + * array generation will be performed. Defaults to FALSE. + * + * @return array + * A Schema API array describing the entity schema, excluding dedicated + * field tables. + */ + protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) { + $entity_type_id = $entity_type->id(); + + if (!isset($this->schema[$entity_type_id]) || $reset) { // Initialize the table schema. + $tables = $this->getTables(); $schema[$tables['base_table']] = $this->initializeBaseTable(); if (isset($tables['revision_table'])) { $schema[$tables['revision_table']] = $this->initializeRevisionTable(); @@ -108,10 +163,13 @@ public function getSchema() { $this->processRevisionDataTable($schema[$tables['revision_data_table']]); } - $this->schema[$this->entityType->id()] = $schema; + // Perform additional customizations. + $this->storage->customizeSchema($schema); + + $this->schema[$entity_type_id] = $schema; } - return $this->schema[$this->entityType->id()]; + return $this->schema[$entity_type_id]; } /** diff --git a/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandlerInterface.php b/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandlerInterface.php new file mode 100644 index 0000000..94c599a --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandlerInterface.php @@ -0,0 +1,47 @@ +getUniqueStorageIdentifier()), 0, 10); + } + else { + return $this->generateFieldTableName($storage_definition, FALSE); + } + } + + /** + * Generates a table name for a field revision archive table. + * + * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition + * The field storage definition. + * @param bool $is_deleted + * (optional) Whether the table name holding the values of a deleted field + * should be returned. + * + * @return string + * A string containing the generated name for the database table. + */ + public function getDedicatedRevisionTableName(FieldStorageDefinitionInterface $storage_definition, $is_deleted = FALSE) { + if ($is_deleted) { + // When a field is a deleted, the table is renamed to + // {field_deleted_revision_FIELD_UUID}. To make sure we don't end up with + // table names longer than 64 characters, we hash the unique storage + // identifier and return the first 10 characters so we end up with a short + // unique ID. + return "field_deleted_revision_" . substr(hash('sha256', $storage_definition->getUniqueStorageIdentifier()), 0, 10); + } + else { + return $this->generateFieldTableName($storage_definition, TRUE); + } + } + + /** + * Generates a safe and unambiguous field table name. + * + * The method accounts for a maximum table name length of 64 characters, and + * takes care of disambiguation. + * + * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition + * The field storage definition. + * @param bool $revision + * TRUE for revision table, FALSE otherwise. + * + * @return string + * The final table name. + */ + protected function generateFieldTableName(FieldStorageDefinitionInterface $storage_definition, $revision) { + $separator = $revision ? '_revision__' : '__'; + $table_name = $storage_definition->getTargetEntityTypeId() . $separator . $storage_definition->getName(); + // Limit the string to 48 characters, keeping a 16 characters margin for db + // prefixes. + if (strlen($table_name) > 48) { + // Use a shorter separator, a truncated entity_type, and a hash of the + // field UUID. + $separator = $revision ? '_r__' : '__'; + // Truncate to the same length for the current and revision tables. + $entity_type = substr($storage_definition->getTargetEntityTypeId(), 0, 34); + $field_hash = substr(hash('sha256', $storage_definition->getUniqueStorageIdentifier()), 0, 10); + $table_name = $entity_type . $separator . $field_hash; + } + return $table_name; + } + + /** + * {@inheritdoc} + */ + public function getFieldColumnName(FieldStorageDefinitionInterface $storage_definition, $column) { + return in_array($column, $this->getReservedColumns()) ? $column : $storage_definition->getName() . '_' . $column; + } + } diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlEntityStorageInterface.php b/core/lib/Drupal/Core/Entity/Sql/SqlEntityStorageInterface.php index 107ed26..e8d07df 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlEntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlEntityStorageInterface.php @@ -8,12 +8,11 @@ namespace Drupal\Core\Entity\Sql; use Drupal\Core\Entity\EntityStorageInterface; -use Drupal\Core\Entity\Schema\EntitySchemaProviderInterface; /** * A common interface for SQL-based entity storage implementations. */ -interface SqlEntityStorageInterface extends EntityStorageInterface, EntitySchemaProviderInterface { +interface SqlEntityStorageInterface extends EntityStorageInterface { /** * Gets a table mapping for the entity's SQL tables. @@ -23,4 +22,9 @@ */ public function getTableMapping(); + /** + * @todo Document. + */ + public function customizeSchema(&$schema); + } diff --git a/core/lib/Drupal/Core/Entity/Sql/TableMappingInterface.php b/core/lib/Drupal/Core/Entity/Sql/TableMappingInterface.php index c6b5706..9607fe4 100644 --- a/core/lib/Drupal/Core/Entity/Sql/TableMappingInterface.php +++ b/core/lib/Drupal/Core/Entity/Sql/TableMappingInterface.php @@ -7,8 +7,20 @@ namespace Drupal\Core\Entity\Sql; +use Drupal\Core\Field\FieldStorageDefinitionInterface; + /** * Provides a common interface for mapping field columns to SQL tables. + * + * Warning: using methods provided here should be done only when writing code + * that is explicitly targeting a SQL-based entity storage. Typically this API + * is used by SQL storage classes, or other SQL-specific code like the Views + * integration code for the Entity SQL storage. Another example of legal usage + * of this API is when needing to write a query that \Drupal::entityQuery() does + * not support. Always retrieve entity identifiers and use them to load entities + * instead of accessing data stored in the database directly. Any other usage + * circumvents the entity system and is strongly discouraged, at least when + * writing contributed code. */ interface TableMappingInterface { @@ -70,4 +82,24 @@ public function getColumnNames($field_name); */ public function getExtraColumns($table_name); + /** + * A list of columns that can not be used as field type columns. + * + * @return array + */ + public function getReservedColumns(); + + /** + * Generates a column name for a field. + * + * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition + * The field storage definition. + * @param string $column + * The name of the column. + * + * @return string + * A string containing a generated column name for a field data table that is + * unique among all other fields. + */ + public function getFieldColumnName(FieldStorageDefinitionInterface $storage_definition, $column); } diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php index cb65449..46a6aab 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandler.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php @@ -12,7 +12,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\String; use Drupal\Core\Cache\CacheBackendInterface; -use Drupal\Core\Entity\Schema\EntitySchemaProviderInterface; +use Drupal\Core\Entity\ContentEntityTypeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -847,17 +847,10 @@ public function install(array $module_list, $enable_dependencies = TRUE) { // Install any entity schemas belonging to the module. $entity_manager = \Drupal::entityManager(); - $schema = \Drupal::database()->schema(); foreach ($entity_manager->getDefinitions() as $entity_type) { - if ($entity_type->getProvider() == $module) { + if ($entity_type instanceof ContentEntityTypeInterface && $entity_type->getProvider() == $module) { $storage = $entity_manager->getStorage($entity_type->id()); - if ($storage instanceof EntitySchemaProviderInterface) { - foreach ($storage->getSchema() as $table_name => $table_schema) { - if (!$schema->tableExists($table_name)) { - $schema->createTable($table_name, $table_schema); - } - } - } + $storage->onEntityTypeDefinitionCreate(); } } @@ -955,17 +948,10 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) { // Remove any entity schemas belonging to the module. $entity_manager = \Drupal::entityManager(); - $schema = \Drupal::database()->schema(); foreach ($entity_manager->getDefinitions() as $entity_type) { - if ($entity_type->getProvider() == $module) { + if ($entity_type instanceof ContentEntityTypeInterface && $entity_type->getProvider() == $module) { $storage = $entity_manager->getStorage($entity_type->id()); - if ($storage instanceof EntitySchemaProviderInterface) { - foreach ($storage->getSchema() as $table_name => $table_schema) { - if ($schema->tableExists($table_name)) { - $schema->dropTable($table_name); - } - } - } + $storage->onEntityTypeDefinitionDelete(); } } diff --git a/core/modules/aggregator/src/FeedStorage.php b/core/modules/aggregator/src/FeedStorage.php index da784f5..ddc054a 100644 --- a/core/modules/aggregator/src/FeedStorage.php +++ b/core/modules/aggregator/src/FeedStorage.php @@ -21,9 +21,7 @@ class FeedStorage extends ContentEntityDatabaseStorage implements FeedStorageInt /** * {@inheritdoc} */ - public function getSchema() { - $schema = parent::getSchema(); - + public function customizeSchema(&$schema) { // Marking the respective fields as NOT NULL makes the indexes more // performant. $schema['aggregator_feed']['fields']['url']['not null'] = TRUE; @@ -37,8 +35,6 @@ public function getSchema() { $schema['aggregator_feed']['unique keys'] += array( 'aggregator_feed__title' => array('title'), ); - - return $schema; } /** diff --git a/core/modules/aggregator/src/ItemStorage.php b/core/modules/aggregator/src/ItemStorage.php index f2b4aa6..bd38a3a 100644 --- a/core/modules/aggregator/src/ItemStorage.php +++ b/core/modules/aggregator/src/ItemStorage.php @@ -22,9 +22,7 @@ class ItemStorage extends ContentEntityDatabaseStorage implements ItemStorageInt /** * {@inheritdoc} */ - public function getSchema() { - $schema = parent::getSchema(); - + public function customizeSchema(&$schema) { // Marking the respective fields as NOT NULL makes the indexes more // performant. $schema['aggregator_item']['fields']['timestamp']['not null'] = TRUE; @@ -38,8 +36,6 @@ public function getSchema() { 'columns' => array('fid' => 'fid'), ), ); - - return $schema; } /** diff --git a/core/modules/block_content/src/BlockContentStorage.php b/core/modules/block_content/src/BlockContentStorage.php index b98a861..8301bba 100644 --- a/core/modules/block_content/src/BlockContentStorage.php +++ b/core/modules/block_content/src/BlockContentStorage.php @@ -17,9 +17,7 @@ class BlockContentStorage extends ContentEntityDatabaseStorage { /** * {@inheritdoc} */ - public function getSchema() { - $schema = parent::getSchema(); - + public function customizeSchema(&$schema) { // Marking the respective fields as NOT NULL makes the indexes more // performant. $schema['block_content_field_data']['fields']['info']['not null'] = TRUE; @@ -27,8 +25,6 @@ public function getSchema() { $schema['block_content_field_data']['unique keys'] += array( 'block_content__info' => array('info', 'langcode'), ); - - return $schema; } } diff --git a/core/modules/comment/src/CommentStorage.php b/core/modules/comment/src/CommentStorage.php index 9639d59..3618fb8 100644 --- a/core/modules/comment/src/CommentStorage.php +++ b/core/modules/comment/src/CommentStorage.php @@ -321,9 +321,7 @@ public function loadThread(EntityInterface $entity, $field_name, $mode, $comment /** * {@inheritdoc} */ - public function getSchema() { - $schema = parent::getSchema(); - + public function customizeSchema(&$schema) { // Marking the respective fields as NOT NULL makes the indexes more // performant. $schema['comment_field_data']['fields']['created']['not null'] = TRUE; @@ -356,8 +354,6 @@ public function getSchema() { 'columns' => array('uid' => 'uid'), ), ); - - return $schema; } /** diff --git a/core/modules/contact/src/Tests/Views/ContactFieldsTest.php b/core/modules/contact/src/Tests/Views/ContactFieldsTest.php index 63c7d1a..a4afee7 100644 --- a/core/modules/contact/src/Tests/Views/ContactFieldsTest.php +++ b/core/modules/contact/src/Tests/Views/ContactFieldsTest.php @@ -7,7 +7,6 @@ namespace Drupal\contact\Tests\Views; -use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\views\Tests\ViewTestBase; /** @@ -60,7 +59,7 @@ protected function setUp() { public function testViewsData() { // Test that the field is not exposed to views, since contact_message // entities have no storage. - $table_name = ContentEntityDatabaseStorage::_fieldTableName($this->field_storage); + $table_name = 'contact_message__' . $this->field_storage->getName(); $data = $this->container->get('views.views_data')->get($table_name); $this->assertFalse($data, 'The field is not exposed to Views.'); } diff --git a/core/modules/contact/tests/modules/contact_storage_test/contact_storage_test.install b/core/modules/contact/tests/modules/contact_storage_test/contact_storage_test.install index bcbe5df..c749147 100644 --- a/core/modules/contact/tests/modules/contact_storage_test/contact_storage_test.install +++ b/core/modules/contact/tests/modules/contact_storage_test/contact_storage_test.install @@ -9,16 +9,10 @@ * Implements hook_install(). */ function contact_storage_test_install() { - // ModuleHandler won't create the schema automatically because Message entity - // belongs to contact.module. - // @todo Remove this when https://www.drupal.org/node/1498720 is in. + // Recreate the original entity type definition, so we can trigger the entity + // schema creation. $entity_manager = \Drupal::entityManager(); - $schema = \Drupal::database()->schema(); - $entity_type = $entity_manager->getDefinition('contact_message'); - $storage = $entity_manager->getStorage($entity_type->id()); - foreach ($storage->getSchema() as $table_name => $table_schema) { - if (!$schema->tableExists($table_name)) { - $schema->createTable($table_name, $table_schema); - } - } + $original = clone $entity_manager->getDefinition('contact_message'); + $original->setStorageClass('Drupal\Core\Entity\ContentEntityNullStorage'); + $entity_manager->getStorage($original->id())->onEntityTypeDefinitionUpdate($original); } diff --git a/core/modules/entity_reference/entity_reference.views.inc b/core/modules/entity_reference/entity_reference.views.inc index e35884b..0308415 100644 --- a/core/modules/entity_reference/entity_reference.views.inc +++ b/core/modules/entity_reference/entity_reference.views.inc @@ -5,7 +5,6 @@ * Provides views data for the entity_reference module. */ -use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\field\FieldStorageConfigInterface; /** @@ -14,6 +13,7 @@ function entity_reference_field_views_data(FieldStorageConfigInterface $field_storage) { $data = field_views_field_default_views_data($field_storage); $entity_manager = \Drupal::entityManager(); + $table_mapping = $entity_manager->getStorage($field_storage->getTargetEntityTypeId())->getTableMapping(); foreach ($data as $table_name => $table_data) { // Add a relationship to the target entity type. $target_entity_type_id = $field_storage->getSetting('target_type'); @@ -38,13 +38,16 @@ function entity_reference_field_views_data(FieldStorageConfigInterface $field_st // Provide a reverse relationship for the entity type that is referenced by // the field. - $pseudo_field_name = 'reverse__' . $field_storage->getTargetEntityTypeId() . '__' . $field_storage->getName(); + $entity_type_id = $field_storage->getTargetEntityTypeId(); + $pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_storage->getName(); + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = \Drupal::entityManager()->getStorage($entity_type_id)->getTableMapping(); $data[$target_base_table][$pseudo_field_name]['relationship'] = array( 'title' => t('@label using @field_name', $args), 'help' => t('Relate each @label with a @field_name.', $args), 'id' => 'entity_reverse', 'field_name' => $field_storage->getName(), - 'field table' => ContentEntityDatabaseStorage::_fieldTableName($field_storage), + 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), 'field field' => $field_storage->getName() . '_target_id', 'base' => $target_entity_type->getBaseTable(), 'base field' => $target_entity_type->getKey('id'), diff --git a/core/modules/field/field.views.inc b/core/modules/field/field.views.inc index 3f68419..95e360e 100644 --- a/core/modules/field/field.views.inc +++ b/core/modules/field/field.views.inc @@ -22,7 +22,7 @@ function field_views_data() { $module_handler = \Drupal::moduleHandler(); foreach (\Drupal::entityManager()->getStorage('field_storage_config')->loadMultiple() as $field) { - if (_field_views_is_sql_entity_type($field)) { + if (_field_views_get_entity_type_storage($field)) { $result = (array) $module_handler->invoke($field->module, 'field_views_data', array($field)); if (empty($result)) { $result = field_views_field_default_views_data($field); @@ -48,7 +48,7 @@ function field_views_data() { */ function field_views_data_alter(&$data) { foreach (\Drupal::entityManager()->getStorage('field_storage_config')->loadMultiple() as $field) { - if (_field_views_is_sql_entity_type($field)) { + if (_field_views_get_entity_type_storage($field)) { $function = $field->module . '_field_views_data_views_data_alter'; if (function_exists($function)) { $function($data, $field); @@ -63,12 +63,17 @@ function field_views_data_alter(&$data) { * @param \Drupal\field\FieldStorageConfigInterface $field_storage * The field storage definition. * - * @return bool - * True if the entity type uses ContentEntityDatabaseStorage. + * @return \Drupal\Core\Entity\ContentEntityDatabaseStorage + * Returns the entity type storage if supported. */ -function _field_views_is_sql_entity_type(FieldStorageConfigInterface $field_storage) { +function _field_views_get_entity_type_storage(FieldStorageConfigInterface $field_storage) { + $result = FALSE; $entity_manager = \Drupal::entityManager(); - return $entity_manager->hasDefinition($field_storage->entity_type) && $entity_manager->getStorage($field_storage->entity_type) instanceof ContentEntityDatabaseStorage; + if ($entity_manager->hasDefinition($field_storage->getTargetEntityTypeId())) { + $storage = $entity_manager->getStorage($field_storage->getTargetEntityTypeId()); + $result = $storage instanceof ContentEntityDatabaseStorage ? $storage : FALSE; + } + return $result; } /** @@ -120,6 +125,11 @@ function field_views_field_default_views_data(FieldStorageConfigInterface $field if (!$field_storage->getBundles()) { return $data; } + // Check whether the entity type storage is supported. + $storage = _field_views_get_entity_type_storage($field_storage); + if (!$storage) { + return $data; + } $field_name = $field_storage->getName(); $field_columns = $field_storage->getColumns(); @@ -127,7 +137,7 @@ function field_views_field_default_views_data(FieldStorageConfigInterface $field // Grab information about the entity type tables. // We need to join to both the base table and the data table, if available. $entity_manager = \Drupal::entityManager(); - $entity_type_id = $field_storage->entity_type; + $entity_type_id = $field_storage->getTargetEntityTypeId(); $entity_type = $entity_manager->getDefinition($entity_type_id); if (!$base_table = $entity_type->getBaseTable()) { // We cannot do anything if for some reason there is no base table. @@ -150,15 +160,18 @@ function field_views_field_default_views_data(FieldStorageConfigInterface $field } // Description of the field tables. + // @todo Generalize this code to make it work with any table layout. See + // https://drupal.org/node/2079019. + $table_mapping = $storage->getTableMapping(); $field_tables = array( EntityStorageInterface::FIELD_LOAD_CURRENT => array( - 'table' => ContentEntityDatabaseStorage::_fieldTableName($field_storage), + 'table' => $table_mapping->getDedicatedDataTableName($field_storage), 'alias' => "{$entity_type_id}__{$field_name}", ), ); if ($supports_revisions) { $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION] = array( - 'table' => ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), + 'table' => $table_mapping->getDedicatedRevisionTableName($field_storage), 'alias' => "{$entity_type_id}_revision__{$field_name}", ); } @@ -220,7 +233,7 @@ function field_views_field_default_views_data(FieldStorageConfigInterface $field // Build the list of additional fields to add to queries. $add_fields = array('delta', 'langcode', 'bundle'); foreach (array_keys($field_columns) as $column) { - $add_fields[] = ContentEntityDatabaseStorage::_fieldColumnName($field_storage, $column); + $add_fields[] = $table_mapping->getFieldColumnName($field_storage, $column); } // Determine the label to use for the field. We don't have a label available // at the field level, so we just go through all instances and take the one @@ -344,11 +357,10 @@ function field_views_field_default_views_data(FieldStorageConfigInterface $field else { $group = t('@group (historical data)', array('@group' => $group_name)); } - $column_real_name = ContentEntityDatabaseStorage::_fieldColumnName($field_storage, $column); + $column_real_name = $table_mapping->getFieldColumnName($field_storage, $column); // Load all the fields from the table by default. - $field_sql_schema = ContentEntityDatabaseStorage::_fieldSqlSchema($field_storage); - $additional_fields = array_keys($field_sql_schema[$table]['fields']); + $additional_fields = $table_mapping->getAllColumns($table); $data[$table_alias][$column_real_name] = array( 'group' => $group, diff --git a/core/modules/field/src/Plugin/views/field/Field.php b/core/modules/field/src/Plugin/views/field/Field.php index 04976b4..a42f4c2 100644 --- a/core/modules/field/src/Plugin/views/field/Field.php +++ b/core/modules/field/src/Plugin/views/field/Field.php @@ -9,7 +9,6 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\Xss; -use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityStorageInterface; @@ -353,9 +352,12 @@ public function clickSort($order) { } $this->ensureMyTable(); - $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($this->definition['entity_type']); + $entity_type_id = $this->definition['entity_type']; + $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id); $field_storage = $field_storage_definitions[$this->definition['field_name']]; - $column = ContentEntityDatabaseStorage::_fieldColumnName($field_storage, $this->options['click_sort_column']); + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = $this->entityManager->getStorage($entity_type_id)->getTableMapping(); + $column = $table_mapping->getFieldColumnName($field_storage, $this->options['click_sort_column']); if (!isset($this->aliases[$column])) { // Column is not in query; add a sort on it (without adding the column). $this->aliases[$column] = $this->tableAlias . '.' . $column; diff --git a/core/modules/field/src/Tests/BulkDeleteTest.php b/core/modules/field/src/Tests/BulkDeleteTest.php index ee099a8..d37d919 100644 --- a/core/modules/field/src/Tests/BulkDeleteTest.php +++ b/core/modules/field/src/Tests/BulkDeleteTest.php @@ -7,7 +7,6 @@ namespace Drupal\field\Tests; -use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Entity\FieldInstanceConfig; @@ -178,11 +177,13 @@ function testDeleteFieldInstance() { $this->assertEqual($instance->bundle, $bundle, 'The deleted instance is for the correct bundle'); // Check that the actual stored content did not change during delete. - $schema = ContentEntityDatabaseStorage::_fieldSqlSchema($field_storage); - $table = ContentEntityDatabaseStorage::_fieldTableName($field_storage); - $column = ContentEntityDatabaseStorage::_fieldColumnName($field_storage, 'value'); + $storage = \Drupal::entityManager()->getStorage($this->entity_type); + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = $storage->getTableMapping(); + $table = $table_mapping->getDedicatedDataTableName($field_storage); + $column = $table_mapping->getFieldColumnName($field_storage, 'value'); $result = db_select($table, 't') - ->fields('t', array_keys($schema[$table]['fields'])) + ->fields('t') ->execute(); foreach ($result as $row) { $this->assertEqual($this->entities[$row->entity_id]->{$field_storage->name}->value, $row->$column); diff --git a/core/modules/field/src/Tests/FieldDataCountTest.php b/core/modules/field/src/Tests/FieldDataCountTest.php index 1a57fc9..66e60db 100644 --- a/core/modules/field/src/Tests/FieldDataCountTest.php +++ b/core/modules/field/src/Tests/FieldDataCountTest.php @@ -76,7 +76,8 @@ public function testEntityCountAndHasData() { $storage = \Drupal::entityManager()->getStorage('entity_test'); if ($storage instanceof ContentEntityDatabaseStorage) { // Count the actual number of rows in the field table. - $field_table_name = $storage->_fieldTableName($field_storage); + $table_mapping = $storage->getTableMapping(); + $field_table_name = $table_mapping->getDedicatedDataTableName($field_storage); $result = db_select($field_table_name, 't') ->fields('t') ->countQuery() diff --git a/core/modules/field/src/Tests/Views/ApiDataTest.php b/core/modules/field/src/Tests/Views/ApiDataTest.php index 7631e29..4e755dc 100644 --- a/core/modules/field/src/Tests/Views/ApiDataTest.php +++ b/core/modules/field/src/Tests/Views/ApiDataTest.php @@ -6,7 +6,6 @@ */ namespace Drupal\field\Tests\Views; -use Drupal\Core\Entity\ContentEntityDatabaseStorage; /** * Tests the Field Views data. @@ -51,8 +50,10 @@ function testViewsData() { // Check the table and the joins of the first field. // Attached to node only. $field_storage = $this->fieldStorages[0]; - $current_table = ContentEntityDatabaseStorage::_fieldTableName($field_storage); - $revision_table = ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage); + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = \Drupal::entityManager()->getStorage('node')->getTableMapping(); + $current_table = $table_mapping->getDedicatedDataTableName($field_storage); + $revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage); $data[$current_table] = $views_data->get($current_table); $data[$revision_table] = $views_data->get($revision_table); diff --git a/core/modules/file/file.views.inc b/core/modules/file/file.views.inc index bfa3d15..ee523b7 100644 --- a/core/modules/file/file.views.inc +++ b/core/modules/file/file.views.inc @@ -5,7 +5,6 @@ * Provide views data for file.module. */ -use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\field\FieldStorageConfigInterface; /** @@ -39,9 +38,12 @@ function file_field_views_data(FieldStorageConfigInterface $field_storage) { */ function file_field_views_data_views_data_alter(array &$data, FieldStorageConfigInterface $field_storage) { $entity_type_id = $field_storage->entity_type; - $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); + $entity_manager = \Drupal::entityManager(); + $entity_type = $entity_manager->getDefinition($entity_type_id); $field_name = $field_storage->getName(); $pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id; + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping(); list($label) = field_views_field_label($entity_type_id, $field_name); @@ -51,7 +53,7 @@ function file_field_views_data_views_data_alter(array &$data, FieldStorageConfig 'id' => 'entity_reverse', 'field_name' => $field_name, 'entity_type' => $entity_type_id, - 'field table' => ContentEntityDatabaseStorage::_fieldTableName($field_storage), + 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), 'field field' => $field_name . '_target_id', 'base' => $entity_type->getBaseTable(), 'base field' => $entity_type->getKey('id'), diff --git a/core/modules/file/src/FileStorage.php b/core/modules/file/src/FileStorage.php index 93dae42..8da4cbc 100644 --- a/core/modules/file/src/FileStorage.php +++ b/core/modules/file/src/FileStorage.php @@ -30,9 +30,7 @@ public function spaceUsed($uid = NULL, $status = FILE_STATUS_PERMANENT) { /** * {@inheritdoc} */ - public function getSchema() { - $schema = parent::getSchema(); - + public function customizeSchema(&$schema) { // Marking the respective fields as NOT NULL makes the indexes more // performant. $schema['file_managed']['fields']['status']['not null'] = TRUE; @@ -48,8 +46,6 @@ public function getSchema() { $schema['file_managed']['unique keys'] += array( 'file__uri' => array('uri'), ); - - return $schema; } } diff --git a/core/modules/image/image.views.inc b/core/modules/image/image.views.inc index 5120350..2424b1a 100644 --- a/core/modules/image/image.views.inc +++ b/core/modules/image/image.views.inc @@ -5,7 +5,6 @@ * Provide views data for image.module. */ -use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\field\FieldStorageConfigInterface; /** @@ -37,10 +36,13 @@ function image_field_views_data(FieldStorageConfigInterface $field_storage) { * Views integration to provide reverse relationships on image fields. */ function image_field_views_data_views_data_alter(array &$data, FieldStorageConfigInterface $field_storage) { - $entity_type_id = $field_storage->entity_type; + $entity_type_id = $field_storage->getTargetEntityTypeId(); $field_name = $field_storage->getName(); - $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); + $entity_manager = \Drupal::entityManager(); + $entity_type = $entity_manager->getDefinition($entity_type_id); $pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id; + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping(); list($label) = field_views_field_label($entity_type_id, $field_name); @@ -50,7 +52,7 @@ function image_field_views_data_views_data_alter(array &$data, FieldStorageConfi 'id' => 'entity_reverse', 'field_name' => $field_name, 'entity_type' => $entity_type_id, - 'field table' => ContentEntityDatabaseStorage::_fieldTableName($field_storage), + 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), 'field field' => $field_name . '_target_id', 'base' => $entity_type->getBaseTable(), 'base field' => $entity_type->getKey('id'), diff --git a/core/modules/node/src/NodeStorage.php b/core/modules/node/src/NodeStorage.php index 86cf982..9e216a5 100644 --- a/core/modules/node/src/NodeStorage.php +++ b/core/modules/node/src/NodeStorage.php @@ -62,9 +62,7 @@ public function clearRevisionsLanguage($language) { /** * {@inheritdoc} */ - public function getSchema() { - $schema = parent::getSchema(); - + public function customizeSchema(&$schema) { // Marking the respective fields as NOT NULL makes the indexes more // performant. $schema['node_field_data']['fields']['changed']['not null'] = TRUE; @@ -101,8 +99,6 @@ public function getSchema() { 'node__default_langcode' => array('default_langcode'), 'node__langcode' => array('langcode'), ); - - return $schema; } } diff --git a/core/modules/simpletest/src/KernelTestBase.php b/core/modules/simpletest/src/KernelTestBase.php index 02331c8..c126ede 100644 --- a/core/modules/simpletest/src/KernelTestBase.php +++ b/core/modules/simpletest/src/KernelTestBase.php @@ -11,10 +11,10 @@ use Drupal\Core\Database\Database; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DrupalKernel; +use Drupal\Core\Entity\Sql\SqlEntityStorageInterface; use Drupal\Core\KeyValueStore\KeyValueMemoryFactory; use Drupal\Core\Language\Language; use Drupal\Core\Site\Settings; -use Drupal\Core\Entity\Schema\EntitySchemaProviderInterface; use Symfony\Component\DependencyInjection\Parameter; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpFoundation\Request; @@ -387,24 +387,14 @@ protected function installSchema($module, $tables) { protected function installEntitySchema($entity_type_id) { /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */ $entity_manager = $this->container->get('entity.manager'); - /** @var \Drupal\Core\Database\Schema $schema_handler */ - $schema_handler = $this->container->get('database')->schema(); - $storage = $entity_manager->getStorage($entity_type_id); - if ($storage instanceof EntitySchemaProviderInterface) { - $schema = $storage->getSchema(); - foreach ($schema as $table_name => $table_schema) { - $schema_handler->createTable($table_name, $table_schema); - } + $storage->onEntityTypeDefinitionCreate(); + if ($storage instanceof SqlEntityStorageInterface) { + $table_mapping = $storage->getTableMapping(); $this->pass(String::format('Installed entity type tables for the %entity_type entity type: %tables', array( '%entity_type' => $entity_type_id, - '%tables' => '{' . implode('}, {', array_keys($schema)) . '}', - ))); - } - else { - throw new \RuntimeException(String::format('Entity type %entity_type does not support automatic schema installation.', array( - '%entity-type' => $entity_type_id, + '%tables' => '{' . implode('}, {', $table_mapping->getTableNames()) . '}', ))); } } diff --git a/core/modules/system/src/Tests/Entity/EntityBundleFieldTest.php b/core/modules/system/src/Tests/Entity/EntityBundleFieldTest.php index 377312a..02c082e 100644 --- a/core/modules/system/src/Tests/Entity/EntityBundleFieldTest.php +++ b/core/modules/system/src/Tests/Entity/EntityBundleFieldTest.php @@ -40,54 +40,40 @@ protected function setUp() { } /** - * Tests the custom bundle field creation and deletion. - */ - public function testCustomBundleFieldCreateDelete() { - // Install the module which adds the field. - $this->moduleHandler->install(array('entity_bundle_field_test'), FALSE); - $definition = $this->entityManager->getFieldDefinitions('entity_test', 'custom')['custom_field']; - $this->assertNotNull($definition, 'Field definition found.'); - - // Make sure the table has been created. - $table = $this->entityManager->getStorage('entity_test')->_fieldTableName($definition); - $this->assertTrue($this->database->schema()->tableExists($table), 'Table created'); - $this->moduleHandler->uninstall(array('entity_bundle_field_test'), FALSE); - $this->assertFalse($this->database->schema()->tableExists($table), 'Table dropped'); - } - - /** * Tests making use of a custom bundle field. */ public function testCustomBundleFieldUsage() { // Check that an entity with bundle entity_test does not have the custom // field. - $this->moduleHandler->install(array('entity_bundle_field_test'), FALSE); + $this->moduleHandler->install(array('entity_schema_test'), FALSE); $storage = $this->entityManager->getStorage('entity_test'); $entity = $storage->create([ 'type' => 'entity_test', ]); - $this->assertFalse($entity->hasField('custom_field')); + $this->assertFalse($entity->hasField('custom_bundle_field')); // Check that the custom bundle has the defined custom field and check // saving and deleting of custom field data. $entity = $storage->create([ 'type' => 'custom', ]); - $this->assertTrue($entity->hasField('custom_field')); - $entity->custom_field->value = 'swanky'; + $this->assertTrue($entity->hasField('custom_bundle_field')); + $entity->custom_bundle_field->value = 'swanky'; $entity->save(); $storage->resetCache(); $entity = $storage->load($entity->id()); - $this->assertEqual($entity->custom_field->value, 'swanky', 'Entity was saved correct.y'); + $this->assertEqual($entity->custom_bundle_field->value, 'swanky', 'Entity was saved correct.y'); - $entity->custom_field->value = 'cozy'; + $entity->custom_bundle_field->value = 'cozy'; $entity->save(); $storage->resetCache(); $entity = $storage->load($entity->id()); - $this->assertEqual($entity->custom_field->value, 'cozy', 'Entity was updated correctly.'); + $this->assertEqual($entity->custom_bundle_field->value, 'cozy', 'Entity was updated correctly.'); $entity->delete(); - $table = $storage->_fieldTableName($entity->getFieldDefinition('custom_field')); + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = $storage->getTableMapping(); + $table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field')); $result = $this->database->select($table, 'f') ->fields('f') ->condition('f.entity_id', $entity->id()) @@ -96,11 +82,11 @@ public function testCustomBundleFieldUsage() { // Create another entity to test that values are marked as deleted when a // bundle is deleted. - $entity = $storage->create(['type' => 'custom', 'custom_field' => 'new']); + $entity = $storage->create(['type' => 'custom', 'custom_bundle_field' => 'new']); $entity->save(); entity_test_delete_bundle('custom'); - $table = $storage->_fieldTableName($entity->getFieldDefinition('custom_field')); + $table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field')); $result = $this->database->select($table, 'f') ->condition('f.entity_id', $entity->id()) ->condition('deleted', 1) @@ -108,7 +94,8 @@ public function testCustomBundleFieldUsage() { ->execute(); $this->assertEqual(1, $result->fetchField(), 'Field data has been deleted'); - // @todo Test field purge and table deletion once supported. + // @todo Test field purge and table deletion once supported. See + // https://www.drupal.org/node/2282119. // $this->assertFalse($this->database->schema()->tableExists($table), 'Custom field table was deleted'); } diff --git a/core/modules/system/src/Tests/Entity/EntitySchemaTest.php b/core/modules/system/src/Tests/Entity/EntitySchemaTest.php new file mode 100644 index 0000000..56fbacb --- /dev/null +++ b/core/modules/system/src/Tests/Entity/EntitySchemaTest.php @@ -0,0 +1,167 @@ + 'Entity Schema', + 'description' => 'Tests entity field schema API for base and bundle fields.', + 'group' => 'Entity API', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + parent::setUp(); + $this->installSchema('user', array('users_data')); + $this->installSchema('system', array('router')); + $this->moduleHandler = $this->container->get('module_handler'); + $this->database = $this->container->get('database'); + } + + /** + * Tests the custom bundle field creation and deletion. + */ + public function testCustomFieldCreateDelete() { + // Install the module which adds the field. + $this->installModule('entity_schema_test'); + $this->entityManager->clearCachedDefinitions(); + $definition = $this->entityManager->getBaseFieldDefinitions('entity_test')['custom_base_field']; + $this->assertNotNull($definition, 'Base field definition found.'); + $definition = $this->entityManager->getFieldDefinitions('entity_test', 'custom')['custom_bundle_field']; + $this->assertNotNull($definition, 'Bundle field definition found.'); + + // Make sure the field schema has been created. + /** @var \Drupal\Core\Entity\Sql\DefaultTableMappingInterface $table_mapping */ + $table_mapping = $this->entityManager->getStorage('entity_test')->getTableMapping(); + $base_table = current($table_mapping->getTableNames()); + $base_column = current($table_mapping->getColumnNames('custom_base_field')); + $this->assertTrue($this->database->schema()->fieldExists($base_table, $base_column), 'Table column created'); + + $table = $table_mapping->getDedicatedDataTableName($definition->getFieldStorageDefinition()); + $this->assertTrue($this->database->schema()->tableExists($table), 'Table created'); + $this->uninstallModule('entity_schema_test'); + $this->assertFalse($this->database->schema()->fieldExists($base_table, $base_column), 'Table column dropped'); + $this->assertFalse($this->database->schema()->tableExists($table), 'Table dropped'); + } + + /** + * Tests that entity schema responds to changes in the entity type definition. + */ + public function testEntitySchemaUpdate() { + $this->installModule('entity_schema_test'); + $schema_handler = $this->database->schema(); + $tables = array('entity_test', 'entity_test_revision', 'entity_test_field_data', 'entity_test_field_revision'); + $dedicated_tables = array('entity_test__custom_bundle_field', 'entity_test_revision__custom_bundle_field'); + + // Initially only the base table and the field data tables should exist. + foreach ($tables as $index => $table) { + $this->assertEqual($schema_handler->tableExists($table), !$index, String::format('Entity schema correct for the @table table.', array('@table' => $table))); + } + $this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), String::format('Field schema correct for the @table table.', array('@table' => $table))); + + // Update the entity type definition and check that the entity schema now + // supports translations and revisions. + $this->updateEntityType(TRUE); + foreach ($tables as $table) { + $this->assertTrue($schema_handler->tableExists($table), String::format('Entity schema correct for the @table table.', array('@table' => $table))); + } + foreach ($dedicated_tables as $table) { + $this->assertTrue($schema_handler->tableExists($table), String::format('Field schema correct for the @table table.', array('@table' => $table))); + } + + // Revert changes and check that the entity schema now does not support + // neither translations nor revisions. + $this->updateEntityType(FALSE); + foreach ($tables as $index => $table) { + $this->assertEqual($schema_handler->tableExists($table), !$index, String::format('Entity schema correct for the @table table.', array('@table' => $table))); + } + $this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), String::format('Field schema correct for the @table table.', array('@table' => $table))); + } + + /** + * Updates the entity type definition. + * + * @param bool $alter + * Whether the original definition should be altered or not. + */ + protected function updateEntityType($alter) { + $entity_test_id = 'entity_test'; + $original = $this->entityManager->getDefinition($entity_test_id); + $this->entityManager->clearCachedDefinitions(); + $this->state->set('entity_schema_update', $alter); + $this->entityManager->getStorage($entity_test_id)->onEntityTypeDefinitionUpdate($original); + } + + /** + * Installs a module and refreshes services. + * + * @param string $module + * The module to install. + */ + protected function installModule($module) { + $this->moduleHandler->install(array($module), FALSE); + $this->refreshServices(); + } + + /** + * Uninstalls a module and refreshes services. + * + * @param string $module + * The module to uninstall. + */ + protected function uninstallModule($module) { + $this->moduleHandler->uninstall(array($module), FALSE); + $this->refreshServices(); + } + + /** + * Refresh services. + */ + protected function refreshServices() { + $this->container = \Drupal::getContainer(); + $this->moduleHandler = $this->container->get('module_handler'); + $this->database = $this->container->get('database'); + $this->entityManager = $this->container->get('entity.manager'); + $this->state = $this->container->get('state'); + } + +} diff --git a/core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php b/core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php index 924164c..9c3a870 100644 --- a/core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php +++ b/core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php @@ -8,7 +8,6 @@ namespace Drupal\system\Tests\Entity; use Drupal\Core\Database\Database; -use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException; use Drupal\field\Entity\FieldStorageConfig; @@ -56,6 +55,8 @@ class FieldSqlStorageTest extends EntityUnitTestBase { protected $instance; /** + * Name of the data table of the field. + * * @var string */ protected $table; @@ -67,6 +68,13 @@ class FieldSqlStorageTest extends EntityUnitTestBase { */ protected $revision_table; + /** + * The table mapping for the tested entity type. + * + * @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping + */ + protected $table_mapping; + protected function setUp() { parent::setUp(); @@ -88,8 +96,11 @@ protected function setUp() { )); $this->instance->save(); - $this->table = ContentEntityDatabaseStorage::_fieldTableName($this->fieldStorage); - $this->revision_table = ContentEntityDatabaseStorage::_fieldRevisionTableName($this->fieldStorage); + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = \Drupal::entityManager()->getStorage($entity_type)->getTableMapping(); + $this->table_mapping = $table_mapping; + $this->table = $table_mapping->getDedicatedDataTableName($this->fieldStorage); + $this->revision_table = $table_mapping->getDedicatedRevisionTableName($this->fieldStorage); } /** @@ -99,7 +110,7 @@ function testFieldLoad() { $entity_type = $bundle = 'entity_test_rev'; $storage = $this->container->get('entity.manager')->getStorage($entity_type); - $columns = array('bundle', 'deleted', 'entity_id', 'revision_id', 'delta', 'langcode', ContentEntityDatabaseStorage::_fieldColumnName($this->fieldStorage, 'value')); + $columns = array('bundle', 'deleted', 'entity_id', 'revision_id', 'delta', 'langcode', $this->table_mapping->getFieldColumnName($this->fieldStorage, 'value')); // Create an entity with four revisions. $revision_ids = array(); @@ -337,7 +348,7 @@ function testFieldUpdateFailure() { // Create a text field. $field_storage = entity_create('field_storage_config', array( 'name' => 'test_text', - 'entity_type' => 'entity_test', + 'entity_type' => 'entity_test_rev', 'type' => 'text', 'settings' => array('max_length' => 255), )); @@ -355,7 +366,11 @@ function testFieldUpdateFailure() { } // Ensure that the field tables are still there. - foreach (ContentEntityDatabaseStorage::_fieldSqlSchema($prior_field_storage) as $table_name => $table_info) { + $tables = array( + $this->table_mapping->getDedicatedDataTableName($prior_field_storage), + $this->table_mapping->getDedicatedRevisionTableName($prior_field_storage), + ); + foreach ($tables as $table_name) { $this->assertTrue(db_table_exists($table_name), t('Table %table exists.', array('%table' => $table_name))); } } @@ -378,7 +393,7 @@ function testFieldUpdateIndexesWithData() { 'bundle' => $entity_type, )); $instance->save(); - $tables = array(ContentEntityDatabaseStorage::_fieldTableName($field_storage), ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage)); + $tables = array($this->table_mapping->getDedicatedDataTableName($field_storage), $this->table_mapping->getDedicatedRevisionTableName($field_storage)); // Verify the indexes we will create do not exist yet. foreach ($tables as $table) { @@ -444,19 +459,9 @@ function testFieldSqlStorageForeignKeys() { // Reload the field schema after the update. $schema = $field_storage->getSchema(); - // Retrieve the field definition and check that the foreign key is in place. - $field_storage = FieldStorageConfig::loadByName('entity_test', $field_name); + // Check that the foreign key is in place. $this->assertEqual($schema['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name modified after update'); $this->assertEqual($schema['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name modified after update'); - - // Verify the SQL schema. - $schemas = ContentEntityDatabaseStorage::_fieldSqlSchema($field_storage); - $schema = $schemas[ContentEntityDatabaseStorage::_fieldTableName($field_storage)]; - $this->assertEqual(count($schema['foreign keys']), 1, 'There is 1 foreign key in the schema'); - $foreign_key = reset($schema['foreign keys']); - $foreign_key_column = ContentEntityDatabaseStorage::_fieldColumnName($field_storage, $foreign_key_name); - $this->assertEqual($foreign_key['table'], $foreign_key_name, 'Foreign key table name preserved in the schema'); - $this->assertEqual($foreign_key['columns'][$foreign_key_column], 'id', 'Foreign key column name preserved in the schema'); } /** @@ -504,9 +509,9 @@ public function testTableNames() { 'type' => 'test_field', )); $expected = 'short_entity_type__short_field_name'; - $this->assertEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage), $expected); + $this->assertEqual($this->table_mapping->getDedicatedDataTableName($field_storage), $expected); $expected = 'short_entity_type_revision__short_field_name'; - $this->assertEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), $expected); + $this->assertEqual($this->table_mapping->getDedicatedRevisionTableName($field_storage), $expected); // Short entity type, long field name $entity_type = 'short_entity_type'; @@ -517,9 +522,9 @@ public function testTableNames() { 'type' => 'test_field', )); $expected = 'short_entity_type__' . substr(hash('sha256', $field_storage->uuid()), 0, 10); - $this->assertEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage), $expected); + $this->assertEqual($this->table_mapping->getDedicatedDataTableName($field_storage), $expected); $expected = 'short_entity_type_r__' . substr(hash('sha256', $field_storage->uuid()), 0, 10); - $this->assertEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), $expected); + $this->assertEqual($this->table_mapping->getDedicatedRevisionTableName($field_storage), $expected); // Long entity type, short field name $entity_type = 'long_entity_type_abcdefghijklmnopqrstuvwxyz'; @@ -530,9 +535,9 @@ public function testTableNames() { 'type' => 'test_field', )); $expected = 'long_entity_type_abcdefghijklmnopq__' . substr(hash('sha256', $field_storage->uuid()), 0, 10); - $this->assertEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage), $expected); + $this->assertEqual($this->table_mapping->getDedicatedDataTableName($field_storage), $expected); $expected = 'long_entity_type_abcdefghijklmnopq_r__' . substr(hash('sha256', $field_storage->uuid()), 0, 10); - $this->assertEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), $expected); + $this->assertEqual($this->table_mapping->getDedicatedRevisionTableName($field_storage), $expected); // Long entity type and field name. $entity_type = 'long_entity_type_abcdefghijklmnopqrstuvwxyz'; @@ -543,17 +548,17 @@ public function testTableNames() { 'type' => 'test_field', )); $expected = 'long_entity_type_abcdefghijklmnopq__' . substr(hash('sha256', $field_storage->uuid()), 0, 10); - $this->assertEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage), $expected); + $this->assertEqual($this->table_mapping->getDedicatedDataTableName($field_storage), $expected); $expected = 'long_entity_type_abcdefghijklmnopq_r__' . substr(hash('sha256', $field_storage->uuid()), 0, 10); - $this->assertEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), $expected); + $this->assertEqual($this->table_mapping->getDedicatedRevisionTableName($field_storage), $expected); // Try creating a second field and check there are no clashes. $field_storage2 = entity_create('field_storage_config', array( 'entity_type' => $entity_type, 'name' => $field_name . '2', 'type' => 'test_field', )); - $this->assertNotEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage), ContentEntityDatabaseStorage::_fieldTableName($field_storage2)); - $this->assertNotEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage2)); + $this->assertNotEqual($this->table_mapping->getDedicatedDataTableName($field_storage), $this->table_mapping->getDedicatedDataTableName($field_storage2)); + $this->assertNotEqual($this->table_mapping->getDedicatedRevisionTableName($field_storage), $this->table_mapping->getDedicatedRevisionTableName($field_storage2)); // Deleted field. $field_storage = entity_create('field_storage_config', array( @@ -563,9 +568,9 @@ public function testTableNames() { 'deleted' => TRUE, )); $expected = 'field_deleted_data_' . substr(hash('sha256', $field_storage->uuid()), 0, 10); - $this->assertEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage, TRUE), $expected); + $this->assertEqual($this->table_mapping->getDedicatedDataTableName($field_storage, TRUE), $expected); $expected = 'field_deleted_revision_' . substr(hash('sha256', $field_storage->uuid()), 0, 10); - $this->assertEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage, TRUE), $expected); + $this->assertEqual($this->table_mapping->getDedicatedRevisionTableName($field_storage, TRUE), $expected); } } diff --git a/core/modules/system/src/Tests/Entity/FieldTranslationSqlStorageTest.php b/core/modules/system/src/Tests/Entity/FieldTranslationSqlStorageTest.php index 511eeec..2874aed 100644 --- a/core/modules/system/src/Tests/Entity/FieldTranslationSqlStorageTest.php +++ b/core/modules/system/src/Tests/Entity/FieldTranslationSqlStorageTest.php @@ -8,7 +8,6 @@ namespace Drupal\system\Tests\Entity; use Drupal\Core\Entity\ContentEntityInterface; -use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\Core\Language\LanguageInterface; use Drupal\field\Entity\FieldStorageConfig; @@ -83,12 +82,14 @@ protected function assertFieldStorageLangcode(ContentEntityInterface $entity, $m $id = $entity->id(); $langcode = $entity->getUntranslated()->language()->id; $fields = array($this->field_name, $this->untranslatable_field_name); + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = \Drupal::entityManager()->getStorage($entity_type)->getTableMapping(); foreach ($fields as $field_name) { $field_storage = FieldStorageConfig::loadByName($entity_type, $field_name); $tables = array( - ContentEntityDatabaseStorage::_fieldTableName($field_storage), - ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), + $table_mapping->getDedicatedDataTableName($field_storage), + $table_mapping->getDedicatedRevisionTableName($field_storage), ); foreach ($tables as $table) { diff --git a/core/modules/system/tests/modules/entity_bundle_field_test/entity_bundle_field_test.info.yml b/core/modules/system/tests/modules/entity_bundle_field_test/entity_bundle_field_test.info.yml deleted file mode 100644 index 6732090..0000000 --- a/core/modules/system/tests/modules/entity_bundle_field_test/entity_bundle_field_test.info.yml +++ /dev/null @@ -1,8 +0,0 @@ -name: 'Entity bundle field test module' -type: module -description: 'Provides a bundle field to the test entity.' -package: Testing -version: VERSION -core: 8.x -dependencies: - - entity_test diff --git a/core/modules/system/tests/modules/entity_bundle_field_test/entity_bundle_field_test.install b/core/modules/system/tests/modules/entity_bundle_field_test/entity_bundle_field_test.install deleted file mode 100644 index 6065425..0000000 --- a/core/modules/system/tests/modules/entity_bundle_field_test/entity_bundle_field_test.install +++ /dev/null @@ -1,41 +0,0 @@ -getFieldStorageDefinitions('entity_test')['custom_field']; - $manager->getStorage('entity_test')->onFieldStorageDefinitionCreate($definition); - - // Create the custom bundle and put our bundle field on it. - entity_test_create_bundle('custom'); - $definition = $manager->getFieldDefinitions('entity_test', 'custom')['custom_field']; - $manager->getStorage('entity_test')->onFieldDefinitionCreate($definition); -} - -/** - * Implements hook_uninstall(). - */ -function entity_bundle_field_test_uninstall() { - entity_bundle_field_test_is_uninstalling(TRUE); - $manager = \Drupal::entityManager(); - // Notify the entity storage that our field is gone. - $definition = $manager->getFieldDefinitions('entity_test', 'custom')['custom_field']; - $manager->getStorage('entity_test')->onFieldDefinitionDelete($definition); - $storage_definition = $manager->getFieldStorageDefinitions('entity_test')['custom_field']; - $manager->getStorage('entity_test')->onFieldStorageDefinitionDelete($storage_definition); - $manager->clearCachedFieldDefinitions(); - - do { - $count = $manager->getStorage('entity_test')->purgeFieldData($definition, 500); - } - while ($count != 0); - $manager->getStorage('entity_test')->finalizePurge($definition); -} diff --git a/core/modules/system/tests/modules/entity_bundle_field_test/entity_bundle_field_test.module b/core/modules/system/tests/modules/entity_bundle_field_test/entity_bundle_field_test.module deleted file mode 100644 index 7a39717..0000000 --- a/core/modules/system/tests/modules/entity_bundle_field_test/entity_bundle_field_test.module +++ /dev/null @@ -1,69 +0,0 @@ -id() == 'entity_test' && !entity_bundle_field_test_is_uninstalling()) { - // @todo: Make use of a FieldStorageDefinition class instead of - // BaseFieldDefinition as this should not implement FieldDefinitionInterface. - // See https://drupal.org/node/2280639. - $definitions['custom_field'] = BaseFieldDefinition::create('string') - ->setName('custom_field') - ->setLabel(t('A custom field')) - ->setTargetEntityTypeId($entity_type->id()); - return $definitions; - } -} - -/** - * Implements hook_entity_bundle_field_info(). - */ -function entity_bundle_field_test_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { - if ($entity_type->id() == 'entity_test' && $bundle == 'custom' && !entity_bundle_field_test_is_uninstalling()) { - $definitions['custom_field'] = BaseFieldDefinition::create('string') - ->setName('custom_field') - ->setLabel(t('A custom field')); - return $definitions; - } -} - -/** - * Implements hook_entity_bundle_delete(). - */ -function entity_bundle_field_test_entity_bundle_delete($entity_type_id, $bundle) { - if ($entity_type_id == 'entity_test' && $bundle == 'custom') { - // Notify the entity storage that our field is gone. - $field_definition = BaseFieldDefinition::create('string') - ->setTargetEntityTypeId($entity_type_id) - ->setBundle($bundle) - ->setName('custom_field') - ->setLabel(t('A custom field')); - \Drupal::entityManager()->getStorage('entity_test') - ->onFieldDefinitionDelete($field_definition); - } -} diff --git a/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.info.yml b/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.info.yml new file mode 100644 index 0000000..646717d --- /dev/null +++ b/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.info.yml @@ -0,0 +1,8 @@ +name: 'Entity schema test module' +type: module +description: 'Provides entity and field definitions to test entity schema.' +package: Testing +version: VERSION +core: 8.x +dependencies: + - entity_test diff --git a/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.install b/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.install new file mode 100644 index 0000000..072cd3b --- /dev/null +++ b/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.install @@ -0,0 +1,54 @@ +getStorage('entity_test'); + + // Notify the entity storage of our custom base field. + $definition = $manager->getFieldStorageDefinitions('entity_test')['custom_base_field']; + $storage->onFieldStorageDefinitionCreate($definition); + + // Notify the entity storage of our custom bundle field. + $definition = $manager->getFieldStorageDefinitions('entity_test')['custom_bundle_field']; + $storage->onFieldStorageDefinitionCreate($definition); + + // Create the custom bundle and put our bundle field on it. + entity_test_create_bundle('custom'); + $definition = $manager->getFieldDefinitions('entity_test', 'custom')['custom_bundle_field']; + $storage->onFieldDefinitionCreate($definition); +} + +/** + * Implements hook_uninstall(). + */ +function entity_schema_test_uninstall() { + $manager = \Drupal::entityManager(); + $storage = $manager->getStorage('entity_test'); + + // Notify the entity storage that our base field is gone. + $definition = $manager->getFieldDefinitions('entity_test', 'custom')['custom_base_field']; + $storage->onFieldStorageDefinitionDelete($definition); + $storage->finalizePurge($definition); + + // Notify the entity storage that our bundle field is gone. + $definition = $manager->getFieldDefinitions('entity_test', 'custom')['custom_bundle_field']; + $storage->onFieldDefinitionDelete($definition); + do { + $count = $storage->purgeFieldData($definition, 500); + } + while ($count != 0); + + $storage_definition = $manager->getFieldStorageDefinitions('entity_test')['custom_bundle_field']; + $storage->onFieldStorageDefinitionDelete($storage_definition); + $storage->finalizePurge($storage_definition); + + $manager->clearCachedFieldDefinitions(); +} diff --git a/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.module b/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.module new file mode 100644 index 0000000..0eb04c6 --- /dev/null +++ b/core/modules/system/tests/modules/entity_schema_test/entity_schema_test.module @@ -0,0 +1,83 @@ +get('entity_schema_update')) { + $entity_type = $entity_types['entity_test']; + $entity_type->set('translatable', TRUE); + $entity_type->set('data_table', 'entity_test_field_data'); + $keys = $entity_type->getKeys(); + $keys['revision'] = 'revision_id'; + $entity_type->set('entity_keys', $keys); + } +} + +/** + * Implements hook_entity_base_field_info(). + */ +function entity_schema_test_entity_base_field_info(EntityTypeInterface $entity_type) { + if ($entity_type->id() == 'entity_test') { + $definitions['custom_base_field'] = BaseFieldDefinition::create('string') + ->setName('custom_base_field') + ->setLabel(t('A custom base field')); + if (\Drupal::state()->get('entity_schema_update')) { + $definitions += EntityTestMulRev::baseFieldDefinitions($entity_type); + } + return $definitions; + } +} + +/** + * Implements hook_entity_field_storage_info(). + */ +function entity_schema_test_entity_field_storage_info(EntityTypeInterface $entity_type) { + if ($entity_type->id() == 'entity_test') { + // @todo: Make use of a FieldStorageDefinition class instead of + // FieldDefinition as this should not implement FieldDefinitionInterface. + // See https://drupal.org/node/2280639. + $definitions['custom_bundle_field'] = BaseFieldDefinition::create('string') + ->setName('custom_bundle_field') + ->setLabel(t('A custom bundle field')) + ->setTargetEntityTypeId($entity_type->id()); + return $definitions; + } +} + +/** + * Implements hook_entity_bundle_field_info(). + */ +function entity_schema_test_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle) { + if ($entity_type->id() == 'entity_test' && $bundle == 'custom') { + $definitions['custom_bundle_field'] = BaseFieldDefinition::create('string') + ->setName('custom_bundle_field') + ->setLabel(t('A custom bundle field')); + return $definitions; + } +} + +/** + * Implements hook_entity_bundle_delete(). + */ +function entity_schema_test_entity_bundle_delete($entity_type_id, $bundle) { + if ($entity_type_id == 'entity_test' && $bundle == 'custom') { + $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); + $field_definitions = entity_schema_test_entity_bundle_field_info($entity_type, $bundle); + $field_definitions['custom_bundle_field'] + ->setTargetEntityTypeId($entity_type_id) + ->setBundle($bundle); + // Notify the entity storage that our field is gone. + \Drupal::entityManager()->getStorage($entity_type_id) + ->onFieldDefinitionDelete($field_definitions['custom_bundle_field']); + } +} diff --git a/core/modules/taxonomy/src/TermStorage.php b/core/modules/taxonomy/src/TermStorage.php index 345921c..38472ba 100644 --- a/core/modules/taxonomy/src/TermStorage.php +++ b/core/modules/taxonomy/src/TermStorage.php @@ -157,9 +157,7 @@ public function resetWeights($vid) { /** * {@inheritdoc} */ - public function getSchema() { - $schema = parent::getSchema(); - + public function customizeSchema(&$schema) { // Marking the respective fields as NOT NULL makes the indexes more // performant. $schema['taxonomy_term_field_data']['fields']['weight']['not null'] = TRUE; @@ -249,8 +247,6 @@ public function getSchema() { ), ), ); - - return $schema; } /** diff --git a/core/modules/taxonomy/taxonomy.views.inc b/core/modules/taxonomy/taxonomy.views.inc index 383075f..4de1876 100644 --- a/core/modules/taxonomy/taxonomy.views.inc +++ b/core/modules/taxonomy/taxonomy.views.inc @@ -5,7 +5,6 @@ * Provides views data for taxonomy.module. */ -use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\field\FieldStorageConfigInterface; /** @@ -92,9 +91,12 @@ function taxonomy_field_views_data(FieldStorageConfigInterface $field_storage) { */ function taxonomy_field_views_data_views_data_alter(array &$data, FieldStorageConfigInterface $field_storage) { $field_name = $field_storage->getName(); - $entity_type_id = $field_storage->entity_type; - $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); + $entity_type_id = $field_storage->getTargetEntityTypeId(); + $entity_manager = \Drupal::entityManager(); + $entity_type = $entity_manager->getDefinition($entity_type_id); $pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id; + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ + $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping(); list($label) = field_views_field_label($entity_type_id, $field_name); @@ -104,7 +106,7 @@ function taxonomy_field_views_data_views_data_alter(array &$data, FieldStorageCo 'id' => 'entity_reverse', 'field_name' => $field_name, 'entity_type' => $entity_type_id, - 'field table' => ContentEntityDatabaseStorage::_fieldTableName($field_storage), + 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), 'field field' => $field_name . '_target_id', 'base' => $entity_type->getBaseTable(), 'base field' => $entity_type->getKey('id'), diff --git a/core/modules/user/src/UserStorage.php b/core/modules/user/src/UserStorage.php index 65209b8..7d039c3 100644 --- a/core/modules/user/src/UserStorage.php +++ b/core/modules/user/src/UserStorage.php @@ -171,9 +171,7 @@ public function updateLastAccessTimestamp(AccountInterface $account, $timestamp) /** * {@inheritdoc} */ - public function getSchema() { - $schema = parent::getSchema(); - + public function customizeSchema(&$schema) { // The "users" table does not use serial identifiers. $schema['users']['fields']['uid']['type'] = 'int'; @@ -220,8 +218,6 @@ public function getSchema() { ), ), ); - - return $schema; } } diff --git a/core/modules/views/views.api.php b/core/modules/views/views.api.php index e5c8c50..a54d562 100644 --- a/core/modules/views/views.api.php +++ b/core/modules/views/views.api.php @@ -474,6 +474,7 @@ function hook_field_views_data_alter(array &$data, \Drupal\field\FieldStorageCon $field_name = $field_storage->getName(); $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); $pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id; + $table_mapping = \Drupal::entityManager()->getStorage($entity_type_id)->getTableMapping(); list($label) = field_views_field_label($entity_type_id, $field_name); @@ -483,7 +484,7 @@ function hook_field_views_data_alter(array &$data, \Drupal\field\FieldStorageCon 'id' => 'entity_reverse', 'field_name' => $field_name, 'entity_type' => $entity_type_id, - 'field table' => ContentEntityDatabaseStorage::_fieldTableName($field_storage), + 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), 'field field' => $field_name . '_target_id', 'base' => $entity_type->getBaseTable(), 'base field' => $entity_type->getKey('id'), @@ -531,6 +532,7 @@ function hook_field_views_data_views_data_alter(array &$data, \Drupal\field\Fiel $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); $pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id; list($label) = field_views_field_label($entity_type_id, $field_name); + $table_mapping = \Drupal::entityManager()->getStorage($entity_type_id)->getTableMapping(); // Views data for this field is in $data[$data_key]. $data[$data_key][$pseudo_field_name]['relationship'] = array( @@ -539,7 +541,7 @@ function hook_field_views_data_views_data_alter(array &$data, \Drupal\field\Fiel 'id' => 'entity_reverse', 'field_name' => $field_name, 'entity_type' => $entity_type_id, - 'field table' => ContentEntityDatabaseStorage::_fieldTableName($field), + 'field table' => $table_mapping->getDedicatedDataTableName($field), 'field field' => $field_name . '_target_id', 'base' => $entity_type->getBaseTable(), 'base field' => $entity_type->getKey('id'), diff --git a/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php index fde0d84..bbffd9f 100644 --- a/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php @@ -11,6 +11,7 @@ use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Entity\Schema\ContentEntitySchemaHandler; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Language\Language; use Drupal\Tests\UnitTestCase; @@ -266,21 +267,23 @@ public function providerTestGetRevisionDataTable() { } /** - * Tests ContentEntityDatabaseStorage::getSchema(). + * Tests ContentEntityDatabaseStorage::onEntityTypeDefinitionCreate(). * * @covers ::__construct() - * @covers ::getSchema() - * @covers ::schemaHandler() + * @covers ::onEntityTypeDefinitionCreate() * @covers ::getTableMapping() */ - public function testGetSchema() { + public function testOnEntityDefinitionCreate() { $columns = array( 'value' => array( 'type' => 'int', ), ); - $this->fieldDefinitions['id'] = $this->getMock('Drupal\Core\Field\FieldStorageDefinitionInterface'); + $this->fieldDefinitions['id'] = $this->getMock('Drupal\Tests\Core\Field\TestBaseFieldDefinitionInterface'); + $this->fieldDefinitions['id']->expects($this->any()) + ->method('getName') + ->will($this->returnValue('id')); $this->fieldDefinitions['id']->expects($this->once()) ->method('getColumns') ->will($this->returnValue($columns)); @@ -305,35 +308,55 @@ public function testGetSchema() { array('id' => 'id'), ))); - $this->entityManager->expects($this->once()) - ->method('getFieldStorageDefinitions') - ->with($this->entityType->id()) - ->will($this->returnValue($this->fieldDefinitions)); - $this->setUpEntityStorage(); $expected = array( - 'entity_test' => array( - 'description' => 'The base table for entity_test entities.', - 'fields' => array( - 'id' => array( - 'type' => 'serial', - 'description' => NULL, - 'not null' => TRUE, - ), + 'description' => 'The base table for entity_test entities.', + 'fields' => array( + 'id' => array( + 'type' => 'serial', + 'description' => NULL, + 'not null' => TRUE, ), - 'primary key' => array('id'), - 'unique keys' => array(), - 'indexes' => array(), - 'foreign keys' => array(), ), + 'primary key' => array('id'), + 'unique keys' => array(), + 'indexes' => array(), + 'foreign keys' => array(), ); - $this->assertEquals($expected, $this->entityStorage->getSchema()); - // Test that repeated calls do not result in repeatedly instantiating - // ContentEntitySchemaHandler as getFieldStorageDefinitions() is only - // expected to be called once. - $this->assertEquals($expected, $this->entityStorage->getSchema()); + $schema_handler = $this->getMockBuilder('Drupal\Core\Database\Schema') + ->disableOriginalConstructor() + ->getMock(); + $schema_handler->expects($this->any()) + ->method('createTable') + ->with($this->equalTo('entity_test'), $this->equalTo($expected)); + + $this->connection->expects($this->once()) + ->method('schema') + ->will($this->returnValue($schema_handler)); + + $storage = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityDatabaseStorage') + ->setConstructorArgs(array($this->entityType, $this->connection, $this->entityManager, $this->cache)) + ->setMethods(array('schemaHandler')) + ->getMock(); + + $state = $this->getMock('Drupal\Core\State\StateInterface'); + $schema_handler = $this->getMockBuilder('Drupal\Core\Entity\Schema\ContentEntitySchemaHandler') + ->setConstructorArgs(array($this->entityManager, $this->entityType, $storage, $this->connection)) + ->setMethods(array('state')) + ->getMock(); + $schema_handler + ->expects($this->any()) + ->method('state') + ->will($this->returnValue($state)); + + $storage + ->expects($this->any()) + ->method('schemaHandler') + ->will($this->returnValue($schema_handler)); + + $storage->onEntityTypeDefinitionCreate(); } /** @@ -397,18 +420,7 @@ public function testGetTableMappingSimple(array $entity_keys) { public function testGetTableMappingSimpleWithFields(array $entity_keys) { $base_field_names = array('title', 'description', 'owner'); $field_names = array_merge(array_values(array_filter($entity_keys)), $base_field_names); - - $definition = $this->getMock('Drupal\Core\Field\FieldStorageDefinitionInterface'); - $this->fieldDefinitions = array_fill_keys($field_names, $definition); - - $this->entityType->expects($this->any()) - ->method('getKey') - ->will($this->returnValueMap(array( - array('id', $entity_keys['id']), - array('uuid', $entity_keys['uuid']), - array('bundle', $entity_keys['bundle']), - ))); - + $this->fieldDefinitions = $this->mockFieldDefinitions($field_names); $this->setUpEntityStorage(); $mapping = $this->entityStorage->getTableMapping(); @@ -533,25 +545,11 @@ public function testGetTableMappingRevisionableWithFields(array $entity_keys) { $base_field_names = array('title'); $field_names = array_merge(array_values(array_filter($entity_keys)), $base_field_names); - - $definition = $this->getMock('Drupal\Core\Field\FieldStorageDefinitionInterface'); - $this->fieldDefinitions = array_fill_keys($field_names, $definition); + $this->fieldDefinitions = $this->mockFieldDefinitions($field_names); $revisionable_field_names = array('description', 'owner'); - $definition = $this->getMock('Drupal\Core\Field\FieldStorageDefinitionInterface'); - // isRevisionable() is only called once, but we re-use the same definition - // for all revisionable fields. - $definition->expects($this->any()) - ->method('isRevisionable') - ->will($this->returnValue(TRUE)); - $field_names = array_merge( - $field_names, - $revisionable_field_names - ); - $this->fieldDefinitions += array_fill_keys( - array_merge($revisionable_field_names, $revision_metadata_field_names), - $definition - ); + $field_names = array_merge($field_names, $revisionable_field_names); + $this->fieldDefinitions += $this->mockFieldDefinitions(array_merge($revisionable_field_names, $revision_metadata_field_names), array('isRevisionable' => TRUE)); $this->entityType->expects($this->exactly(2)) ->method('isRevisionable') @@ -658,9 +656,7 @@ public function testGetTableMappingTranslatableWithFields(array $entity_keys) { $base_field_names = array('title', 'description', 'owner'); $field_names = array_merge(array_values(array_filter($entity_keys)), $base_field_names); - - $definition = $this->getMock('Drupal\Core\Field\FieldStorageDefinitionInterface'); - $this->fieldDefinitions = array_fill_keys($field_names, $definition); + $this->fieldDefinitions = $this->mockFieldDefinitions($field_names); $this->entityType->expects($this->exactly(2)) ->method('isTranslatable') @@ -840,21 +836,10 @@ public function testGetTableMappingRevisionableTranslatableWithFields(array $ent $base_field_names = array('title'); $field_names = array_merge(array_values(array_filter($entity_keys)), $base_field_names); - - $definition = $this->getMock('Drupal\Core\Field\FieldStorageDefinitionInterface'); - $this->fieldDefinitions = array_fill_keys($field_names, $definition); + $this->fieldDefinitions = $this->mockFieldDefinitions($field_names); $revisionable_field_names = array('description', 'owner'); - $definition = $this->getMock('Drupal\Core\Field\FieldStorageDefinitionInterface'); - // isRevisionable() is only called once, but we re-use the same definition - // for all revisionable fields. - $definition->expects($this->any()) - ->method('isRevisionable') - ->will($this->returnValue(TRUE)); - $this->fieldDefinitions += array_fill_keys( - array_merge($revisionable_field_names, $revision_metadata_field_names), - $definition - ); + $this->fieldDefinitions += $this->mockFieldDefinitions(array_merge($revisionable_field_names, $revision_metadata_field_names), array('isRevisionable' => TRUE)); $this->entityType->expects($this->exactly(2)) ->method('isRevisionable') @@ -1078,9 +1063,56 @@ public function testCreate() { } /** + * Returns a set of mock field definitions for the given names. + * + * @param array $field_names + * An array of field names. + * @param array $methods + * (optional) An associative array of mock method return values keyed by + * method name. + * + * @return \Drupal\Core\Field\FieldDefinition[]|\PHPUnit_Framework_MockObject_MockObject[] + * An array of mock field definitions. + */ + protected function mockFieldDefinitions(array $field_names, $methods = array()) { + $field_definitions = array(); + $definition = $this->getMock('Drupal\Tests\Core\Field\TestBaseFieldDefinitionInterface'); + + // Assign common method return values. + foreach ($methods as $method => $result) { + $definition + ->expects($this->any()) + ->method($method) + ->will($this->returnValue($result)); + } + + // Assign field names to mock definitions. + foreach ($field_names as $field_name) { + $field_definitions[$field_name] = clone $definition; + $field_definitions[$field_name] + ->expects($this->any()) + ->method('getName') + ->will($this->returnValue($field_name)); + } + + return $field_definitions; + } + + /** * Sets up the content entity database storage. */ protected function setUpEntityStorage() { + $this->connection = $this->getMockBuilder('Drupal\Core\Database\Connection') + ->disableOriginalConstructor() + ->getMock(); + + $this->entityManager->expects($this->any()) + ->method('getDefinition') + ->will($this->returnValue($this->entityType)); + + $this->entityManager->expects($this->any()) + ->method('getFieldStorageDefinitions') + ->will($this->returnValue($this->fieldDefinitions)); $this->entityManager->expects($this->any()) ->method('getBaseFieldDefinitions') @@ -1223,7 +1255,6 @@ public function testLoadMultiplePersistentCacheMiss() { $entities = $entity_storage->loadMultiple(array($id)); $this->assertEquals($entity, $entities[$id]); - } /** @@ -1239,6 +1270,7 @@ protected function setUpModuleHandlerNoImplementations() { $this->container->set('module_handler', $this->moduleHandler); } + } /** diff --git a/core/tests/Drupal/Tests/Core/Entity/Schema/ContentEntitySchemaHandlerTest.php b/core/tests/Drupal/Tests/Core/Entity/Schema/ContentEntitySchemaHandlerTest.php index 0a4afe7..384132c 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Schema/ContentEntitySchemaHandlerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Schema/ContentEntitySchemaHandlerTest.php @@ -82,7 +82,7 @@ protected function setUp() { * * @covers ::__construct() * @covers ::getSchema() - * @covers ::getTables() + * @covers ::getEntitySchemaTables() * @covers ::initializeBaseTable() * @covers ::addTableDefaults() * @covers ::getEntityIndexName() @@ -246,16 +246,6 @@ public function testGetSchemaBase() { ), )); - $this->setUpSchemaHandler(); - - $table_mapping = new DefaultTableMapping($this->storageDefinitions); - $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions)); - $table_mapping->setExtraColumns('entity_test', array('default_langcode')); - - $this->storage->expects($this->once()) - ->method('getTableMapping') - ->will($this->returnValue($table_mapping)); - $expected = array( 'entity_test' => array( 'description' => 'The base table for entity_test entities.', @@ -381,9 +371,18 @@ public function testGetSchemaBase() { ), ), ); - $actual = $this->schemaHandler->getSchema(); - $this->assertEquals($expected, $actual); + $this->setUpEntitySchemaHandler($expected); + + $table_mapping = new DefaultTableMapping($this->storageDefinitions); + $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions)); + $table_mapping->setExtraColumns('entity_test', array('default_langcode')); + + $this->storage->expects($this->any()) + ->method('getTableMapping') + ->will($this->returnValue($table_mapping)); + + $this->schemaHandler->createEntitySchema($this->entityType); } /** @@ -391,7 +390,7 @@ public function testGetSchemaBase() { * * @covers ::__construct() * @covers ::getSchema() - * @covers ::getTables() + * @covers ::getEntitySchemaTables() * @covers ::initializeBaseTable() * @covers ::initializeRevisionTable() * @covers ::addTableDefaults() @@ -420,16 +419,6 @@ public function testGetSchemaRevisionable() { ), )); - $this->setUpSchemaHandler(); - - $table_mapping = new DefaultTableMapping($this->storageDefinitions); - $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions)); - $table_mapping->setFieldNames('entity_test_revision', array_keys($this->storageDefinitions)); - - $this->storage->expects($this->once()) - ->method('getTableMapping') - ->will($this->returnValue($table_mapping)); - $expected = array( 'entity_test' => array( 'description' => 'The base table for entity_test entities.', @@ -483,9 +472,17 @@ public function testGetSchemaRevisionable() { ), ); - $actual = $this->schemaHandler->getSchema(); + $this->setUpEntitySchemaHandler($expected); + + $table_mapping = new DefaultTableMapping($this->storageDefinitions); + $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions)); + $table_mapping->setFieldNames('entity_test_revision', array_keys($this->storageDefinitions)); + + $this->storage->expects($this->any()) + ->method('getTableMapping') + ->will($this->returnValue($table_mapping)); - $this->assertEquals($expected, $actual); + $this->schemaHandler->createEntitySchema($this->entityType); } /** @@ -493,7 +490,7 @@ public function testGetSchemaRevisionable() { * * @covers ::__construct() * @covers ::getSchema() - * @covers ::getTables() + * @covers ::getEntitySchemaTables() * @covers ::initializeDataTable() * @covers ::addTableDefaults() * @covers ::getEntityIndexName() @@ -507,7 +504,7 @@ public function testGetSchemaTranslatable() { ), )); - $this->storage->expects($this->once()) + $this->storage->expects($this->any()) ->method('getDataTable') ->will($this->returnValue('entity_test_field_data')); @@ -519,16 +516,6 @@ public function testGetSchemaTranslatable() { ), )); - $this->setUpSchemaHandler(); - - $table_mapping = new DefaultTableMapping($this->storageDefinitions); - $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions)); - $table_mapping->setFieldNames('entity_test_field_data', array_keys($this->storageDefinitions)); - - $this->storage->expects($this->once()) - ->method('getTableMapping') - ->will($this->returnValue($table_mapping)); - $expected = array( 'entity_test' => array( 'description' => 'The base table for entity_test entities.', @@ -575,9 +562,17 @@ public function testGetSchemaTranslatable() { ), ); - $actual = $this->schemaHandler->getSchema(); + $this->setUpEntitySchemaHandler($expected); + + $table_mapping = new DefaultTableMapping($this->storageDefinitions); + $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions)); + $table_mapping->setFieldNames('entity_test_field_data', array_keys($this->storageDefinitions)); + + $this->storage->expects($this->any()) + ->method('getTableMapping') + ->will($this->returnValue($table_mapping)); - $this->assertEquals($expected, $actual); + $this->schemaHandler->createEntitySchema($this->entityType); } /** @@ -585,7 +580,7 @@ public function testGetSchemaTranslatable() { * * @covers ::__construct() * @covers ::getSchema() - * @covers ::getTables() + * @covers ::getEntitySchemaTables() * @covers ::initializeDataTable() * @covers ::addTableDefaults() * @covers ::getEntityIndexName() @@ -626,18 +621,6 @@ public function testGetSchemaRevisionableTranslatable() { ), )); - $this->setUpSchemaHandler(); - - $table_mapping = new DefaultTableMapping($this->storageDefinitions); - $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions)); - $table_mapping->setFieldNames('entity_test_revision', array_keys($this->storageDefinitions)); - $table_mapping->setFieldNames('entity_test_field_data', array_keys($this->storageDefinitions)); - $table_mapping->setFieldNames('entity_test_revision_field_data', array_keys($this->storageDefinitions)); - - $this->storage->expects($this->once()) - ->method('getTableMapping') - ->will($this->returnValue($table_mapping)); - $expected = array( 'entity_test' => array( 'description' => 'The base table for entity_test entities.', @@ -763,26 +746,71 @@ public function testGetSchemaRevisionableTranslatable() { ), ); - $actual = $this->schemaHandler->getSchema(); + $this->setUpEntitySchemaHandler($expected); - $this->assertEquals($expected, $actual); + $table_mapping = new DefaultTableMapping($this->storageDefinitions); + $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions)); + $table_mapping->setFieldNames('entity_test_revision', array_keys($this->storageDefinitions)); + $table_mapping->setFieldNames('entity_test_field_data', array_keys($this->storageDefinitions)); + $table_mapping->setFieldNames('entity_test_revision_field_data', array_keys($this->storageDefinitions)); + + $this->storage->expects($this->any()) + ->method('getTableMapping') + ->will($this->returnValue($table_mapping)); + + $this->schemaHandler->createEntitySchema($this->entityType); } /** * Sets up the schema handler. * - * This uses the field definitions set in $this->fieldDefinitions. + * This uses the field definitions set in $this->storageDefinitions. + * + * @param array $expected + * (optional) An associative array describing the expected entity schema to + * be created. Defaults to expecting nothing. */ - protected function setUpSchemaHandler() { - $this->entityManager->expects($this->once()) + protected function setUpEntitySchemaHandler(array $expected = array()) { + $this->entityManager->expects($this->any()) + ->method('getDefinition') + ->with($this->entityType->id()) + ->will($this->returnValue($this->entityType)); + + $this->entityManager->expects($this->any()) ->method('getFieldStorageDefinitions') ->with($this->entityType->id()) ->will($this->returnValue($this->storageDefinitions)); - $this->schemaHandler = new ContentEntitySchemaHandler( - $this->entityManager, - $this->entityType, - $this->storage - ); + + $db_schema_handler = $this->getMockBuilder('Drupal\Core\Database\Schema') + ->disableOriginalConstructor() + ->getMock(); + + if ($expected) { + $invocation_count = 0; + $expected_table_names = array_keys($expected); + $expected_table_schemas = array_values($expected); + + $db_schema_handler->expects($this->any()) + ->method('createTable') + ->with( + $this->callback(function($table_name) use (&$invocation_count, $expected_table_names) { + return $expected_table_names[$invocation_count] == $table_name; + }), + $this->callback(function($table_schema) use (&$invocation_count, $expected_table_schemas) { + return $expected_table_schemas[$invocation_count] == $table_schema; + }) + ) + ->will($this->returnCallback(function() use (&$invocation_count) { + $invocation_count++; + })); + } + + $connection = $this->getMockBuilder('Drupal\Core\Database\Connection') + ->disableOriginalConstructor() + ->getMock(); + $connection->expects($this->any()) + ->method('schema') + ->will($this->returnValue($db_schema_handler)); } /** @@ -795,7 +823,11 @@ protected function setUpSchemaHandler() { * FieldStorageDefinitionInterface::getSchema(). */ public function setUpStorageDefinition($field_name, array $schema) { - $this->storageDefinitions[$field_name] = $this->getMock('Drupal\Core\Field\FieldStorageDefinitionInterface'); + $this->storageDefinitions[$field_name] = $this->getMock('Drupal\Tests\Core\Field\TestBaseFieldDefinitionInterface'); + // getDescription() is called once for each table. + $this->storageDefinitions[$field_name]->expects($this->any()) + ->method('getName') + ->will($this->returnValue($field_name)); // getDescription() is called once for each table. $this->storageDefinitions[$field_name]->expects($this->any()) ->method('getDescription') @@ -804,7 +836,7 @@ public function setUpStorageDefinition($field_name, array $schema) { $this->storageDefinitions[$field_name]->expects($this->any()) ->method('getSchema') ->will($this->returnValue($schema)); - $this->storageDefinitions[$field_name]->expects($this->once()) + $this->storageDefinitions[$field_name]->expects($this->any()) ->method('getColumns') ->will($this->returnValue($schema['columns'])); } diff --git a/core/tests/Drupal/Tests/Core/Field/TestBaseFieldDefinitionInterface.php b/core/tests/Drupal/Tests/Core/Field/TestBaseFieldDefinitionInterface.php new file mode 100644 index 0000000..f685edd --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Field/TestBaseFieldDefinitionInterface.php @@ -0,0 +1,17 @@ +