diff --git a/core/lib/Drupal/Core/Entity/EntityNG.php b/core/lib/Drupal/Core/Entity/EntityNG.php index bbc3b84..d9d17c2 100644 --- a/core/lib/Drupal/Core/Entity/EntityNG.php +++ b/core/lib/Drupal/Core/Entity/EntityNG.php @@ -387,6 +387,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->filterEmptyValues(); $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..dc05036 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldInterface.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldInterface.php @@ -28,6 +28,11 @@ interface FieldInterface extends ListInterface, AccessibleInterface { /** + * Filters out empty field items and re-numbers the item deltas. + */ + public function filterEmptyValues(); + + /** * 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 5d0a4ff..4829bb7 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/Field.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/Field.php @@ -46,20 +46,13 @@ public function __construct(array $definition, $name = NULL, TypedDataInterface } /** - * Overrides \Drupal\Core\TypedData\ItemList::getValue(). + * {@inheritdoc} */ - public function getValue() { + public function filterEmptyValues() { if (isset($this->list)) { - $values = array(); - foreach ($this->list as $delta => $item) { - if (!$item->isEmpty()) { - $values[$delta] = $item->getValue(); - } - else { - $values[$delta] = NULL; - } - } - return $values; + $this->list = array_values(array_filter($this->list, function($item) { + return !$item->isEmpty(); + })); } } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php index c8bed9a..13e2c38 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php @@ -97,5 +97,11 @@ public function testEntityReferenceItem() { $entity->field_test_taxonomy->target_id = $term2->id(); $this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->id()); $this->assertEqual($entity->field_test_taxonomy->entity->name->value, $term2->name->value); + + // Delete terms so we have nothing to reference and try again + $term->delete(); + $term2->delete(); + $entity = entity_create('entity_test', array('name' => $this->randomName())); + $entity->save(); } } diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index 20e8631..ef2865f 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -334,10 +334,13 @@ function forum_node_presave(EntityInterface $node) { $langcode = key($node->taxonomy_forums); if (!empty($node->taxonomy_forums[$langcode])) { $node->forum_tid = $node->taxonomy_forums[$langcode][0]['tid']; - $old_tid = db_query_range("SELECT f.tid FROM {forum} f INNER JOIN {node} n ON f.vid = n.vid WHERE n.nid = :nid ORDER BY f.vid DESC", 0, 1, array(':nid' => $node->nid))->fetchField(); - if ($old_tid && isset($node->forum_tid) && ($node->forum_tid != $old_tid) && !empty($node->shadow)) { - // A shadow copy needs to be created. Retain new term and add old term. - $node->taxonomy_forums[$langcode][] = array('tid' => $old_tid); + // Only do a shadow copy check if this is not a new node. + if (!$node->isNew()) { + $old_tid = db_query_range("SELECT f.tid FROM {forum} f INNER JOIN {node} n ON f.vid = n.vid WHERE n.nid = :nid ORDER BY f.vid DESC", 0, 1, array(':nid' => $node->nid))->fetchField(); + if ($old_tid && isset($node->forum_tid) && ($node->forum_tid != $old_tid) && !empty($node->shadow)) { + // A shadow copy needs to be created. Retain new term and add old term. + $node->taxonomy_forums[$langcode][] = array('tid' => $old_tid); + } } } } diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php index 7f863cf..73cf876 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php @@ -111,6 +111,11 @@ public function testImageItem() { // Check that the image item can be set to the referenced file directly. $entity->image_test = $this->image; $this->assertEqual($entity->image_test->fid, $this->image->id()); + + // Delete the image and try to save the entity again. + $this->image->delete(); + $entity = entity_create('entity_test', array('mame' => $this->randomName())); + $entity->save(); } } diff --git a/core/modules/text/lib/Drupal/text/Type/TextItem.php b/core/modules/text/lib/Drupal/text/Type/TextItem.php index d7aefc7..b71812f 100644 --- a/core/modules/text/lib/Drupal/text/Type/TextItem.php +++ b/core/modules/text/lib/Drupal/text/Type/TextItem.php @@ -50,4 +50,12 @@ public function getPropertyDefinitions() { } return static::$propertyDefinitions; } + + /** + * {@inheritdoc} + */ + public function isEmpty() { + $value = $this->get('value')->getValue(); + return $value === NULL || $value === ''; + } } diff --git a/core/modules/text/lib/Drupal/text/Type/TextSummaryItem.php b/core/modules/text/lib/Drupal/text/Type/TextSummaryItem.php index 90dc8b9..32a7444 100644 --- a/core/modules/text/lib/Drupal/text/Type/TextSummaryItem.php +++ b/core/modules/text/lib/Drupal/text/Type/TextSummaryItem.php @@ -47,4 +47,12 @@ public function getPropertyDefinitions() { } return static::$propertyDefinitions; } + + /** + * Overrides \Drupal\text\Type\TextItem::isEmpty(). + */ + public function isEmpty() { + $value = $this->get('summary')->getValue(); + return parent::isEmpty() && ($value === NULL || $value === ''); + } }