Commit e5d87b1e authored by alexpott's avatar alexpott

Issue #2502913 by jibran, larowlan: Link field default options values should be array

parent 2c0f5e10
......@@ -177,11 +177,21 @@ public function getUrl() {
* {@inheritdoc}
*/
public function setValue($values, $notify = TRUE) {
// Treat the values as property value of the main property, if no array is
// given.
if (isset($values) && !is_array($values)) {
$values = [static::mainPropertyName() => $values];
}
if (isset($values)) {
$values += [
'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);
......
......@@ -101,6 +101,35 @@ public function testLinkItem() {
$this->assertEqual($entity->field_test->title, $new_title);
$this->assertEqual($entity->field_test->options['attributes']['class'], $new_class);
// 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();
......
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