Commit 530a6eda authored by alexpott's avatar alexpott

Issue #1957888 by ParisLiakos, Berdir: Fixed Exception when a field is empty...

Issue #1957888 by ParisLiakos, Berdir: Fixed Exception when a field is empty on programmatic creation.
parent bdf02865
......@@ -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();
}
}
......
......@@ -27,6 +27,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.
*
......
......@@ -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();
}));
}
}
......
......@@ -97,6 +97,12 @@ 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();
}
/**
......
......@@ -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);
}
}
}
}
......
......@@ -108,6 +108,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();
}
}
......@@ -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();
......
......@@ -50,4 +50,12 @@ public function getPropertyDefinitions() {
}
return static::$propertyDefinitions;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')->getValue();
return $value === NULL || $value === '';
}
}
......@@ -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 === '');
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment