diff --git a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php index 3d506d0..c73a5c1 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Entity; +use Drupal\Component\Utility\String; use Drupal\Core\Database\Connection; use Drupal\Core\Database\Database; use Drupal\Core\Entity\Query\QueryInterface; @@ -825,16 +826,29 @@ protected function mapToStorageRecord(ContentEntityInterface $entity, $table_nam $record = new \stdClass(); $table_mapping = $this->getTableMapping(); foreach ($table_mapping->getFieldNames($table_name) as $field_name) { + + if (empty($this->storageDefinitions[$field_name])) { + throw new EntityStorageException(String::format('Table mapping contains invalid field %field.', array('%field' => $field_name))); + } + $definition = $this->storageDefinitions[$field_name]; $columns = $table_mapping->getColumnNames($field_name); - if (!empty($this->storageDefinitions[$field_name])) { - $definition = $this->storageDefinitions[$field_name]; - foreach ($columns as $column_name => $schema_name) { + + foreach ($columns as $column_name => $schema_name) { + // If there is no main property and only a single column, get all + // properties from the first field item and assume that they will be + // stored serialized. + // @todo Give field types more control over this behavior in + // https://drupal.org/node/2232427. + if (!$definition->getMainPropertyName() && count($columns) == 1) { + $value = $entity->$field_name->first()->getValue(); + } + else { $value = isset($entity->$field_name->$column_name) ? $entity->$field_name->$column_name : NULL; - if (!empty($definition->getSchema()['columns'][$column_name]['serialize'])) { - $value = serialize($value); - } - $record->$schema_name = drupal_schema_get_field_value($definition->getSchema()['columns'][$column_name], $value); } + if (!empty($definition->getSchema()['columns'][$column_name]['serialize'])) { + $value = serialize($value); + } + $record->$schema_name = drupal_schema_get_field_value($definition->getSchema()['columns'][$column_name], $value); } }