From 678df35b2b45cc34abcaa01af5adf00fb95826b7 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 11 Sep 2013 00:20:54 +0200 Subject: [PATCH] Issue #2061331 by yched, plopesc: Added helper methods getEntity() in FieldInterface / FieldItemInterface. --- core/lib/Drupal/Core/Entity/Field/Field.php | 7 ++++++ .../Core/Entity/Field/FieldInterface.php | 8 ++++++ .../Core/Entity/Field/FieldItemBase.php | 7 ++++++ .../Core/Entity/Field/FieldItemInterface.php | 8 ++++++ .../EntityChangedConstraintValidator.php | 4 +-- .../lib/Drupal/comment/CommentNewValue.php | 3 +-- .../ConfigEntityReferenceItemBase.php | 3 +-- .../Plugin/Type/FieldType/ConfigField.php | 25 ++++++++----------- .../field/field_type/LegacyConfigField.php | 13 +++------- .../field_type/LegacyConfigFieldItem.php | 8 +++--- .../EntityReferenceItemNormalizer.php | 2 +- .../Drupal/hal/Normalizer/FieldNormalizer.php | 2 +- .../Plugin/field/widget/OptionsWidgetBase.php | 2 +- .../system/Tests/Entity/EntityFieldTest.php | 2 ++ .../widget/TaxonomyAutocompleteWidget.php | 2 +- 15 files changed, 57 insertions(+), 39 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/Field/Field.php b/core/lib/Drupal/Core/Entity/Field/Field.php index 3a0734b089e9..6f8382a44599 100644 --- a/core/lib/Drupal/Core/Entity/Field/Field.php +++ b/core/lib/Drupal/Core/Entity/Field/Field.php @@ -56,6 +56,13 @@ class Field extends ItemList implements FieldInterface { $this->list[0] = $this->createItem(0); } + /** + * {@inheritdoc} + */ + public function getEntity() { + return $this->getParent(); + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Entity/Field/FieldInterface.php b/core/lib/Drupal/Core/Entity/Field/FieldInterface.php index 80b0412ffc1a..4708418970f5 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldInterface.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldInterface.php @@ -27,6 +27,14 @@ use Drupal\Core\TypedData\ListInterface; */ interface FieldInterface extends ListInterface, AccessibleInterface { + /** + * Gets the entity that field belongs to. + * + * @return \Drupal\Core\Entity\EntityInterface + * The entity object. + */ + public function getEntity(); + /** * Sets the langcode of the field values held in the object. * diff --git a/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php b/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php index 0ac0a5d489c9..15744c0d5e5b 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php @@ -36,6 +36,13 @@ abstract class FieldItemBase extends Map implements FieldItemInterface { } } + /** + * {@inheritdoc} + */ + public function getEntity() { + return $this->getParent()->getEntity(); + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Entity/Field/FieldItemInterface.php b/core/lib/Drupal/Core/Entity/Field/FieldItemInterface.php index e1f5d51b1e4b..71fc08646a7e 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldItemInterface.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldItemInterface.php @@ -23,6 +23,14 @@ use Drupal\Core\TypedData\ComplexDataInterface; */ interface FieldItemInterface extends ComplexDataInterface { + /** + * Gets the entity that field belongs to. + * + * @return \Drupal\Core\Entity\EntityInterface + * The entity object. + */ + public function getEntity(); + /** * Gets the langcode of the field values held in the object. * diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EntityChangedConstraintValidator.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EntityChangedConstraintValidator.php index ca3d99ffd90d..79ce7c917fcb 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EntityChangedConstraintValidator.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EntityChangedConstraintValidator.php @@ -21,9 +21,7 @@ class EntityChangedConstraintValidator extends ConstraintValidator { */ public function validate($value, Constraint $constraint) { if (isset($value)) { - // We are on the field item level, so we need to go two levels up for the - // entity object. - $entity = $this->context->getMetadata()->getTypedData()->getParent()->getParent(); + $entity = $this->context->getMetadata()->getTypedData()->getEntity(); if (!$entity->isNew()) { $saved_entity = \Drupal::entityManager()->getStorageController($entity->entityType())->loadUnchanged($entity->id()); diff --git a/core/modules/comment/lib/Drupal/comment/CommentNewValue.php b/core/modules/comment/lib/Drupal/comment/CommentNewValue.php index 1295776e91b5..4398a1beee95 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentNewValue.php +++ b/core/modules/comment/lib/Drupal/comment/CommentNewValue.php @@ -26,8 +26,7 @@ class CommentNewValue extends TypedData { if (!isset($this->parent)) { throw new InvalidArgumentException('Computed properties require context for computation.'); } - $field = $this->parent->getParent(); - $entity = $field->getParent(); + $entity = $this->parent->getEntity(); $this->value = node_mark($entity->nid->target_id, $entity->changed->value); } return $this->value; diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php index 443e509d918d..6b3c1379e71f 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php @@ -147,8 +147,7 @@ class ConfigEntityReferenceItemBase extends EntityReferenceItem implements Confi if (function_exists($callback)) { // We are at the field item level, so we need to go two levels up to get // to the entity object. - $entity = $this->getParent()->getParent(); - return $callback($this->getFieldDefinition(), $entity); + return $callback($this->getFieldDefinition(), $this->getEntity()); } } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php index eb2e4dbbf792..2eb001334603 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php @@ -37,8 +37,9 @@ class ConfigField extends Field implements ConfigFieldInterface { * {@inheritdoc} */ public function getFieldDefinition() { - if (!isset($this->instance) && $parent = $this->getParent()) { - $instances = FieldAPI::fieldInfo()->getBundleInstances($parent->entityType(), $parent->bundle()); + if (!isset($this->instance)) { + $entity = $this->getEntity(); + $instances = FieldAPI::fieldInfo()->getBundleInstances($entity->entityType(), $entity->bundle()); $this->instance = $instances[$this->getName()]; } return $this->instance; @@ -69,7 +70,7 @@ class ConfigField extends Field implements ConfigFieldInterface { * {@inheritdoc} */ protected function getDefaultValue() { - return $this->getFieldDefinition()->getFieldDefaultValue($this->getParent()); + return $this->getFieldDefinition()->getFieldDefaultValue($this->getEntity()); } /** @@ -77,12 +78,11 @@ class ConfigField extends Field implements ConfigFieldInterface { */ public function defaultValuesForm(array &$form, array &$form_state) { if (empty($this->getFieldDefinition()->default_value_function)) { - $entity = $this->getParent(); $widget = $this->defaultValueWidget($form_state); // Place the input in a separate place in the submitted values tree. $element = array('#parents' => array('default_value_input')); - $element += $widget->form($entity, $entity->language()->id, $this, $element, $form_state); + $element += $widget->form($this->getEntity(), $this->getLangcode(), $this, $element, $form_state); return $element; } @@ -92,11 +92,11 @@ class ConfigField extends Field implements ConfigFieldInterface { * {@inheritdoc} */ public function defaultValuesFormValidate(array $element, array &$form, array &$form_state) { - $entity = $this->getParent(); - $langcode = $entity->language()->id; - $widget = $this->defaultValueWidget($form_state); + $entity = $this->getEntity(); + $langcode = $this->getLangcode(); // Extract the submitted value, and validate it. + $widget = $this->defaultValueWidget($form_state); $widget->extractFormValues($entity, $langcode, $this, $element, $form_state); $violations = $this->validate(); @@ -116,12 +116,9 @@ class ConfigField extends Field implements ConfigFieldInterface { * {@inheritdoc} */ public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) { - $entity = $this->getParent(); - $langcode = $entity->language()->id; - $widget = $this->defaultValueWidget($form_state); - // Extract the submitted value, and return it as an array. - $widget->extractFormValues($entity, $langcode, $this, $element, $form_state); + $widget = $this->defaultValueWidget($form_state); + $widget->extractFormValues($this->getEntity(), $this->getLangcode(), $this, $element, $form_state); return $this->getValue(); } @@ -136,7 +133,7 @@ class ConfigField extends Field implements ConfigFieldInterface { */ protected function defaultValueWidget(array &$form_state) { if (!isset($form_state['default_value_widget'])) { - $entity = $this->getParent(); + $entity = $this->getEntity(); // Force a non-required widget. $this->getFieldDefinition()->required = FALSE; diff --git a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php index ce171badf369..f99b0c61204c 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php +++ b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigField.php @@ -7,7 +7,6 @@ namespace Drupal\field\Plugin\field\field_type; -use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\Type\FieldType\ConfigField; use Drupal\field\FieldInstanceInterface; use Symfony\Component\Validator\ConstraintViolation; @@ -40,8 +39,7 @@ class LegacyConfigField extends ConfigField { $legacy_errors = array(); $this->legacyCallback('validate', array(&$legacy_errors)); - $entity = $this->getParent(); - $langcode = $entity->language()->id; + $langcode = $this->getLangcode(); $field_name = $this->getFieldDefinition()->getFieldName(); if (isset($legacy_errors[$field_name][$langcode])) { @@ -108,19 +106,16 @@ class LegacyConfigField extends ConfigField { $module = $definition['provider']; $callback = "{$module}_field_{$hook}"; if (function_exists($callback)) { - $entity = $this->getParent(); - $langcode = $entity->language()->id; - // We need to remove the empty "prototype" item here. // @todo Revisit after http://drupal.org/node/1988492. $this->filterEmptyValues(); - // Legcacy callbacks alter $items by reference. + // Legacy callbacks alter $items by reference. $items = (array) $this->getValue(TRUE); $args = array_merge(array( - $entity, + $this->getEntity(), $this->getFieldInstance()->getField(), $this->getFieldInstance(), - $langcode, + $this->getLangcode(), &$items ), $args); call_user_func_array($callback, $args); diff --git a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItem.php b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItem.php index 10fa23d36978..e46622fb7422 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItem.php +++ b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItem.php @@ -88,8 +88,7 @@ abstract class LegacyConfigFieldItem extends ConfigFieldItemBase implements Prep */ public function prepareCache() { if ($callback = $this->getLegacyCallback('load')) { - $entity = $this->getParent()->getParent(); - $langcode = $entity->language()->id; + $entity = $this->getEntity(); $entity_id = $entity->id(); // hook_field_load() receives items keyed by entity id, and alters then by @@ -100,7 +99,7 @@ abstract class LegacyConfigFieldItem extends ConfigFieldItemBase implements Prep array($entity_id => $entity), $this->getFieldInstance()->getField(), array($entity_id => $this->getFieldInstance()), - $langcode, + $this->getLangcode(), &$items, EntityStorageControllerInterface::FIELD_LOAD_CURRENT, ); @@ -120,8 +119,7 @@ abstract class LegacyConfigFieldItem extends ConfigFieldItemBase implements Prep $definition = $this->getPluginDefinition(); $callback = "{$definition['provider']}_options_list"; if (function_exists($callback)) { - $entity = $this->getParent()->getParent(); - return $callback($this->getFieldDefinition(), $entity); + return $callback($this->getFieldDefinition(), $this->getEntity()); } } diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php index 71436543cf7f..a4f54f0d85d2 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php @@ -47,7 +47,7 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR // entity so that the items are properly added to the _links and _embedded // objects. $field_name = $field_item->getParent()->getName(); - $entity = $field_item->getRoot(); + $entity = $field_item->getEntity(); $field_uri = $this->linkManager->getRelationUri($entity->entityType(), $entity->bundle(), $field_name); return array( '_links' => array( diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php index 725fca35b097..71d667297695 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php @@ -31,7 +31,7 @@ class FieldNormalizer extends NormalizerBase { $normalized_field_items = array(); // Get the field definition. - $entity = $field->getParent(); + $entity = $field->getEntity(); $field_name = $field->getName(); $field_definition = $entity->getPropertyDefinition($field_name); diff --git a/core/modules/options/lib/Drupal/options/Plugin/field/widget/OptionsWidgetBase.php b/core/modules/options/lib/Drupal/options/Plugin/field/widget/OptionsWidgetBase.php index f218438b8694..5081195f568b 100644 --- a/core/modules/options/lib/Drupal/options/Plugin/field/widget/OptionsWidgetBase.php +++ b/core/modules/options/lib/Drupal/options/Plugin/field/widget/OptionsWidgetBase.php @@ -143,7 +143,7 @@ abstract class OptionsWidgetBase extends WidgetBase { $module_handler = \Drupal::moduleHandler(); $context = array( 'fieldDefinition' => $this->fieldDefinition, - 'entity' => $item->getParent()->getParent(), + 'entity' => $item->getEntity(), ); $module_handler->alter('options_list', $options, $context); 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 12de782c7355..ebc328dacd89 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php @@ -391,12 +391,14 @@ class EntityFieldTest extends EntityUnitTestBase { $field = $entity->user_id; $this->assertIdentical($field->getRoot(), $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.'); $field_item = $field[0]; $this->assertIdentical($field_item->getRoot(), $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.'); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php index e651e8bdce1c..21a3a2c76a1c 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php @@ -75,7 +75,7 @@ class TaxonomyAutocompleteWidget extends WidgetBase { '#default_value' => taxonomy_implode_tags($tags), '#autocomplete_route_name' => $this->getSetting('autocomplete_route_name'), '#autocomplete_route_parameters' => array( - 'entity_type' => $items->getParent()->entityType(), + 'entity_type' => $items->getEntity()->entityType(), 'field_name' => $this->fieldDefinition->getFieldName(), ), '#size' => $this->getSetting('size'),