diff --git a/core/includes/schema.inc b/core/includes/schema.inc index 47e7836..5773d71 100644 --- a/core/includes/schema.inc +++ b/core/includes/schema.inc @@ -370,13 +370,15 @@ function drupal_schema_fields_sql($table, $prefix = NULL) { * If the record insert or update failed, returns FALSE. If it succeeded, * returns SAVED_NEW or SAVED_UPDATED, depending on the operation performed. */ -function drupal_write_record($table, &$record, $primary_keys = array()) { +function drupal_write_record($table, &$record, $primary_keys = array(), $schema = NULL) { // Standardize $primary_keys to an array. if (is_string($primary_keys)) { $primary_keys = array($primary_keys); } + if (!isset($schema)) { $schema = drupal_get_schema($table); + } if (empty($schema)) { return FALSE; } diff --git a/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php b/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php index 8cdde53..43eb437 100644 --- a/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php +++ b/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php @@ -40,6 +40,13 @@ class FieldableDatabaseStorageController extends FieldableEntityStorageControlle protected $revisionKey = FALSE; /** + * The base table of the entity, if the entity has storage. + * + * @var string + */ + protected $baseTable; + + /** * The table that stores revisions, if the entity supports revisions. * * @var string @@ -68,6 +75,13 @@ class FieldableDatabaseStorageController extends FieldableEntityStorageControlle protected $cache; /** + * The computed schema array of this entity type. + * + * @var array + */ + protected $schema; + + /** * Active database connection. * * @var \Drupal\Core\Database\Connection @@ -116,6 +130,11 @@ public function __construct(EntityTypeInterface $entity_type, Connection $databa // Check if the entity type supports UUIDs. $this->uuidKey = $this->entityType->getKey('uuid'); + // Some entity types are not stored in the database at all. + if ($base_table = $this->entityType->getBaseTable()) { + $this->baseTable = $base_table; + } + // Check if the entity type supports revisions. if ($this->entityType->hasKey('revision')) { $this->revisionKey = $this->entityType->getKey('revision'); @@ -123,12 +142,14 @@ public function __construct(EntityTypeInterface $entity_type, Connection $databa } // Check if the entity type has a dedicated table for fields. + // @todo Replace this with a check for the 'language' entity key when + // https://drupal.org/node/2143729 has landed. if ($data_table = $this->entityType->getDataTable()) { $this->dataTable = $data_table; // Entity types having both revision and translation support should always // define a revision data table. - if ($this->revisionTable && $revision_data_table = $this->entityType->getRevisionDataTable()) { - $this->revisionDataTable = $revision_data_table; + if ($this->revisionTable) { + $this->revisionDataTable = $this->entityType->getRevisionDataTable(); } } } @@ -139,6 +160,11 @@ public function __construct(EntityTypeInterface $entity_type, Connection $databa public function loadMultiple(array $ids = NULL) { $entities = array(); + // If the schema has not been initialized yet, no entities exist. + if (!$this->schemaExists()) { + return $entities; + } + // Create a new variable which is either a prepared version of the $ids // array for later comparison with the entity cache, or FALSE if no $ids // were passed. The $ids array is reduced as items are loaded from cache, @@ -319,6 +345,10 @@ protected function attachPropertyData(array &$entities) { * Implements \Drupal\Core\Entity\EntityStorageControllerInterface::loadRevision(). */ public function loadRevision($revision_id) { + if (!$this->schemaExists()) { + return FALSE; + } + // Build and execute the query. $query_result = $this->buildQuery(array(), $revision_id)->execute(); $queried_entities = $query_result->fetchAllAssoc($this->idKey); @@ -469,7 +499,7 @@ protected function postLoad(array &$queried_entities) { * Implements \Drupal\Core\Entity\EntityStorageControllerInterface::delete(). */ public function delete(array $entities) { - if (!$entities) { + if (!$entities || $this->schemaExists()) { // If no IDs or invalid IDs were passed, do nothing. return; } @@ -534,6 +564,8 @@ public function delete(array $entities) { public function save(EntityInterface $entity) { $transaction = $this->database->startTransaction(); try { + $this->ensureSchema(); + // Sync the changes made in the fields array to the internal values array. $entity->updateOriginalValues(); @@ -551,7 +583,7 @@ public function save(EntityInterface $entity) { if (!$entity->isNew()) { if ($entity->isDefaultRevision()) { - $return = drupal_write_record($this->entityType->getBaseTable(), $record, $this->idKey); + $return = drupal_write_record($this->baseTable, $record, $this->idKey, $this->getSchema('base_table')); } else { // @todo, should a different value be returned when saving an entity @@ -581,7 +613,7 @@ public function save(EntityInterface $entity) { // Ensure the entity is still seen as new after assigning it an id, // while storing its data. $entity->enforceIsNew(); - $return = drupal_write_record($this->entityType->getBaseTable(), $record); + $return = drupal_write_record($this->baseTable, $record, array(), $this->getSchema('base_table')); $entity->{$this->idKey}->value = (string) $record->{$this->idKey}; if ($this->revisionTable) { $entity->setNewRevision(); @@ -669,11 +701,16 @@ protected function mapToStorageRecord(EntityInterface $entity, $table_key = 'bas $record = new \stdClass(); $values = array(); $definitions = $entity->getFieldDefinitions(); + $schema = $this->getSchema($table_key); + // Backwards-compatibility layer for entity types that declare their schema + // in hook_schema() manually. + if (!$schema) { $schema = drupal_get_schema($this->entityType->get($table_key)); + } $is_new = $entity->isNew(); $multi_column_fields = array(); - foreach (drupal_schema_fields_sql($this->entityType->get($table_key)) as $name) { + foreach (array_keys($schema['fields']) as $name) { // Check for fields which store data in multiple columns and process them // separately. if ($field = strstr($name, '__', TRUE)) { @@ -746,7 +783,7 @@ protected function saveRevision(EntityInterface $entity) { $entity->preSaveRevision($this, $record); if ($entity->isNewRevision()) { - drupal_write_record($this->revisionTable, $record); + drupal_write_record($this->revisionTable, $record, array(), $this->getSchema('revision_table')); if ($entity->isDefaultRevision()) { $this->database->update($this->entityType->getBaseTable()) ->fields(array($this->revisionKey => $record->{$this->revisionKey})) @@ -755,7 +792,7 @@ protected function saveRevision(EntityInterface $entity) { } } else { - drupal_write_record($this->revisionTable, $record, $this->revisionKey); + drupal_write_record($this->revisionTable, $record, $this->revisionKey, $this->getSchema('revision_table')); } // Make sure to update the new revision key for the entity. @@ -1458,57 +1495,64 @@ static public function _fieldColumnName(FieldConfigInterface $field, $column) { /** * Gets the schema for this entity type. * + * @param string $table_key + * (optional) The entity key identifying the target table to return the + * schema for. If omitted, returns the schema for all table types. + * * @return array|null * Returns the schema definitions for the entity type's tables. If the * entity type does not have a schema, returns NULL. * * @throws \LogicException */ - public function getSchema() { - $schema = array(); - - $entity_type = $this->getEntityType(); - $entity_type_id = $entity_type->id(); - $entity_keys = array_filter($entity_type->getKeys()); - $entity_keys = array_intersect_key($entity_keys, array_flip(array('id', 'revision', 'bundle', 'uuid'))); + public function getSchema($table_key = NULL) { + $passed_table_key = $table_key; $tables = array_filter(array( - 'base table' => $entity_type->getBaseTable(), - 'data table' => $entity_type->getDataTable(), - 'revision table' => $entity_type->getRevisionTable(), - 'revision data table' => $entity_type->getRevisionDataTable(), + 'base_table' => $this->baseTable, + 'revision_table' => $this->revisionTable, + 'data_table' => $this->dataTable, + 'revision_data_table' => $this->revisionDataTable, )); // If this entity type does not support storage, no schema information can // be collected. if (empty($tables)) { - return; + $this->schema = NULL; } - // Rule out faulty configurations. - if (!isset($tables['base table'])) { - throw new \LogicException("The entity type '$entity_type_id' does not provide a base table."); - } - elseif (isset($tables['revision data table']) && !isset($tables['revision table'])) { - throw new \LogicException("The entity type '$entity_type_id' does not provide a revision table."); - } - elseif (isset($tables['data table']) && isset($tables['revision table']) && !isset($tables['revision data table'])) { - throw new \LogicException("The entity type '$entity_type_id' does not provide a revision data table."); - } - if (isset($tables['revision table']) && !isset($entity_keys['revision'])) { - throw new \LogicException("The entity type '$entity_type_id' does not provide a revision entity key."); - } - // @todo Add a check for the language entity key when - // https://drupal.org/node/2143729 has landed + if (!isset($this->schema)) { + $entity_type_id = $this->entityTypeId; + $entity_keys = array_filter(array( + 'id' => $this->idKey, + 'revision' => $this->revisionKey, + 'bundle' => $this->bundleKey, + 'uuid' => $this->uuidKey, + )); // Prepare a mapping of which fields should be stored on which table. This // information depends on which table types are provided. - if (!isset($tables['data table']) && !isset($tables['revision table'])) { + if (!isset($tables['data_table']) && !isset($tables['revision_table'])) { // In the simplest case all fields are stored in the base table. - $field_table_mapping = array('base table' => array_values($entity_keys)); - $other_fields_tables = array('base table'); + $field_table_mapping = array('base_table' => array_values($entity_keys)); + $other_fields_tables = array('base_table'); + } + elseif (!isset($tables['data_table']) && isset($tables['revision_table'])) { + // For non-translatable, revisionable entities the entity key fields + // except for ID and revision ID are only stored in the base table. The ID + // is stored in both the base table and the revision table and the + // revision ID is only stored in the revision table. Additionally, the + // default revision ID of an entity is tracked as the revision ID field in + // the base table. + // Other fields are stored in the in the revision table and other fields + // of the default revision are stored in the the base table. + $field_table_mapping = array( + 'base_table' => array_values($entity_keys), + 'revision_table' => array($entity_keys['id'], $entity_keys['revision']), + ); + $other_fields_tables = array('base_table', 'revision_table'); } - elseif (isset($tables['data table']) && !isset($tables['revision table'])) { + elseif (isset($tables['data_table']) && !isset($tables['revision_table'])) { // @todo Remove this when https://drupal.org/node/2143729 has landed $entity_keys['langcode'] = 'langcode'; @@ -1522,25 +1566,10 @@ public function getSchema() { // default language code is stored as the language code field in the base // table. $field_table_mapping = array( - 'base table' => array_values($entity_keys), - 'data table' => array($entity_keys['id'], 'langcode', 'default_langcode'), + 'base_table' => array_values($entity_keys), + 'data_table' => array($entity_keys['id'], 'langcode', 'default_langcode'), ); - $other_fields_tables = array('data table'); - } - elseif (!isset($tables['data table']) && isset($tables['revision table'])) { - // For non-translatable, revisionable entities the entity key fields - // except for ID and revision ID are only stored in the base table. The ID - // is stored in both the base table and the revision table and the - // revision ID is only stored in the revision table. Additionally, the - // default revision ID of an entity is tracked as the revision ID field in - // the base table. - // Other fields are stored in the in the revision table and other fields - // of the default revision are stored in the the base table. - $field_table_mapping = array( - 'base table' => array_values($entity_keys), - 'revision table' => array($entity_keys['id'], $entity_keys['revision']), - ); - $other_fields_tables = array('base table', 'revision table'); + $other_fields_tables = array('data_table'); } else { // @todo Remove this when https://drupal.org/node/2143729 has landed @@ -1565,39 +1594,39 @@ public function getSchema() { // revision data table and other fields of the default revision are stored // in the data table. $field_table_mapping = array( - 'base table' => array_values($entity_keys), - 'data table' => array($entity_keys['id'], $entity_keys['revision'], 'langcode', 'default_langcode'), - 'revision table' => array($entity_keys['id'], $entity_keys['revision'], 'langcode'), - 'revision data table' => array($entity_keys['id'], $entity_keys['revision'], 'langcode', 'default_langcode'), + 'base_table' => array_values($entity_keys), + 'revision_table' => array($entity_keys['id'], $entity_keys['revision'], 'langcode'), + 'data_table' => array($entity_keys['id'], $entity_keys['revision'], 'langcode', 'default_langcode'), + 'revision_data_table' => array($entity_keys['id'], $entity_keys['revision'], 'langcode', 'default_langcode'), ); - $other_fields_tables = array('data table', 'revision data table'); + $other_fields_tables = array('data_table', 'revision_data_table'); } // Add primary keys and common foreign keys to the tables. - $schema[$tables['base table']]['primary key'] = array($entity_keys['id']); - $schema[$tables['base table']]['foreign keys'] = array(); - if (isset($tables['data table'])) { - $schema[$tables['data table']]['primary key'] = array($entity_keys['id'], 'langcode'); - $schema[$tables['data table']]['foreign keys'][$entity_keys['id']] = array( - 'table' => $tables['base table'], + $this->schema[$tables['base_table']]['primary key'] = array($entity_keys['id']); + $this->schema[$tables['base_table']]['foreign keys'] = array(); + if (isset($tables['data_table'])) { + $this->schema[$tables['data_table']]['primary key'] = array($entity_keys['id'], 'langcode'); + $this->schema[$tables['data_table']]['foreign keys'][$entity_keys['id']] = array( + 'table' => $tables['base_table'], 'columns' => array($entity_keys['id'] => $entity_keys['id']), ); } - if (isset($tables['revision table'])) { - $schema[$tables['revision table']]['primary key'] = array($entity_keys['revision']); - $schema[$tables['revision table']]['foreign keys'][$entity_keys['id']] = array( - 'table' => $tables['base table'], + if (isset($tables['revision_table'])) { + $this->schema[$tables['revision_table']]['primary key'] = array($entity_keys['revision']); + $this->schema[$tables['revision_table']]['foreign keys'][$entity_keys['id']] = array( + 'table' => $tables['base_table'], 'columns' => array($entity_keys['id'] => $entity_keys['id']), ); } - if (isset($tables['revision data table'])) { - $schema[$tables['revision data table']]['primary key'] = array($entity_keys['revision'], 'langcode'); - $schema[$tables['revision data table']]['foreign keys'][$entity_keys['id']] = array( - 'table' => $tables['base table'], + if (isset($tables['revision_data_table'])) { + $this->schema[$tables['revision_data_table']]['primary key'] = array($entity_keys['revision'], 'langcode'); + $this->schema[$tables['revision_data_table']]['foreign keys'][$entity_keys['id']] = array( + 'table' => $tables['base_table'], 'columns' => array($entity_keys['id'] => $entity_keys['id']), ); - $schema[$tables['revision data table']]['foreign keys'][$entity_keys['revision']] = array( - 'table' => $tables['revision table'], + $this->schema[$tables['revision_data_table']]['foreign keys'][$entity_keys['revision']] = array( + 'table' => $tables['revision_table'], 'columns' => array($entity_keys['revision'] => $entity_keys['revision']), ); } @@ -1605,7 +1634,7 @@ public function getSchema() { $class = $this->entityClass; // Invoke ContentEntityInterface::baseFieldDefinitions() directly. Avoid // info or alter hooks. - foreach($class::baseFieldDefinitions($entity_type) as $field_name => $field_definition) { + foreach($class::baseFieldDefinitions($entity_type_id) as $field_name => $field_definition) { /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ $field_schema = $field_definition->getSchema(); @@ -1615,49 +1644,49 @@ public function getSchema() { $main_property_name = $field_definition->getMainPropertyName(); if (in_array($field_name, $entity_keys)) { - foreach ($field_table_mapping as $table_type => $field_names) { + foreach ($field_table_mapping as $table_key => $field_names) { if (in_array($field_name, $field_names)) { if (!$single_column_field) { throw new \LogicException("The $field_name field of the $entity_type_id entity type has multiple columns."); } - $schema[$tables[$table_type]]['fields'][$field_name] = $field_schema['columns'][$main_property_name]; - $schema[$tables[$table_type]]['fields'][$field_name]['description'] = $field_definition->getDescription(); + $this->schema[$tables[$table_key]]['fields'][$field_name] = $field_schema['columns'][$main_property_name]; + $this->schema[$tables[$table_key]]['fields'][$field_name]['description'] = $field_definition->getDescription(); if (!empty($field_schema['unique keys'])) { - $schema[$tables[$table_type]]['unique keys'][$field_name] = array($field_name); + $this->schema[$tables[$table_key]]['unique keys'][$field_name] = array($field_name); } if (!empty($field_schema['indexes'])) { // @todo Support indexes specified as an array of column name and // length. - $schema[$tables[$table_type]]['indexes'][$field_name] = array($field_name); + $this->schema[$tables[$table_key]]['indexes'][$field_name] = array($field_name); } if (!empty($field_schema['foreign keys'])) { - $schema[$tables[$table_type]]['foreign keys'] += $field_schema['foreign keys']; + $this->schema[$tables[$table_key]]['foreign keys'] += $field_schema['foreign keys']; } } } } else { - foreach ($other_fields_tables as $table_type) { + foreach ($other_fields_tables as $table_key) { if ($single_column_field) { - $schema[$tables[$table_type]]['fields'][$field_name] = $field_schema['columns'][$main_property_name]; - $schema[$tables[$table_type]]['fields'][$field_name]['description'] = $field_definition->getDescription(); + $this->schema[$tables[$table_key]]['fields'][$field_name] = $field_schema['columns'][$main_property_name]; + $this->schema[$tables[$table_key]]['fields'][$field_name]['description'] = $field_definition->getDescription(); if (!empty($field_schema['unique keys'])) { - $schema[$tables[$table_type]]['unique keys'][$field_name] = array($field_name); + $this->schema[$tables[$table_key]]['unique keys'][$field_name] = array($field_name); } if (!empty($field_schema['indexes'])) { // @todo Support indexes specified as an array of column name and // length. - $schema[$tables[$table_type]]['indexes'][$field_name] = array($field_name); + $this->schema[$tables[$table_key]]['indexes'][$field_name] = array($field_name); } if (!empty($field_schema['foreign keys'])) { - $schema[$tables[$table_type]]['foreign keys'] += $field_schema['foreign keys']; + $this->schema[$tables[$table_key]]['foreign keys'] += $field_schema['foreign keys']; } } else { foreach ($field_schema['columns'] as $column_name => $column_schema) { - $schema[$tables[$table_type]]['fields'][$field_name . '__' . $column_name] = $column_schema; + $this->schema[$tables[$table_key]]['fields'][$field_name . '__' . $column_name] = $column_schema; } // @todo Unique keys, indexes and foreign keys. @@ -1667,21 +1696,82 @@ public function getSchema() { } // Change the ID field in the base table to 'serial' if it is 'int'. - if ($schema[$tables['base table']]['fields'][$entity_keys['id']]['type'] == 'int') { - $schema[$tables['base table']]['fields'][$entity_keys['id']]['type'] = 'serial'; + if ($this->schema[$tables['base_table']]['fields'][$entity_keys['id']]['type'] == 'int') { + $this->schema[$tables['base_table']]['fields'][$entity_keys['id']]['type'] = 'serial'; + unset($this->schema[$tables['base_table']]['fields'][$entity_keys['id']]['default']); } // Change the revision ID field in the revision table 'serial' if it is // 'int'. - if (isset($tables['revision table']) && $schema[$tables['revision table']]['fields'][$entity_keys['revision']]['type'] == 'int') { - $schema[$tables['revision table']]['fields'][$entity_keys['revision']]['type'] = 'serial'; + if (isset($tables['revision_table']) && $this->schema[$tables['revision_table']]['fields'][$entity_keys['revision']]['type'] == 'int') { + $this->schema[$tables['revision_table']]['fields'][$entity_keys['revision']]['type'] = 'serial'; + unset($this->schema[$tables['revision_table']]['fields'][$entity_keys['revision']]['default']); } // Add table descriptions. - foreach ($tables as $table_type => $table) { - $schema[$table]['description'] = "The $table_type table for $entity_type_id entities."; + foreach ($tables as $table_key => $table) { + $this->schema[$table]['description'] = "The $table_key table for $entity_type_id entities."; + } + } + + if (isset($passed_table_key)) { + if (isset($this->schema[$tables[$passed_table_key]])) { + return $this->schema[$tables[$passed_table_key]]; + } + return; + } + + return $this->schema; + } + + /** + * Checks whether the entity tables exist in the database. + * + * @return bool + * Whether or not the schema tables exist in the database. + */ + protected function schemaExists() { + if ($this->baseTable) { + return $this->database->schema()->tableExists($this->baseTable); + } + // Return FALSE for entities without any storage. + return FALSE; + } + + /** + * Ensures the entity tables exist in the database. + * + * If the entity tables do not exist, they are created according to the schema + * definitions computed from the entity type's fields. + * + * @see \Drupal\Core\Entity\FieldableDatabaseStorageController::getSchema() + * + * @todo Make this protected. + */ + public function ensureSchema() { + if (!$this->schemaExists() && ($schema = $this->getSchema())) { + foreach ($schema as $table_name => $table_schema) { + $this->database->schema()->createTable($table_name, $table_schema); + } + } } - return $schema; + /** + * Deletes the entity tables from the database. + * + * @todo Make this protected. + */ + public function deleteSchema() { + if ($this->schemaExists()) { + $tables = array_filter(array( + $this->baseTable, + $this->revisionTable, + $this->dataTable, + $this->revisionDataTable, + )); + foreach ($tables as $table_name) { + $this->database->schema()->dropTable($table_name); + } + } } } diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/Query.php b/core/lib/Drupal/Core/Entity/Query/Sql/Query.php index 407cef5..62fd9a6 100644 --- a/core/lib/Drupal/Core/Entity/Query/Sql/Query.php +++ b/core/lib/Drupal/Core/Entity/Query/Sql/Query.php @@ -11,6 +11,7 @@ use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Entity\FieldableDatabaseStorageController; use Drupal\Core\Entity\Query\QueryBase; use Drupal\Core\Entity\Query\QueryException; use Drupal\Core\Entity\Query\QueryInterface; @@ -53,13 +54,6 @@ class Query extends QueryBase implements QueryInterface { protected $connection; /** - * Stores the entity manager used by the query. - * - * @var \Drupal\Core\Entity\EntityManagerInterface - */ - protected $entityManager; - - /** * Constructs a query object. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type @@ -107,6 +101,12 @@ protected function prepare() { if ($this->entityType->getDataTable()) { $simple_query = FALSE; } + + $storage_controller = \Drupal::entityManager()->getStorageController($this->entityTypeId); + if ($storage_controller instanceof FieldableDatabaseStorageController) { + $storage_controller->ensureSchema(); + } + $this->sqlQuery = $this->connection->select($base_table, 'base_table', array('conjunction' => $this->conjunction)); $this->sqlQuery->addMetaData('entity_type', $this->entityTypeId); $id_field = $this->entityType->getKey('id'); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index 58a5961..90a2711 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -89,6 +89,7 @@ public static function schema(FieldDefinitionInterface $field_definition) { 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, + 'default' => 0, ), ); } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php index 0a7a8cf..07c9a81 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php @@ -42,6 +42,7 @@ public static function schema(FieldDefinitionInterface $field_definition) { 'value' => array( 'type' => 'int', 'not null' => TRUE, + 'default' => 0, ), ), ); diff --git a/core/modules/system/tests/modules/entity_test/entity_test.install b/core/modules/system/tests/modules/entity_test/entity_test.install index 3df5396..eb46f54 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.install +++ b/core/modules/system/tests/modules/entity_test/entity_test.install @@ -9,433 +9,57 @@ * Implements hook_install(). */ function entity_test_install() { - $entity_types = array( + $entity_manager = \Drupal::entityManager(); + + $entity_type_ids = array( 'entity_test', 'entity_test_rev', 'entity_test_mul', 'entity_test_mulrev', ); - foreach ($entity_types as $entity_type) { + foreach ($entity_type_ids as $entity_type_id) { + $storage_controller = $entity_manager->getStorageController($entity_type_id); + + // Install schema. + $storage_controller->ensureSchema(); + // Auto-create fields for testing. - entity_create('field_config', array( + $storage_controller->create(array( 'name' => 'field_test_text', - 'entity_type' => $entity_type, + 'entity_type' => $entity_type_id, 'type' => 'text', 'cardinality' => 1, 'translatable' => FALSE, ))->save(); - entity_create('field_instance_config', array( - 'entity_type' => $entity_type, + $storage_controller->create(array( + 'entity_type' => $entity_type_id, 'field_name' => 'field_test_text', - 'bundle' => $entity_type, + 'bundle' => $entity_type_id, 'label' => 'Test text-field', ))->save(); - entity_get_form_display($entity_type, $entity_type, 'default') + entity_get_form_display($entity_type_id, $entity_type_id, 'default') ->setComponent('field_test_text', array('type' => 'text_text')) ->save(); } } /** - * Implements hook_schema(). + * Implements hook_uninstall(). */ -function entity_test_schema() { - // Schema for simple entity. - $schema['entity_test'] = array( - 'description' => 'Stores entity_test items.', - 'fields' => array( - 'id' => array( - 'type' => 'serial', - 'not null' => TRUE, - 'description' => 'Primary Key: Unique entity-test item ID.', - ), - 'uuid' => array( - 'description' => 'Unique Key: Universally unique identifier for this entity.', - 'type' => 'varchar', - 'length' => 128, - 'not null' => FALSE, - ), - 'type' => array( - 'description' => 'The bundle of the test entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - 'langcode' => array( - 'description' => 'The {language}.langcode of the original variant of this test entity.', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - ), - 'name' => array( - 'description' => 'The name of the test entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - 'user_id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - 'default' => NULL, - 'description' => 'The {users}.uid of the associated user.', - ), - ), - 'primary key' => array('id'), - 'unique keys' => array( - 'uuid' => array('uuid'), - ), - ); - - // Schema for entity with revisions. - $schema['entity_test_rev'] = array( - 'description' => 'Stores entity_test_rev items.', - 'fields' => array( - 'id' => array( - 'type' => 'serial', - 'not null' => TRUE, - 'description' => 'Primary Key: Unique entity-test item ID.', - ), - 'revision_id' => array( - 'description' => 'The current {entity_test_rev_property_revision}.revision_id version identifier.', - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'default' => 0, - ), - 'uuid' => array( - 'description' => 'Unique Key: Universally unique identifier for this entity.', - 'type' => 'varchar', - 'length' => 128, - 'not null' => FALSE, - ), - 'type' => array( - 'description' => 'The bundle of the test entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - 'name' => array( - 'description' => 'The name of the test entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - 'user_id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - 'default' => NULL, - 'description' => 'The {users}.uid of the associated user.', - ), - ), - 'primary key' => array('id'), - 'unique keys' => array( - 'uuid' => array('uuid'), - ), - ); - $schema['entity_test_rev_revision'] = array( - 'description' => 'Stores entity_test_rev item property revisions.', - 'fields' => array( - 'id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'description' => 'The {entity_test_rev}.id of the test entity.', - ), - 'revision_id' => array( - 'type' => 'serial', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'description' => 'The primary identifier for this version.', - ), - 'langcode' => array( - 'description' => 'The {language}.langcode of this variant of this test entity.', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - ), - 'name' => array( - 'description' => 'The name of the test entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - 'user_id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - 'default' => NULL, - 'description' => 'The {users}.uid of the associated user.', - ), - ), - 'indexes' => array( - 'user_id' => array('user_id'), - ), - 'foreign keys' => array( - 'user_id' => array('users' => 'uid'), - 'id' => array('entity_test_rev' => 'id'), - ), - 'primary key' => array('revision_id'), - ); - - // Schema for entity with data table. - $schema['entity_test_mul'] = array( - 'description' => 'Stores entity_test_mul items.', - 'fields' => array( - 'id' => array( - 'type' => 'serial', - 'not null' => TRUE, - 'description' => 'Primary Key: Unique entity-test item ID.', - ), - 'uuid' => array( - 'description' => 'Unique Key: Universally unique identifier for this entity.', - 'type' => 'varchar', - 'length' => 128, - 'not null' => FALSE, - ), - 'type' => array( - 'description' => 'The bundle of the test entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - 'langcode' => array( - 'description' => 'The {language}.langcode of the original variant of this test entity.', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - ), - ), - 'primary key' => array('id'), - 'unique keys' => array( - 'uuid' => array('uuid'), - ), - ); - $schema['entity_test_mul_property_data'] = array( - 'description' => 'Stores entity_test_mul item properties.', - 'fields' => array( - 'id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'description' => 'The {entity_test_mul}.id of the test entity.', - ), - 'langcode' => array( - 'description' => 'The {language}.langcode of this variant of this test entity.', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - ), - 'default_langcode' => array( - 'description' => 'Boolean indicating whether the current variant is in the original entity language.', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 1, - ), - 'name' => array( - 'description' => 'The name of the test entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => FALSE, - ), - 'user_id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - 'default' => NULL, - 'description' => 'The {users}.uid of the associated user.', - ), - ), - 'indexes' => array( - 'user_id' => array('user_id'), - ), - 'foreign keys' => array( - 'user_id' => array('users' => 'uid'), - 'id' => array('entity_test_mul' => 'id'), - ), - 'primary key' => array('id', 'langcode'), - ); +function entity_test_uninstall() { + $entity_manager = \Drupal::entityManager(); - // Schema for entity with data table and revisions. - $schema['entity_test_mulrev'] = array( - 'description' => 'Stores entity_test_mulrev items.', - 'fields' => array( - 'id' => array( - 'type' => 'serial', - 'not null' => TRUE, - 'description' => 'Primary Key: Unique entity-test item ID.', - ), - 'revision_id' => array( - 'description' => 'The current {entity_test_mulrev_property_revision}.revision_id version identifier.', - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'default' => 0, - ), - 'uuid' => array( - 'description' => 'Unique Key: Universally unique identifier for this entity.', - 'type' => 'varchar', - 'length' => 128, - 'not null' => FALSE, - ), - 'type' => array( - 'description' => 'The bundle of the test entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - ), - 'primary key' => array('id'), - 'unique keys' => array( - 'uuid' => array('uuid'), - ), - ); - $schema['entity_test_mulrev_revision'] = array( - 'description' => 'Stores entity_test_rev item property revisions.', - 'fields' => array( - 'id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'description' => 'The {entity_test_rev}.id of the test entity.', - ), - 'revision_id' => array( - 'type' => 'serial', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'description' => 'The primary identifier for this version.', - ), - 'langcode' => array( - 'description' => 'The {language}.langcode of this variant of this test entity.', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - ), - ), - 'foreign keys' => array( - 'id' => array('entity_test_rev' => 'id'), - ), - 'primary key' => array('revision_id'), - ); - $schema['entity_test_mulrev_property_data'] = array( - 'description' => 'Stores entity_test_mulrev item properties.', - 'fields' => array( - 'id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'description' => 'The {entity_test_mulrev}.id of the test entity.', - ), - 'revision_id' => array( - 'description' => 'The current {entity_test_mulrev_property_revision}.revision_id version identifier.', - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'default' => 0, - ), - 'langcode' => array( - 'description' => 'The {language}.langcode of this variant of this test entity.', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - ), - 'default_langcode' => array( - 'description' => 'Boolean indicating whether the current variant is in the original entity language.', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 1, - ), - 'name' => array( - 'description' => 'The name of the test entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - 'user_id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - 'default' => NULL, - 'description' => 'The {users}.uid of the associated user.', - ), - ), - 'indexes' => array( - 'user_id' => array('user_id'), - ), - 'foreign keys' => array( - 'user_id' => array('users' => 'uid'), - 'id' => array('entity_test_mulrev' => 'id'), - ), - 'primary key' => array('id', 'langcode'), - ); - $schema['entity_test_mulrev_property_revision'] = array( - 'description' => 'Stores entity_test_mulrev item property revisions.', - 'fields' => array( - 'id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'description' => 'The {entity_test_mulrev}.id of the test entity.', - ), - 'revision_id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'description' => 'The primary identifier for this version.', - ), - 'langcode' => array( - 'description' => 'The {language}.langcode of this variant of this test entity.', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - ), - 'default_langcode' => array( - 'description' => 'Boolean indicating whether the current variant is in the original entity language.', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 1, - ), - 'name' => array( - 'description' => 'The name of the test entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - 'user_id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - 'default' => NULL, - 'description' => 'The {users}.uid of the associated user.', - ), - ), - 'indexes' => array( - 'user_id' => array('user_id'), - ), - 'foreign keys' => array( - 'user_id' => array('users' => 'uid'), - 'id' => array('entity_test_mulrev' => 'id'), - ), - 'primary key' => array('revision_id', 'langcode'), + $entity_type_ids = array( + 'entity_test', + 'entity_test_rev', + 'entity_test_mul', + 'entity_test_mulrev', ); + foreach ($entity_type_ids as $entity_type_id) { + $storage_controller = $entity_manager->getStorageController($entity_type_id); - return $schema; + // Install schema. + $storage_controller->deleteSchema(); + } }