diff --git a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php index 0b1709b..4c0467c 100644 --- a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php +++ b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php @@ -177,20 +177,21 @@ public function getUrl() { * {@inheritdoc} */ public function setValue($values, $notify = TRUE) { - // The main property for LinkItem is uri so if value is not array then it - // should be a uri. + // Treat the values as property value of the main property, if no array is + // given. if (isset($values) && !is_array($values)) { - $values = ['uri' => $values]; + $values = [static::mainPropertyName() => $values]; + } + if (isset($values)) { + $values += [ + 'options' => [], + ]; } - $values += [ - 'title' => NULL, - 'options' => [], - ]; // Unserialize the values. // @todo The storage controller should take care of this, see // SqlContentEntityStorage::loadFieldItems, see // https://www.drupal.org/node/2414835 - if (isset($values['options']) && is_string($values['options'])) { + if (is_string($values['options'])) { $values['options'] = unserialize($values['options']); } parent::setValue($values, $notify); diff --git a/core/modules/link/src/Tests/LinkItemTest.php b/core/modules/link/src/Tests/LinkItemTest.php index 352d909..a476853 100644 --- a/core/modules/link/src/Tests/LinkItemTest.php +++ b/core/modules/link/src/Tests/LinkItemTest.php @@ -101,20 +101,40 @@ public function testLinkItem() { $this->assertEqual($entity->field_test->title, $new_title); $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class); - // Test the generateSampleValue() method. - $entity = entity_create('entity_test'); - $entity->field_test->generateSampleItems(); - $this->entityValidateAndSave($entity); - + // Check that if we only set uri the default values for title and options + // are also initialized. $entity->field_test = ['uri' => 'internal:/node/add']; $this->assertEqual($entity->field_test->uri, 'internal:/node/add'); $this->assertNull($entity->field_test->title); $this->assertIdentical($entity->field_test->options, []); + // Check that if set uri and serialize options then the default values are + // properly initialized. + $entity->field_test = [ + 'uri' => 'internal:/node/add', + 'options' => serialize(['query' => NULL]), + ]; + $this->assertEqual($entity->field_test->uri, 'internal:/node/add'); + $this->assertNull($entity->field_test->title); + $this->assertNull($entity->field_test->options['query']); + + // Check that if we set the direct value of link field it correctly set the + // uri and the default values of the field. $entity->field_test = 'internal:/node/add'; $this->assertEqual($entity->field_test->uri, 'internal:/node/add'); $this->assertNull($entity->field_test->title); $this->assertIdentical($entity->field_test->options, []); + + // Check that setting LinkItem value NULL doesn't generate any error or + // warning. + $entity->field_test[0] = NULL; + $this->assertNull($entity->field_test[0]->getValue()); + + + // Test the generateSampleValue() method. + $entity = entity_create('entity_test'); + $entity->field_test->generateSampleItems(); + $this->entityValidateAndSave($entity); } }