Commit 2733bf20 authored by catch's avatar catch

Issue #1839080 by Berdir, fago, dasjo, jjchinquist: Implement the new entity...

Issue #1839080 by Berdir, fago, dasjo, jjchinquist: Implement the new entity field API for the link field type.
parent ee5b4a70
......@@ -34,7 +34,7 @@ class Map extends ContextAwareTypedData implements \IteratorAggregate, ComplexDa
*
* @var array
*/
protected $properties;
protected $properties = array();
/**
* Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
......@@ -53,6 +53,10 @@ public function getPropertyDefinitions() {
* Overrides \Drupal\Core\TypedData\TypedData::getValue().
*/
public function getValue() {
// Update the values and return them.
foreach ($this->properties as $name => $property) {
$this->values[$name] = $property->getValue();
}
return $this->values;
}
......@@ -67,7 +71,7 @@ public function setValue($values) {
throw new \InvalidArgumentException("Invalid values given. Values must be represented as an associative array.");
}
$this->values = $values;
unset($this->properties);
$this->properties = array();
}
/**
......@@ -99,7 +103,13 @@ public function get($property_name) {
* Implements \Drupal\Core\TypedData\ComplexDataInterface::set().
*/
public function set($property_name, $value) {
$this->get($property_name)->setValue($value);
if (isset($this->properties[$property_name])) {
$this->properties[$property_name]->setValue($value);
}
else {
// Just the plain value, so it's possible to add a new entry to the map.
$this->values[$property_name] = $value;
}
}
/**
......
<?php
/**
* @file
* Contains \Drupal\link\Tests\LinkItemTest.
*/
namespace Drupal\link\Tests;
use Drupal\Core\Entity\Field\FieldInterface;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\field\Tests\FieldUnitTestBase;
/**
* Tests the new entity API for the link field type.
*/
class LinkItemTest extends FieldUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('link');
public static function getInfo() {
return array(
'name' => 'Link field item',
'description' => 'Tests the new entity API for the link field type.',
'group' => 'Field types',
);
}
public function setUp() {
parent::setUp();
// Create an link field and instance for validation.
$this->field = array(
'field_name' => 'field_test',
'type' => 'link',
);
field_create_field($this->field);
$this->instance = array(
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
'widget' => array(
'type' => 'link_default',
),
);
field_create_instance($this->instance);
}
/**
* Tests using entity fields of the link field type.
*/
public function testLinkItem() {
// Create entity.
$entity = entity_create('entity_test', array());
$url = 'http://www.drupal.org';
$title = $this->randomName();
$class = $this->randomName();
$entity->field_test->url = $url;
$entity->field_test->title = $title;
$entity->field_test->get('attributes')->set('class', $class);
$entity->name->value = $this->randomName();
$entity->save();
// Verify that the field value is changed.
$id = $entity->id();
$entity = entity_load('entity_test', $id);
$this->assertTrue($entity->field_test instanceof FieldInterface, 'Field implements interface.');
$this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_test->url, $url);
$this->assertEqual($entity->field_test[0]->url, $url);
$this->assertEqual($entity->field_test->title, $title);
$this->assertEqual($entity->field_test[0]->title, $title);
$this->assertEqual($entity->field_test->attributes['class'], $class);
// Verify changing the field value.
$new_url = 'http://drupal.org';
$new_title = $this->randomName();
$new_class = $this->randomName();
$entity->field_test->url = $new_url;
$entity->field_test->title = $new_title;
$entity->field_test->get('attributes')->set('class', $new_class);
$this->assertEqual($entity->field_test->url, $new_url);
$this->assertEqual($entity->field_test->title, $new_title);
$this->assertEqual($entity->field_test->attributes['class'], $new_class);
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
$this->assertEqual($entity->field_test->url, $new_url);
$this->assertEqual($entity->field_test->title, $new_title);
$this->assertEqual($entity->field_test->attributes['class'], $new_class);
}
}
<?php
/**
* @file
* Contains \Drupal\link\Type\LinkItem.
*/
namespace Drupal\link\Type;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'link_field' entity field item.
*/
class LinkItem extends FieldItemBase {
/**
* Property definitions of the contained properties.
*
* @see self::getPropertyDefinitions()
*
* @var array
*/
static $propertyDefinitions;
/**
* Implements ComplexDataInterface::getPropertyDefinitions().
*/
public function getPropertyDefinitions() {
if (!isset(self::$propertyDefinitions)) {
self::$propertyDefinitions['url'] = array(
'type' => 'uri',
'label' => t('URL'),
);
self::$propertyDefinitions['title'] = array(
'type' => 'string',
'label' => t('Title'),
);
self::$propertyDefinitions['attributes'] = array(
'type' => 'map',
'label' => t('Attributes'),
);
}
return self::$propertyDefinitions;
}
}
......@@ -32,6 +32,7 @@ function link_field_info() {
),
'default_widget' => 'link_default',
'default_formatter' => 'link',
'field item class' => '\Drupal\link\Type\LinkItem',
);
return $types;
}
......
......@@ -342,6 +342,13 @@ public function testTypedDataMaps() {
$this->assertEqual($typed_data->getPropertyValues(), $value);
$typed_data->set('one', 'uno');
$this->assertEqual($typed_data->get('one')->getValue(), 'uno');
// Make sure the update is reflected in the value of the map also.
$value = $typed_data->getValue();
$this->assertEqual($value, array(
'one' => 'uno',
'two' => 'zwei',
'three' => 'drei'
));
$properties = $typed_data->getProperties();
$this->assertEqual(array_keys($properties), array_keys($value));
......@@ -383,13 +390,6 @@ public function testTypedDataMaps() {
catch (\Exception $e) {
$this->pass('Exception thrown:' . $e->getMessage());
}
try {
$typed_data->set('invalid', 'foo');
$this->fail('No exception has been thrown when setting an invalid value.');
}
catch (\Exception $e) {
$this->pass('Exception thrown:' . $e->getMessage());
}
// Test setting invalid values.
try {
......@@ -399,6 +399,12 @@ public function testTypedDataMaps() {
catch (\Exception $e) {
$this->pass('Exception thrown:' . $e->getMessage());
}
// Test adding a new entry to the map.
$typed_data->set('zero', 'null');
$this->assertEqual($typed_data->get('zero')->getValue(), 'null');
$definition = $typed_data->getPropertyDefinition('zero');
$this->assertEqual($definition['type'], 'any', 'Definition for a new map entry returned.');
}
/**
......
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