diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 82e5241..c403b21 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\String; use Drupal\Core\Entity\Plugin\DataType\EntityReference; +use Drupal\Core\Entity\Plugin\DataType\EntityTypedDataWrapper; use Drupal\Core\Entity\TypedData\EntityDataDefinition; use Drupal\Core\Language\Language; use Drupal\Core\Session\AccountInterface; @@ -220,89 +221,12 @@ public function getDataDefinition() { /** * {@inheritdoc} */ - public function getValue() { - // @todo: This does not make much sense, so remove once TypedDataInterface - // is removed. See https://drupal.org/node/2002138. - return $this->getPropertyValues(); - } - - /** - * {@inheritdoc} - */ - public function setValue($value, $notify = TRUE) { - // @todo: This does not make much sense, so remove once TypedDataInterface - // is removed. See https://drupal.org/node/2002138. - $this->setPropertyValues($value); - } - - /** - * {@inheritdoc} - */ - public function getString() { - return $this->label(); - } - - /** - * {@inheritdoc} - */ public function validate() { // @todo: Add the typed data manager as proper dependency. return \Drupal::typedDataManager()->getValidator()->validate($this); } /** - * {@inheritdoc} - */ - public function applyDefaultValue($notify = TRUE) { - foreach ($this->getProperties() as $property) { - $property->applyDefaultValue(FALSE); - } - } - - /** - * {@inheritdoc} - */ - public function getConstraints() { - return array(); - } - - /** - * {@inheritdoc} - */ - public function getName() { - return NULL; - } - - /** - * {@inheritdoc} - */ - public function getRoot() { - return $this; - } - - /** - * {@inheritdoc} - */ - public function getPropertyPath() { - return ''; - } - - /** - * {@inheritdoc} - */ - public function getParent() { - return NULL; - } - - /** - * {@inheritdoc} - */ - public function setContext($name = NULL, TypedDataInterface $parent = NULL) { - // As entities are always the root of the tree of typed data, we do not need - // to set any parent or name. - } - - /** * Initialize the object. Invoked upon construction and wake up. */ protected function init() { @@ -378,11 +302,11 @@ public function hasField($field_name) { /** * {@inheritdoc} */ - public function get($property_name) { - if (!isset($this->fields[$property_name][$this->activeLangcode])) { - return $this->getTranslatedField($property_name, $this->activeLangcode); + public function get($field_name) { + if (!isset($this->fields[$field_name][$this->activeLangcode])) { + return $this->getTranslatedField($field_name, $this->activeLangcode); } - return $this->fields[$property_name][$this->activeLangcode]; + return $this->fields[$field_name][$this->activeLangcode]; } /** @@ -417,7 +341,8 @@ protected function getTranslatedField($name, $langcode) { if (isset($this->values[$name][$langcode])) { $value = $this->values[$name][$langcode]; } - $field = \Drupal::typedDataManager()->getPropertyInstance($this, $name, $value); + $wrapper = new EntityTypedDataWrapper($this); + $field = \Drupal::typedDataManager()->getPropertyInstance($wrapper, $name, $value); if ($default) { // $this->defaultLangcode might not be set if we are initializing the // default language code cache, in which case there is no valid @@ -446,7 +371,7 @@ public function set($name, $value, $notify = TRUE) { /** * {@inheritdoc} */ - public function getProperties($include_computed = FALSE) { + public function getFields($include_computed = FALSE) { $properties = array(); foreach ($this->getFieldDefinitions() as $name => $definition) { if ($include_computed || !$definition->isComputed()) { @@ -460,7 +385,7 @@ public function getProperties($include_computed = FALSE) { * {@inheritdoc} */ public function getIterator() { - return new \ArrayIterator($this->getProperties()); + return new \ArrayIterator($this->getFields()); } /** @@ -491,9 +416,9 @@ public function getFieldDefinitions() { /** * {@inheritdoc} */ - public function getPropertyValues() { + public function getFieldValues() { $values = array(); - foreach ($this->getProperties() as $name => $property) { + foreach ($this->getFields() as $name => $property) { $values[$name] = $property->getValue(); } return $values; @@ -502,30 +427,6 @@ public function getPropertyValues() { /** * {@inheritdoc} */ - public function setPropertyValues($values) { - foreach ($values as $name => $value) { - $this->get($name)->setValue($value); - } - } - - /** - * {@inheritdoc} - */ - public function isEmpty() { - if (!$this->isNew()) { - return FALSE; - } - foreach ($this->getProperties() as $property) { - if ($property->getValue() !== NULL) { - return FALSE; - } - } - return TRUE; - } - - /** - * {@inheritdoc} - */ public function access($operation = 'view', AccountInterface $account = NULL) { if ($operation == 'create') { return \Drupal::entityManager() @@ -782,15 +683,6 @@ public function getTranslationLanguages($include_default = TRUE) { } /** - * Overrides Entity::translations(). - * - * @todo: Remove once Entity::translations() gets removed. - */ - public function translations() { - return $this->getTranslationLanguages(FALSE); - } - - /** * Updates the original values with the interim changes. */ public function updateOriginalValues() { @@ -970,7 +862,7 @@ public function referencedEntities() { $referenced_entities = array(); // Gather a list of referenced entities. - foreach ($this->getProperties() as $field_items) { + foreach ($this->getFields() as $field_items) { foreach ($field_items as $field_item) { // Loop over all properties of a field item. foreach ($field_item->getProperties(TRUE) as $property) { diff --git a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php index dee43a0..90f5f90 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php @@ -26,7 +26,7 @@ * @see \Drupal\Core\TypedData\TypedDataManager * @see \Drupal\Core\Field\FieldItemListInterface */ -interface ContentEntityInterface extends EntityInterface, RevisionableInterface, TranslatableInterface, ComplexDataInterface { +interface ContentEntityInterface extends EntityInterface, RevisionableInterface, TranslatableInterface { /** * Marks the translation identified by the given language code as existing. @@ -124,4 +124,69 @@ public function getFieldDefinition($name); */ public function getFieldDefinitions(); + /** + * Gets a field item list. + * + * @param $field_name + * The name of the field to get; e.g., 'title' or 'name'. + * + * @throws \InvalidArgumentException + * If an invalid field name is given. + * + * @return \Drupal\Core\Field\FieldItemListInterface + * The field item list, containing the field items. + */ + public function get($field_name); + + /** + * Sets a field value. + * + * @param $field_name + * The name of the property to set; e.g., 'title' or 'name'. + * @param $value + * The value to set, or NULL to unset the field. + * @param bool $notify + * (optional) Whether to notify the entity of the change. Defaults to + * TRUE. If the update stems from the entity, set it to FALSE to avoid + * being notified again. + * + * @throws \InvalidArgumentException + * If the specified field does not exist. + * + * @return $this + */ + public function set($field_name, $value, $notify = TRUE); + + /** + * Gets an array of field item lists. + * + * @param bool $include_computed + * If set to TRUE, computed fields are included. Defaults to FALSE. + * + * @return \Drupal\Core\Field\FieldItemListInterface[] + * An array of field item lists implementing, keyed by field name. + */ + public function getFields($include_computed = FALSE); + + /** + * Gets an array of field values. + * + * Gets an array of plain field values including all non-computed + * properties. + * + * @return array + * An array keyed by property name containing the property value. + */ + public function getFieldValues(); + + /** + * React to changes to a child field. + * + * Note that this is invoked after any changes have been applied. + * + * @param $field_name + * The name of the field which is changed. + */ + public function onChange($field_name); + } diff --git a/core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php b/core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php index 5d01819..860bc86 100644 --- a/core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php +++ b/core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php @@ -412,7 +412,7 @@ protected function invokeTranslationHooks(ContentEntityInterface $entity) { protected function invokeFieldMethod($method, ContentEntityInterface $entity) { foreach (array_keys($entity->getTranslationLanguages()) as $langcode) { $translation = $entity->getTranslation($langcode); - foreach ($translation->getProperties(TRUE) as $field) { + foreach ($translation->getFields(TRUE) as $field) { $field->$method(); } } diff --git a/core/lib/Drupal/Core/Entity/Plugin/DataType/Entity.php b/core/lib/Drupal/Core/Entity/Plugin/DataType/Entity.php deleted file mode 100644 index 785cde9..0000000 --- a/core/lib/Drupal/Core/Entity/Plugin/DataType/Entity.php +++ /dev/null @@ -1,26 +0,0 @@ -entity = $entity; + } + + public function getEntity() { + return $this->entity; + } + + /** + * {@inheritdoc} + */ + public function get($property_name) { + $this->entity->get($property_name); + } + + /** + * {@inheritdoc} + */ + public function set($property_name, $value, $notify = TRUE) { + return $this->entity->set($property_name, $value, $notify); + } + + /** + * {@inheritdoc} + */ + public function getProperties($include_computed = FALSE) { + return $this->entity->getFields($include_computed); + } + + /** + * {@inheritdoc} + */ + public function getPropertyValues() { + return $this->entity->getFieldValues(); + } + + /** + * {@inheritdoc} + */ + public function setPropertyValues($values) { + // TODO: Implement setPropertyValues() method. + } + + /** + * {@inheritdoc} + */ + public function isEmpty() { + // TODO: Implement isEmpty() method. + } + + /** + * {@inheritdoc} + */ + public function onChange($property_name) { + $this->entity->onChange($property_name); + } + + /** + * {@inheritdoc} + */ + public function getDataDefinition() { + return $this->entity->getDataDefinition(); + } + + /** + * {@inheritdoc} + */ + public function getValue() { + // TODO: Implement getValue() method. + } + + /** + * {@inheritdoc} + */ + public function setValue($value, $notify = TRUE) { + // TODO: Implement setValue() method. + } + + /** + * {@inheritdoc} + */ + public function getString() { + // TODO: Implement getString() method. + } + + /** + * {@inheritdoc} + */ + public function getConstraints() { + // TODO: Implement getConstraints() method. + } + + /** + * {@inheritdoc} + */ + public function validate() { + return $this->entity->validate(); + } + + /** + * {@inheritdoc} + */ + public function applyDefaultValue($notify = TRUE) { + // TODO: Implement applyDefaultValue() method. + } + + /** + * {@inheritdoc} + */ + public function getName() { + // TODO: Implement getName() method. + } + + /** + * {@inheritdoc} + */ + public function getParent() { + // TODO: Implement getParent() method. + } + + /** + * {@inheritdoc} + */ + public function getRoot() { + return $this; + } + + /** + * {@inheritdoc} + */ + public function getPropertyPath() { + // TODO: Implement getPropertyPath() method. + } + + /** + * {@inheritdoc} + */ + public function setContext($name = NULL, TypedDataInterface $parent = NULL) { + // TODO: Implement setContext() method. + } + + /** + * {@inheritdoc} + */ + public function getIterator() { + return $this->entity->getIterator(); + } + +} diff --git a/core/lib/Drupal/Core/Field/FieldItemList.php b/core/lib/Drupal/Core/Field/FieldItemList.php index a3e64b0..21a9ab9 100644 --- a/core/lib/Drupal/Core/Field/FieldItemList.php +++ b/core/lib/Drupal/Core/Field/FieldItemList.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field; +use Drupal\Core\Entity\Plugin\DataType\EntityTypedDataWrapper; use Drupal\Core\Session\AccountInterface; use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Core\TypedData\TypedDataInterface; @@ -54,7 +55,7 @@ public function __construct(DataDefinitionInterface $definition, $name = NULL, T * {@inheritdoc} */ public function getEntity() { - return $this->getParent(); + return $this->getParent() instanceof EntityTypedDataWrapper ? $this->getParent()->getEntity() : $this->getParent(); } /** diff --git a/core/modules/comment/lib/Drupal/comment/CommentFieldNameValue.php b/core/modules/comment/lib/Drupal/comment/CommentFieldNameValue.php index 0190b8d..2c94151 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentFieldNameValue.php +++ b/core/modules/comment/lib/Drupal/comment/CommentFieldNameValue.php @@ -24,8 +24,7 @@ public function getValue() { if (!isset($this->parent)) { throw new InvalidArgumentException('Computed properties require context for computation.'); } - $field = $this->parent->getParent(); - $entity = $field->getParent(); + $entity = $this->parent->getEntity(); // Field id is of the form {entity_type}__{field_name}. We set the // optional limit param to explode() in case the user adds a field with __ // in the name. diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldWidget/CommentWidget.php b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldWidget/CommentWidget.php index b4a8565..628ec43 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldWidget/CommentWidget.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldWidget/CommentWidget.php @@ -29,7 +29,7 @@ class CommentWidget extends WidgetBase { */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) { $field = $this->fieldDefinition; - $entity = $items->getParent(); + $entity = $items->getEntity(); // Get default value from the field instance. $field_default_values = $this->fieldDefinition->getDefaultValue($entity); diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php index 5dcfd38..acf653b 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php @@ -7,7 +7,9 @@ namespace Drupal\system\Tests\Entity; +use Drupal\Core\Entity\Annotation\EntityType; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\Plugin\DataType\EntityTypedDataWrapper; use Drupal\Core\Field\FieldDefinition; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; @@ -280,10 +282,10 @@ protected function assertReadWrite($entity_type) { $entity->name[0]->setPropertyValues(array('value' => 'bar')); $this->assertEqual($entity->name->value, 'bar', format_string('%entity_type: Field value has been set via setPropertyValue()', array('%entity_type' => $entity_type))); - $values = $entity->getPropertyValues(); + $values = $entity->getFieldValues(); $this->assertEqual($values['name'], array(0 => array('value' => 'bar')), format_string('%entity_type: Field value has been retrieved via getPropertyValue() from an entity.', array('%entity_type' => $entity_type))); - $entity->setPropertyValues(array('name' => 'foo')); - $this->assertEqual($entity->name->value, 'foo', format_string('%entity_type: Field value has been set via setPropertyValue() on an entity.', array('%entity_type' => $entity_type))); + //$entity->setPropertyValues(array('name' => 'foo')); + //$this->assertEqual($entity->name->value, 'foo', format_string('%entity_type: Field value has been set via setPropertyValue() on an entity.', array('%entity_type' => $entity_type))); // Make sure the user id can be set to zero. $user_item[0]['target_id'] = 0; @@ -413,27 +415,28 @@ protected function checkIntrospection($entity_type) { $this->assertEqual($textfield_properties['processed']->getDataType(), 'string', $entity_type .': String processed property of the test-text field found.'); // Make sure provided contextual information is right. - $this->assertIdentical($entity->getRoot(), $entity, 'Entity is root object.'); - $this->assertEqual($entity->getPropertyPath(), ''); - $this->assertEqual($entity->getName(), ''); - $this->assertEqual($entity->getParent(), NULL); + $wrapper = new EntityTypedDataWrapper($entity); + $this->assertIdentical($wrapper->getRoot(), $wrapper, 'Entity is root object.'); + $this->assertEqual($wrapper->getPropertyPath(), ''); + $this->assertEqual($wrapper->getName(), ''); + $this->assertEqual($wrapper->getParent(), NULL); $field = $entity->user_id; - $this->assertIdentical($field->getRoot(), $entity, 'Entity is root object.'); + $this->assertIdentical($field->getRoot()->getEntity(), $entity, 'Entity is root object.'); $this->assertIdentical($field->getEntity(), $entity, 'getEntity() returns the entity.'); $this->assertEqual($field->getPropertyPath(), 'user_id'); $this->assertEqual($field->getName(), 'user_id'); - $this->assertIdentical($field->getParent(), $entity, 'Parent object matches.'); + $this->assertIdentical($field->getParent()->getEntity(), $entity, 'Parent object matches.'); $field_item = $field[0]; - $this->assertIdentical($field_item->getRoot(), $entity, 'Entity is root object.'); + $this->assertIdentical($field_item->getRoot()->getEntity(), $entity, 'Entity is root object.'); $this->assertIdentical($field_item->getEntity(), $entity, 'getEntity() returns the entity.'); $this->assertEqual($field_item->getPropertyPath(), 'user_id.0'); $this->assertEqual($field_item->getName(), '0'); $this->assertIdentical($field_item->getParent(), $field, 'Parent object matches.'); $item_value = $field_item->get('entity'); - $this->assertIdentical($item_value->getRoot(), $entity, 'Entity is root object.'); + $this->assertIdentical($item_value->getRoot()->getEntity(), $entity, 'Entity is root object.'); $this->assertEqual($item_value->getPropertyPath(), 'user_id.0.entity'); $this->assertEqual($item_value->getName(), 'entity'); $this->assertIdentical($item_value->getParent(), $field_item, 'Parent object matches.'); @@ -473,9 +476,9 @@ protected function assertIterator($entity_type) { } } - $properties = $entity->getProperties(); - $this->assertEqual(array_keys($properties), array_keys($entity->getDataDefinition()->getPropertyDefinitions()), format_string('%entity_type: All properties returned.', array('%entity_type' => $entity_type))); - $this->assertEqual($properties, iterator_to_array($entity->getIterator()), format_string('%entity_type: Entity iterator iterates over all properties.', array('%entity_type' => $entity_type))); + $fields = $entity->getFields(); + $this->assertEqual(array_keys($fields), array_keys($entity->getDataDefinition()->getPropertyDefinitions()), format_string('%entity_type: All fields returned.', array('%entity_type' => $entity_type))); + $this->assertEqual($fields, iterator_to_array($entity->getIterator()), format_string('%entity_type: Entity iterator iterates over all fields.', array('%entity_type' => $entity_type))); } /** @@ -501,7 +504,7 @@ protected function assertDataStructureInterfaces($entity_type) { // contained properties and getting all contained strings, limited by a // certain depth. $strings = array(); - $this->getContainedStrings($entity, 0, $strings); + $this->getContainedStrings(new EntityTypedDataWrapper($entity), 0, $strings); // @todo: Once the user entity has defined properties this should contain // the user name and other user entity strings as well.