diff --git a/core/lib/Drupal/Core/Field/FieldItemBase.php b/core/lib/Drupal/Core/Field/FieldItemBase.php index b5b0f3f..37ffa55 100644 --- a/core/lib/Drupal/Core/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php @@ -26,6 +26,13 @@ /** * {@inheritdoc} */ + public static function getMainPropertyName() { + return 'value'; + } + + /** + * {@inheritdoc} + */ public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) { parent::__construct($definition, $name, $parent); // Initialize computed properties by default, such that they get cloned @@ -211,11 +218,4 @@ public function delete() { } */ public function deleteRevision() { } - /** - * {@inheritdoc} - */ - public function getMainPropertyName() { - return 'value'; - } - } diff --git a/core/lib/Drupal/Core/Field/FieldItemInterface.php b/core/lib/Drupal/Core/Field/FieldItemInterface.php index f378b86..ee5c06a 100644 --- a/core/lib/Drupal/Core/Field/FieldItemInterface.php +++ b/core/lib/Drupal/Core/Field/FieldItemInterface.php @@ -30,11 +30,25 @@ * An array of property definitions of contained properties, keyed by * property name. * - * @see \Drupal\Core\Field\FieldItemDefinition + * @see \Drupal\Core\Field\FieldDefinition */ public static function propertyDefinitions(FieldDefinitionInterface $field_definition); /** + * Returns the name of the main property, if any. + * + * Some field items consist mainly of one main property, e.g. the value of a + * text field or the @code target_id @endcode of an entity reference. If the + * field item has no main property, the method returns NULL. + * + * @return string|null + * The name of the value property, or NULL if there is none. + * + * @see \Drupal\Core\Field\FieldDefinition + */ + public static function getMainPropertyName(); + + /** * Gets the entity that field belongs to. * * @return \Drupal\Core\Entity\EntityInterface diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index f1623c8..97a9bec 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -72,6 +72,13 @@ public static function propertyDefinitions(FieldDefinitionInterface $field_defin /** * {@inheritdoc} */ + public static function getMainPropertyName() { + return 'target_id'; + } + + /** + * {@inheritdoc} + */ public function __get($name) { $name = ($name == 'value') ? 'target_id' : $name; return parent::__get($name); @@ -129,11 +136,4 @@ public function onChange($property_name) { parent::onChange($property_name); } - /** - * {@inheritdoc} - */ - public function getMainPropertyName() { - return 'target_id'; - } - } diff --git a/core/lib/Drupal/Core/TypedData/ComplexDataInterface.php b/core/lib/Drupal/Core/TypedData/ComplexDataInterface.php index 438ea6e..13dfa19 100644 --- a/core/lib/Drupal/Core/TypedData/ComplexDataInterface.php +++ b/core/lib/Drupal/Core/TypedData/ComplexDataInterface.php @@ -99,6 +99,10 @@ public function setPropertyValues($values); * * @return array|FALSE * The definition of the property or FALSE if the property does not exist. + * + * @deprecated + * Use $this->getDefinition()->getPropertyDefinition() instead). + * See \Drupal\Core\TypedData\ComplexDataDefinitionInterface. */ public function getPropertyDefinition($name); @@ -108,6 +112,10 @@ public function getPropertyDefinition($name); * @return \Drupal\Core\TypedData\DataDefinitionInterface[] * An array of property definitions of contained properties, keyed by * property name. + * + * @deprecated + * Use $this->getDefinition()->getPropertyDefinitions() instead). + * See \Drupal\Core\TypedData\ComplexDataDefinitionInterface. */ public function getPropertyDefinitions(); diff --git a/core/modules/system/lib/Drupal/system/Tests/TypedData/MetadataTest.php b/core/modules/system/lib/Drupal/system/Tests/TypedData/MetadataTest.php new file mode 100644 index 0000000..0ccda3a --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/TypedData/MetadataTest.php @@ -0,0 +1,116 @@ + 'Metadata', + 'description' => 'Tests reading and deriving metadata of core data types.', + 'group' => 'Typed Data API', + ); + } + + public function setUp() { + parent::setup(); + $this->typedDataManager = $this->container->get('typed_data_manager'); + } + + /** + * Tests deriving metadata about list items. + */ + public function testListMetadata() { + // @todo: Add a definition factory to the typed data manager. + $list = ListDefinition::create('string'); + $this->assertTrue($list instanceof ListDefinitionInterface); + $item = $list->getItemDefinition(); + $this->assertTrue($item instanceof DataDefinitionInterface); + $this->assertEqual($item->getDataType(), 'string'); + } + + /** + * Tests deriving metadata about maps. + */ + public function testTypedDataMaps() { + $map = MapDefinition::create('map') + ->setPropertyDefinition('one', DataDefinition::create('string')) + ->setPropertyDefinition('two', DataDefinition::create('string')) + ->setPropertyDefinition('three', DataDefinition::create('string')); + + $this->assertTrue($map instanceof ComplexDataDefinitionInterface); + + // Test retrieving metadata about contained properties. + $this->assertEqual(array_keys($map->getPropertyDefinitions()), array('one', 'two', 'three')); + $this->assertEqual($map->getPropertyDefinition('one')->getDataType(), 'string'); + $this->assertNull($map->getMainPropertyName()); + $this->assertNull($map->getPropertyDefinition('invalid')); + } + + /** + * Tests deriving metadata about fields. + */ + public function testFields() { + $field = FieldDefinition::create('integer'); + // Fields are lists of complex data. + $this->assertTrue($field instanceof ListDefinitionInterface); + $this->assertFalse($field instanceof ComplexDataDefinitionInterface); + $field_item = $field->getItemDefinition(); + $this->assertFalse($field_item instanceof ListDefinitionInterface); + $this->assertTrue($field_item instanceof ComplexDataDefinitionInterface); + + // Derive metadata about field item properties. + $this->assertEqual(array_keys($field_item->getPropertyDefinitions()), array('value')); + $this->assertEqual($field_item->getPropertyDefinition('value')->getDataType(), 'integer'); + $this->assertEqual($field_item->getMainPropertyName(), 'value'); + $this->assertNull($field_item->getPropertyDefinition('invalid')); + + // To ease access of field item property metadata, field definitions make + // metadata about field item properties on the field level as well. + $this->assertTrue($field instanceof FieldDefinitionInterface); + $this->assertEqual(array_keys($field->getPropertyDefinitions()), array('value')); + $this->assertEqual($field->getPropertyDefinition('value')->getDataType(), 'integer'); + $this->assertEqual($field->getMainPropertyName(), 'value'); + $this->assertNull($field->getPropertyDefinition('invalid')); + } + + /** + * Tests deriving metadata about entities. + */ + public function testEntities() { + // @todo: Implement. + } + +} diff --git a/core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php b/core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php index 2982685..2854045 100644 --- a/core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php @@ -57,7 +57,7 @@ protected function setUp() { * Tests field name methods. */ public function testFieldName() { - $definition = new FieldDefinition(); + $definition = FieldDefinition::create('integer'); $field_name = $this->randomName(); $definition->setName($field_name); $this->assertEquals($field_name, $definition->getName()); @@ -67,7 +67,7 @@ public function testFieldName() { * Tests field label methods. */ public function testFieldLabel() { - $definition = new FieldDefinition(); + $definition = FieldDefinition::create('integer'); $label = $this->randomName(); $definition->setLabel($label); $this->assertEquals($label, $definition->getLabel()); @@ -77,7 +77,7 @@ public function testFieldLabel() { * Tests field description methods. */ public function testFieldDescription() { - $definition = new FieldDefinition(); + $definition = FieldDefinition::create('integer'); $description = $this->randomName(); $definition->setDescription($description); $this->assertEquals($description, $definition->getDescription()); @@ -96,7 +96,7 @@ public function testFieldType() { * Tests field settings methods. */ public function testFieldSettings() { - $definition = new FieldDefinition(); + $definition = FieldDefinition::create('integer'); $setting = $this->randomName(); $value = $this->randomName(); $definition->setSetting($setting, $value); @@ -108,7 +108,7 @@ public function testFieldSettings() { * Tests field default value. */ public function testFieldDefaultValue() { - $definition = new FieldDefinition(); + $definition = FieldDefinition::create('integer'); $setting = 'default_value'; $value = $this->randomName(); $definition->setSetting($setting, $value); @@ -122,7 +122,7 @@ public function testFieldDefaultValue() { * Tests field translatable methods. */ public function testFieldTranslatable() { - $definition = new FieldDefinition(); + $definition = FieldDefinition::create('integer'); $this->assertFalse($definition->isTranslatable()); $definition->setTranslatable(TRUE); $this->assertTrue($definition->isTranslatable()); @@ -134,7 +134,7 @@ public function testFieldTranslatable() { * Tests field cardinality. */ public function testFieldCardinality() { - $definition = new FieldDefinition(); + $definition = FieldDefinition::create('integer'); $this->assertEquals(1, $definition->getCardinality()); // @todo: Add more tests when this can be controlled. } @@ -143,7 +143,7 @@ public function testFieldCardinality() { * Tests required. */ public function testFieldRequired() { - $definition = new FieldDefinition(); + $definition = FieldDefinition::create('integer'); $this->assertFalse($definition->isRequired()); $definition->setRequired(TRUE); $this->assertTrue($definition->isRequired()); @@ -155,7 +155,7 @@ public function testFieldRequired() { * Tests configurable. */ public function testFieldConfigurable() { - $definition = new FieldDefinition(); + $definition = FieldDefinition::create('integer'); $this->assertFalse($definition->isConfigurable()); }