diff --git a/core/includes/schema.inc b/core/includes/schema.inc index ac0ffad..e7ec164 100644 --- a/core/includes/schema.inc +++ b/core/includes/schema.inc @@ -525,14 +525,17 @@ function drupal_write_record($table, &$record, $primary_keys = array()) { * The converted value. */ function drupal_schema_get_field_value(array $info, $value) { - if ($info['type'] == 'int' || $info['type'] == 'serial') { - $value = (int) $value; - } - elseif ($info['type'] == 'float') { - $value = (float) $value; - } - else { - $value = (string) $value; + // Preserve legal NULL values. + if (isset($value) || !empty($info['not null'])) { + if ($info['type'] == 'int' || $info['type'] == 'serial') { + $value = (int) $value; + } + elseif ($info['type'] == 'float') { + $value = (float) $value; + } + else { + $value = (string) $value; + } } return $value; } diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php index 2753133..a61a773 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php @@ -336,6 +336,7 @@ public function save(EntityInterface $entity) { } } else { + $entity->enforceIsNew(); $return = drupal_write_record($this->entityInfo['base_table'], $record); $entity->{$this->idKey}->value = $record->{$this->idKey}; if ($this->revisionKey) { @@ -461,9 +462,19 @@ protected function savePropertyData(EntityInterface $entity, $revision = FALSE) protected function mapToStorageRecord(EntityInterface $entity, $table_key = 'base_table') { $record = new \stdClass(); $definitions = $entity->getPropertyDefinitions(); + $schema = drupal_get_schema($this->entityInfo[$table_key]); + $is_new = $entity->isNew(); + foreach (drupal_schema_fields_sql($this->entityInfo[$table_key]) as $name) { - $record->$name = $entity->$name->value; + $info = $schema['fields'][$name]; + $value = isset($definitions[$name]) && isset($entity->$name->value) ? $entity->$name->value : NULL; + // 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->$name = drupal_schema_get_field_value($info, $value); + } } + return $record; } @@ -493,20 +504,9 @@ protected function mapToRevisionStorageRecord(EntityInterface $entity) { * The record to store. */ protected function mapToDataStorageRecord(EntityInterface $entity, $table_key = 'data_table') { - $langcode = $entity->language()->id; - $default_langcode = $entity->getUntranslated()->language()->id; - $definitions = $entity->getPropertyDefinitions(); - $schema = drupal_get_schema($this->entityInfo[$table_key]); - - $record = new \stdClass(); - foreach (drupal_schema_fields_sql($this->entityInfo[$table_key]) as $name) { - $info = $schema['fields'][$name]; - $value = isset($definitions[$name]) && isset($entity->$name->value) ? $entity->$name->value : NULL; - $record->$name = drupal_schema_get_field_value($info, $value); - } - $record->langcode = $langcode; - $record->default_langcode = intval($default_langcode == $langcode); - + $record = $this->mapToStorageRecord($entity, $table_key); + $record->langcode = $entity->language()->id; + $record->default_langcode = intval($record->langcode == $entity->getUntranslated()->language()->id); return $record; } diff --git a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php index df5012b..db8feb2 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php @@ -169,7 +169,7 @@ protected function entityValues($entity_type) { 'field_test_text' => array(0 => array('value' => $this->randomString())), ); case 'node': - return array('title' => $this->randomString(), 'type' => 'resttest', 'status' => TRUE); + return array('title' => $this->randomString(), 'type' => 'resttest'); case 'node_type': return array( 'type' => 'article',