Commit a18a231e authored by Dries's avatar Dries

Issue #2056721 by Berdir, effulgentsia: Remove or rename TypedDataInterface::getType().

parent c3be1f10
......@@ -431,24 +431,16 @@ public function getNGEntity() {
/**
* {@inheritdoc}
*/
public function getType() {
public function getDefinition() {
// @todo: This does not make much sense, so remove once TypedDataInterface
// is removed. See https://drupal.org/node/2002138.
if ($this->bundle() != $this->entityType()) {
return 'entity:' . $this->entityType() . ':' . $this->bundle();
$type = 'entity:' . $this->entityType() . ':' . $this->bundle();
}
return 'entity:' . $this->entityType();
}
/**
* {@inheritdoc}
*/
public function getDefinition() {
// @todo: This does not make much sense, so remove once TypedDataInterface
// is removed. See https://drupal.org/node/2002138.
return array(
'type' => $this->getType()
);
else {
$type = 'entity:' . $this->entityType();
}
return array('type' => $type);
}
/**
......
......@@ -429,13 +429,6 @@ public function getTranslation($langcode) {
return $this->decorated->getTranslation($langcode);
}
/**
* Forwards the call to the decorated entity.
*/
public function getType() {
return $this->decorated->getType();
}
/**
* Forwards the call to the decorated entity.
*/
......
......@@ -58,13 +58,6 @@ public function __construct(array $definition, $name = NULL, TypedDataInterface
$this->name = $name;
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getType().
*/
public function getType() {
return $this->definition['type'];
}
/**
* {@inheritdoc}
*/
......
......@@ -14,14 +14,6 @@
*/
interface TypedDataInterface {
/**
* Gets the data type.
*
* @return string
* The data type of the wrapped data.
*/
public function getType();
/**
* Gets the data definition.
*
......
......@@ -8,6 +8,8 @@
namespace Drupal\config\Tests;
use Drupal\Core\Config\TypedConfig;
use Drupal\Core\TypedData\Type\IntegerInterface;
use Drupal\Core\TypedData\Type\StringInterface;
use Drupal\simpletest\DrupalUnitTestBase;
/**
......@@ -162,14 +164,16 @@ function testSchemaData() {
// Try some simple properties.
$meta = config_typed()->get('system.site');
$property = $meta->get('name');
$this->assertTrue(is_a($property, 'Drupal\Core\TypedData\Plugin\DataType\String'), 'Got the right wrapper fo the site name property.');
$this->assertEqual($property->getType(), 'label', 'Got the right string type for site name data.');
$this->assertTrue($property instanceof StringInterface, 'Got the right wrapper fo the site name property.');
$this->assertEqual($property->getValue(), 'Drupal', 'Got the right string value for site name data.');
$definition = $property->getDefinition();
$this->assertTrue($definition['translatable'], 'Got the right translatability setting for site name data.');
$property = $meta->get('page')->get('front');
$this->assertTrue(is_a($property, 'Drupal\Core\TypedData\Plugin\DataType\String'), 'Got the right wrapper fo the page.front property.');
$this->assertEqual($property->getType(), 'path', 'Got the right type for page.front data (undefined).');
$this->assertTrue($property instanceof StringInterface, 'Got the right wrapper fo the page.front property.');
$this->assertEqual($property->getValue(), 'user', 'Got the right value for page.front data.');
$definition = $property->getDefinition();
$this->assertTrue(empty($definition['translatable']), 'Got the right translatability setting for page.front data.');
// Check nested array of properties.
$list = $meta->get('page');
......@@ -192,7 +196,7 @@ function testSchemaData() {
$uuid = key($effects->getValue());
$effect = $effects[$uuid];
$this->assertTrue(count($effect['data']) && $effect['id']->getValue() == 'image_scale', 'Got data for the image scale effect from metadata.');
$this->assertEqual($effect['data']['width']->getType(), 'integer', 'Got the right type for the scale effect width.');
$this->assertTrue($effect['data']['width'] instanceof IntegerInterface, 'Got the right type for the scale effect width.');
$this->assertEqual($effect['data']['width']->getValue(), 480, 'Got the right value for the scale effect width.' );
// Finally update some object using a configuration wrapper.
......
......@@ -11,6 +11,7 @@
use Drupal\Core\Entity\Field\FieldInterface;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\TypedData\Type\StringInterface;
use Drupal\Core\TypedData\TypedDataInterface;
/**
......@@ -492,7 +493,7 @@ protected function assertDataStructureInterfaces($entity_type) {
*/
public function getContainedStrings(TypedDataInterface $wrapper, $depth, array &$strings) {
if ($wrapper->getType() == 'string') {
if ($wrapper instanceof StringInterface) {
$strings[] = $wrapper->getValue();
}
......
......@@ -47,21 +47,13 @@ public function setUp() {
}
/**
* Creates a typed data object and executes some basic assertions.
* Creates a typed data object and ensures it implements TypedDataInterface.
*
* @see Drupal\Core\TypedData\TypedDataManager::create().
*/
protected function createTypedData($definition, $value = NULL, $name = NULL) {
// Save the type that was passed in so we can compare with it later.
$type = $definition['type'];
// Construct the object.
$data = $this->typedData->create($definition, $value, $name);
// Assert the definition of the wrapper.
$this->assertTrue($data instanceof \Drupal\Core\TypedData\TypedDataInterface, 'Typed data object is an instance of the typed data interface.');
$definition = $data->getDefinition();
$this->assertTrue(!empty($definition['type']), format_string('!type data definition was returned.', array('!type' => $definition['type'])));
// Assert that the correct type was constructed.
$this->assertEqual($data->getType(), $type, format_string('!type object returned type.', array('!type' => $definition['type'])));
return $data;
}
......@@ -71,6 +63,7 @@ protected function createTypedData($definition, $value = NULL, $name = NULL) {
public function testGetAndSet() {
// Boolean type.
$typed_data = $this->createTypedData(array('type' => 'boolean'), TRUE);
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\BooleanInterface, 'Typed data object is an instance of BooleanInterface.');
$this->assertTrue($typed_data->getValue() === TRUE, 'Boolean value was fetched.');
$this->assertEqual($typed_data->validate()->count(), 0);
$typed_data->setValue(FALSE);
......@@ -86,6 +79,7 @@ public function testGetAndSet() {
// String type.
$value = $this->randomString();
$typed_data = $this->createTypedData(array('type' => 'string'), $value);
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\StringInterface, 'Typed data object is an instance of StringInterface.');
$this->assertTrue($typed_data->getValue() === $value, 'String value was fetched.');
$this->assertEqual($typed_data->validate()->count(), 0);
$new_value = $this->randomString();
......@@ -103,6 +97,7 @@ public function testGetAndSet() {
// Integer type.
$value = rand();
$typed_data = $this->createTypedData(array('type' => 'integer'), $value);
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\IntegerInterface, 'Typed data object is an instance of IntegerInterface.');
$this->assertTrue($typed_data->getValue() === $value, 'Integer value was fetched.');
$this->assertEqual($typed_data->validate()->count(), 0);
$new_value = rand();
......@@ -119,6 +114,7 @@ public function testGetAndSet() {
// Float type.
$value = 123.45;
$typed_data = $this->createTypedData(array('type' => 'float'), $value);
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\FloatInterface, 'Typed data object is an instance of FloatInterface.');
$this->assertTrue($typed_data->getValue() === $value, 'Float value was fetched.');
$this->assertEqual($typed_data->validate()->count(), 0);
$new_value = 678.90;
......@@ -135,6 +131,7 @@ public function testGetAndSet() {
// Date Time type.
$value = '2014-01-01T20:00:00+00:00';
$typed_data = $this->createTypedData(array('type' => 'datetime_iso8601'), $value);
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\DateTimeInterface, 'Typed data object is an instance of DateTimeInterface.');
$this->assertTrue($typed_data->getValue() == $value, 'Date value was fetched.');
$this->assertEqual($typed_data->getValue(), $typed_data->getDateTime()->format('c'), 'Value representation of a date is ISO 8601');
$this->assertEqual($typed_data->validate()->count(), 0);
......@@ -160,6 +157,7 @@ public function testGetAndSet() {
// Timestamp type.
$value = REQUEST_TIME;
$typed_data = $this->createTypedData(array('type' => 'timestamp'), $value);
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\DateTimeInterface, 'Typed data object is an instance of DateTimeInterface.');
$this->assertTrue($typed_data->getValue() == $value, 'Timestamp value was fetched.');
$this->assertEqual($typed_data->validate()->count(), 0);
$new_value = REQUEST_TIME + 1;
......@@ -182,6 +180,7 @@ public function testGetAndSet() {
// DurationIso8601 type.
$value = 'PT20S';
$typed_data = $this->createTypedData(array('type' => 'duration_iso8601'), $value);
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\DurationInterface, 'Typed data object is an instance of DurationInterface.');
$this->assertIdentical($typed_data->getValue(), $value, 'DurationIso8601 value was fetched.');
$this->assertEqual($typed_data->validate()->count(), 0);
$typed_data->setValue('P40D');
......@@ -205,6 +204,7 @@ public function testGetAndSet() {
// Time span type.
$value = 20;
$typed_data = $this->createTypedData(array('type' => 'timespan'), $value);
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\DurationInterface, 'Typed data object is an instance of DurationInterface.');
$this->assertIdentical($typed_data->getValue(), $value, 'Time span value was fetched.');
$this->assertEqual($typed_data->validate()->count(), 0);
$typed_data->setValue(60 * 60 * 4);
......@@ -227,6 +227,7 @@ public function testGetAndSet() {
// URI type.
$uri = 'http://example.com/foo/';
$typed_data = $this->createTypedData(array('type' => 'uri'), $uri);
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\UriInterface, 'Typed data object is an instance of UriInterface.');
$this->assertTrue($typed_data->getValue() === $uri, 'URI value was fetched.');
$this->assertEqual($typed_data->validate()->count(), 0);
$typed_data->setValue($uri . 'bar.txt');
......@@ -253,6 +254,7 @@ public function testGetAndSet() {
// Email type.
$value = $this->randomString();
$typed_data = $this->createTypedData(array('type' => 'email'), $value);
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\StringInterface, 'Typed data object is an instance of StringInterface.');
$this->assertIdentical($typed_data->getValue(), $value, 'E-mail value was fetched.');
$new_value = 'test@example.com';
$typed_data->setValue($new_value);
......@@ -267,6 +269,7 @@ public function testGetAndSet() {
// Binary type.
$typed_data = $this->createTypedData(array('type' => 'binary'), $files[0]->getFileUri());
$this->assertTrue($typed_data instanceof \Drupal\Core\TypedData\Type\BinaryInterface, 'Typed data object is an instance of BinaryInterface.');
$this->assertTrue(is_resource($typed_data->getValue()), 'Binary value was fetched.');
$this->assertEqual($typed_data->validate()->count(), 0);
// Try setting by URI.
......
......@@ -71,16 +71,23 @@ public function isEmpty() {
* {@inheritdoc}
*/
public function prepareCache() {
// Where possible, generate the sanitized version of each field early so
// that it is cached in the field cache. This avoids the need to look up the
// field in the filter cache separately.
// Where possible, generate the sanitized version of each textual property
// (e.g., 'value', 'summary') within this field item early so that it is
// cached in the field cache. This avoids the need to look up the sanitized
// value in the filter cache separately.
$text_processing = $this->getFieldSetting('text_processing');
if (!$text_processing || filter_format_allowcache($this->get('format')->getValue())) {
$itemBC = $this->getValue();
$langcode = $this->getParent()->getParent()->language()->id;
$this->set('safe_value', text_sanitize($text_processing, $langcode, $itemBC, 'value'));
if ($this->getType() == 'field_item:text_with_summary') {
$this->set('safe_summary', text_sanitize($text_processing, $langcode, $itemBC, 'summary'));
// The properties that need sanitizing are the ones that are the 'text
// source' of a TextProcessed computed property.
// @todo Clean up this mess by making the TextProcessed property type
// support its own cache integration: https://drupal.org/node/2026339.
foreach ($this->getPropertyDefinitions() as $definition) {
if (isset($definition['class']) && ($definition['class'] == '\Drupal\text\TextProcessed')) {
$source_property = $definition['settings']['text source'];
$this->set('safe_' . $source_property, text_sanitize($text_processing, $langcode, $itemBC, $source_property));
}
}
}
}
......
......@@ -1080,13 +1080,6 @@ public function initTranslation($langcode) {
$this->storage->initTranslation($langcode);
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getType().
*/
public function getType() {
return $this->storage->getType();
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition().
*/
......
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