diff --git a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php index 5c33f1e..4624459 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php @@ -161,7 +161,7 @@ class ContentEntityDatabaseStorage extends ContentEntityStorageBase implements C * * @var \Drupal\Core\Entity\Schema\ContentEntitySchemaHandlerInterface */ - protected $schemaBuilder; + protected $schemaHandler; /** * {@inheritdoc} @@ -221,7 +221,10 @@ protected function initTableLayout() { $this->revisionTable = $this->entityType->getRevisionTable() ?: $this->entityTypeId . '_revision'; } - if ($layout_type & static::LAYOUT_MULTILINGUAL) { + // @todo Remove the data table check once all entity types are using entity + // query. See https://drupal.org/node/2068325. + $data_table = $this->entityType->getDataTable(); + if ($data_table && ($layout_type & static::LAYOUT_MULTILINGUAL)) { $this->dataTable = $this->entityType->getDataTable() ?: $this->entityTypeId . '_field_data'; $this->langcodeKey = $this->entityType->getKey('langcode') ?: 'langcode'; $this->defaultLangcodeKey = $this->entityType->getKey('default_langcode') ?: 'default_langcode'; @@ -300,17 +303,17 @@ public function getRevisionDataTable() { * {@inheritdoc} */ public function getSchema() { - return $this->schemaBuilder()->getSchema(); + return $this->schemaHandler()->getSchema(); } /** * Gets the schema builder for this storage controller. */ - protected function schemaBuilder() { - if (!isset($this->schemaBuilder)) { - $this->schemaBuilder = new ContentEntitySchemaHandler($this->entityManager, $this->entityType, $this); + protected function schemaHandler() { + if (!isset($this->schemaHandler)) { + $this->schemaHandler = new ContentEntitySchemaHandler($this->entityManager, $this->entityType, $this); } - return $this->schemaBuilder; + return $this->schemaHandler; } /** @@ -969,7 +972,7 @@ protected function savePropertyData(EntityInterface $entity, $table_name = NULL) /** * Maps from an entity object to the storage record. * - * @param \Drupal\Core\Entity\EntityInterface $entity + * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity object. * @param string $table_name * (optional) The table name to map records to. Defaults to the base table. @@ -977,68 +980,44 @@ protected function savePropertyData(EntityInterface $entity, $table_name = NULL) * @return \stdClass * The record to store. */ - protected function mapToStorageRecord(EntityInterface $entity, $table_name = NULL) { + protected function mapToStorageRecord(ContentEntityInterface $entity, $table_name = NULL) { if (!isset($table_name)) { $table_name = $this->baseTable; } $record = new \stdClass(); - $values = array(); - $definitions = $entity->getFieldDefinitions(); - $schema = $this->schemaBuilder()->getSchema()[$table_name]; $is_new = $entity->isNew(); + $table_mapping = $this->getTableMapping(); - $multi_column_fields = array(); - 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)) { - $multi_column_fields[$field] = TRUE; - continue; - } - $value = isset($definitions[$name]) && isset($entity->$name->value) ? $entity->$name->value : NULL; - - // We allow for storage controllers to have schema fields which are not - // entity fields, which can be useful for normalization purposes. The - // 'default_langcode' field, for example, is simply a denormalization of - // checking the language code in the data table against the language code - // in the base table and similarly the 'isDefaultRevision' field (which we - // add as a query expression in - // FieldableDatabaseStorageController::buildQuery()) is a denormalization - // of checking the revision ID in the revision table against the revision - // ID in the base table. - $serialize = FALSE; - if (isset($definitions[$name])) { - $definition = $definitions[$name]; - // Since this is not a multi-column field we can assume there is only - // one column. - $serialize = !empty($definition->getColumns()[$definition->getMainPropertyName()]['serialize']); - } - - $values[$name] = $serialize ? serialize($value) : $value; - } + foreach ($table_mapping[$table_name] as $name => $storage_columns) { + if (!empty($this->fieldDefinitions[$name])) { + $definition = $this->fieldDefinitions[$name]; + foreach ($definition->getColumns() as $column => $column_info) { + $value = isset($entity->$name->$column) ? $entity->$name->$column : NULL; + + // We allow for storage controllers to have schema fields which are + // not entity fields, which can be useful for normalization purposes. + // The 'default_langcode' field, for example, is simply a + // denormalization of checking the language code in the data table + // against the language code in the base table and similarly the + // 'isDefaultRevision' field (which we add as a query expression in + // self::buildQuery()) is denormalization of checking the revision ID + // in the revision table against the revision ID in the base table. + if (!empty($column_info['serialize'])) { + $value = serialize($value); + } - // Handle fields that store multiple properties and match each property name - // to its schema column name. - foreach (array_keys($multi_column_fields) as $field_name) { - $field_items = $entity->get($field_name); - $field_value = $field_items->getValue(); - foreach ($field_items->getFieldDefinition()->getColumns() as $column_name => $column_info) { - if (isset($schema['fields'][$field_name . '__' . $column_name])) { - $value = isset($field_value[0][$column_name]) ? $field_value[0][$column_name] : NULL; - $values[$field_name . '__' . $column_name] = !empty($column_info['serialize']) ? serialize($value) : $value; + // If we are creating a new entity, we must not populate the record + // with NULL values otherwise defaults would not be applied. + if (isset($value) || !$is_new) { + $info = $definition->getSchema()['columns'][$column]; + list(, $storage_column) = each($storage_columns); + $record->$storage_column = drupal_schema_get_field_value($info, $value); + } } } } - foreach ($values as $field_name => $value) { - // If we are creating a new entity, we must not populate the record with - // NULL values otherwise defaults would not be applied. - if (isset($value) || !$is_new) { - $record->$field_name = drupal_schema_get_field_value($schema['fields'][$field_name], $value); - } - } - return $record; } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php index ec3d65f..68414d3 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php @@ -42,7 +42,7 @@ * "admin-form" = "custom_block.type_edit" * }, * fieldable = TRUE, - * translatable = FALSE, + * translatable = TRUE, * entity_keys = { * "id" = "id", * "revision" = "revision_id", diff --git a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php index 3656b3e..69adba8 100644 --- a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php +++ b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php @@ -37,7 +37,7 @@ * base_table = "comment", * uri_callback = "comment_uri", * fieldable = TRUE, - * translatable = FALSE, + * translatable = TRUE, * entity_keys = { * "id" = "cid", * "bundle" = "field_id", diff --git a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php index a522385..1ad843a 100644 --- a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php +++ b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php @@ -98,15 +98,6 @@ public function testNormalize() { 'revision_id' => array( array('value' => 1), ), - 'revision_timestamp' => array( - array('value' => NULL), - ), - 'revision_uid' => array( - array('target_id' => NULL), - ), - 'log' => array( - array('value' => NULL), - ), 'field_test_text' => array( array( 'value' => $this->values['field_test_text']['value'], @@ -153,9 +144,6 @@ public function testSerialize() { 'type' => 'entity_test_mulrev', 'user_id' => '' . $this->values['user_id'] . '', 'revision_id' => '' . $this->entity->getRevisionId() . '', - 'revision_timestamp' => '', - 'revision_uid' => '', - 'log' => '', 'field_test_text' => '' . $this->values['field_test_text']['value'] . '' . $this->values['field_test_text']['format'] . '', ); // Sort it in the same order as normalised. diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php b/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php index f4bbde9..255200a 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php @@ -31,7 +31,7 @@ * }, * base_table = "shortcut", * data_table = "shortcut_field_data", - * translatable = FALSE, + * translatable = TRUE, * entity_keys = { * "id" = "id", * "uuid" = "uuid", diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php index 17414d2..4550acb 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php @@ -35,7 +35,7 @@ * base_table = "taxonomy_term_data", * uri_callback = "taxonomy_term_uri", * fieldable = TRUE, - * translatable = FALSE, + * translatable = TRUE, * entity_keys = { * "id" = "tid", * "bundle" = "vid", diff --git a/core/modules/user/lib/Drupal/user/Entity/User.php b/core/modules/user/lib/Drupal/user/Entity/User.php index 1fd5709..3c13a94 100644 --- a/core/modules/user/lib/Drupal/user/Entity/User.php +++ b/core/modules/user/lib/Drupal/user/Entity/User.php @@ -38,7 +38,7 @@ * uri_callback = "user_uri", * label_callback = "user_format_name", * fieldable = TRUE, - * translatable = FALSE, + * translatable = TRUE, * entity_keys = { * "id" = "uid", * "uuid" = "uuid"