diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php index 1baa6ff..4b647aa 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php @@ -99,7 +99,7 @@ public function create(array $values) { $bundle = FALSE; if ($this->bundleKey) { if (!isset($values[$this->bundleKey])) { - throw new EntityStorageException(t('Missing bundle for entity type @type', array('@type' => $this->entityType))); + throw new EntityStorageException(format_string('Missing bundle for entity type @type', array('@type' => $this->entityType))); } $bundle = $values[$this->bundleKey]; } @@ -112,6 +112,12 @@ public function create(array $values) { elseif (!array_key_exists($name, $values)) { $entity->get($name)->applyDefaultValue(); } + unset($values[$name]); + } + + // Set any passed values for non-defined fields also. + foreach ($values as $name => $value) { + $entity->$name = $value; } // Modules might need to add or change the data initially held by the new diff --git a/core/lib/Drupal/Core/Entity/EntityNG.php b/core/lib/Drupal/Core/Entity/EntityNG.php index 0aeb37f..00f546e 100644 --- a/core/lib/Drupal/Core/Entity/EntityNG.php +++ b/core/lib/Drupal/Core/Entity/EntityNG.php @@ -383,6 +383,7 @@ public function updateOriginalValues() { foreach ($this->getPropertyDefinitions() as $name => $definition) { if (empty($definition['computed']) && !empty($this->fields[$name])) { foreach ($this->fields[$name] as $langcode => $field) { + $field->cleanValue(); $this->values[$name][$langcode] = $field->getValue(); } } diff --git a/core/lib/Drupal/Core/Entity/Field/FieldInterface.php b/core/lib/Drupal/Core/Entity/Field/FieldInterface.php index 7b1d235..63b6dd0 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldInterface.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldInterface.php @@ -28,6 +28,13 @@ interface FieldInterface extends ListInterface, AccessibleInterface { /** + * Cleans the field value. + * + * Removes empty field items and re-numbers the item deltas. + */ + public function cleanValue(); + + /** * Gets a property object from the first field item. * * @see \Drupal\Core\Entity\Field\FieldItemInterface::get() diff --git a/core/lib/Drupal/Core/Entity/Field/Type/Field.php b/core/lib/Drupal/Core/Entity/Field/Type/Field.php index 3f89f97..50cd51d 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/Field.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/Field.php @@ -49,20 +49,16 @@ public function __construct(array $definition, $name = NULL, TypedDataInterface } /** - * Overrides \Drupal\Core\TypedData\ItemList::getValue(). + * {@inheritdoc} */ - public function getValue() { + public function cleanValue() { if (isset($this->list)) { - $values = array(); foreach ($this->list as $delta => $item) { - if (!$item->isEmpty()) { - $values[$delta] = $item->getValue(); - } - else { - $values[$delta] = NULL; + if ($item->isEmpty()) { + unset($this->list[$delta]); } } - return $values; + $this->list = array_values($this->list); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldDefaultValueTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldDefaultValueTest.php index 74bbd96..f8e5501 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldDefaultValueTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldDefaultValueTest.php @@ -2,18 +2,13 @@ /** * @file - * Definition of Drupal\system\Tests\Entity\EntityFieldDefaultValueTest. + * Contains \Drupal\system\Tests\Entity\EntityFieldDefaultValueTest. */ namespace Drupal\system\Tests\Entity; -use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Entity\Field\FieldInterface; -use Drupal\Core\Entity\Field\FieldItemInterface; -use Drupal\Core\TypedData\TypedDataInterface; use Drupal\Component\Uuid\Uuid; - /** * Tests Entity API default field value functionality. */ @@ -50,7 +45,7 @@ public function setUp() { } /** - * Tests. + * Tests default values on entities and fields. */ public function testDefaultValues() { // All entity variations have to have the same results. @@ -69,5 +64,6 @@ protected function assertDefaultValues($entity_type) { $entity = entity_create($entity_type, array()); $this->assertEqual($entity->langcode->value, LANGUAGE_NOT_SPECIFIED, format_string('%entity_type: Default language', array('%entity_type' => $entity_type))); $this->assertTrue($this->uuid->isValid($entity->uuid->value), format_string('%entity_type: Default UUID', array('%entity_type' => $entity_type))); + $this->assertEqual($entity->name->getValue(), array(0 => array('value' => NULL)), 'Field has one empty value by default.'); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php index fabbcfe..29e1462 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php @@ -253,7 +253,7 @@ protected function assertReadWrite($entity_type) { $this->assertTrue($entity->name->isEmpty(), format_string('%entity_type: Name field is empty.', array('%entity_type' => $entity_type))); $this->assertEqual(count($entity->name), 1, format_string('%entity_type: Empty item is considered when counting.', array('%entity_type' => $entity_type))); $this->assertEqual(count(iterator_to_array($entity->name->getIterator())), count($entity->name), format_string('%entity_type: Count matches iterator count.', array('%entity_type' => $entity_type))); - $this->assertTrue($entity->name->getValue() === array(0 => NULL), format_string('%entity_type: Name field value contains a NULL value.', array('%entity_type' => $entity_type))); + $this->assertTrue($entity->name->getValue() === array(0 => array('value' => NULL)), format_string('%entity_type: Name field value contains a NULL value.', array('%entity_type' => $entity_type))); // Test removing all list items by assigning an empty array. $entity->name = array(); diff --git a/core/modules/views_ui/lib/Drupal/views_ui/ViewUI.php b/core/modules/views_ui/lib/Drupal/views_ui/ViewUI.php index 5877f28..ef07e78 100644 --- a/core/modules/views_ui/lib/Drupal/views_ui/ViewUI.php +++ b/core/modules/views_ui/lib/Drupal/views_ui/ViewUI.php @@ -1134,4 +1134,11 @@ public function setContext($name = NULL, TypedDataInterface $parent = NULL) { public function onChange($property_name) { $this->storage->onChange($property_name); } + + /** + * Implements \Drupal\Core\TypedData\TypedDataInterface::applyDefaultValue(). + */ + public function applyDefaultValue($notify = TRUE) { + $this->storage->applyDefaultValue($notify); + } }