Issue #2061331 by yched, plopesc: Added helper methods getEntity() in FieldInterface / FieldItemInterface.
parent
742908a681
commit
678df35b2b
|
@ -56,6 +56,13 @@ class Field extends ItemList implements FieldInterface {
|
||||||
$this->list[0] = $this->createItem(0);
|
$this->list[0] = $this->createItem(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getEntity() {
|
||||||
|
return $this->getParent();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -27,6 +27,14 @@ use Drupal\Core\TypedData\ListInterface;
|
||||||
*/
|
*/
|
||||||
interface FieldInterface extends ListInterface, AccessibleInterface {
|
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.
|
* Sets the langcode of the field values held in the object.
|
||||||
*
|
*
|
||||||
|
|
|
@ -36,6 +36,13 @@ abstract class FieldItemBase extends Map implements FieldItemInterface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getEntity() {
|
||||||
|
return $this->getParent()->getEntity();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -23,6 +23,14 @@ use Drupal\Core\TypedData\ComplexDataInterface;
|
||||||
*/
|
*/
|
||||||
interface FieldItemInterface extends 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.
|
* Gets the langcode of the field values held in the object.
|
||||||
*
|
*
|
||||||
|
|
|
@ -21,9 +21,7 @@ class EntityChangedConstraintValidator extends ConstraintValidator {
|
||||||
*/
|
*/
|
||||||
public function validate($value, Constraint $constraint) {
|
public function validate($value, Constraint $constraint) {
|
||||||
if (isset($value)) {
|
if (isset($value)) {
|
||||||
// We are on the field item level, so we need to go two levels up for the
|
$entity = $this->context->getMetadata()->getTypedData()->getEntity();
|
||||||
// entity object.
|
|
||||||
$entity = $this->context->getMetadata()->getTypedData()->getParent()->getParent();
|
|
||||||
if (!$entity->isNew()) {
|
if (!$entity->isNew()) {
|
||||||
$saved_entity = \Drupal::entityManager()->getStorageController($entity->entityType())->loadUnchanged($entity->id());
|
$saved_entity = \Drupal::entityManager()->getStorageController($entity->entityType())->loadUnchanged($entity->id());
|
||||||
|
|
||||||
|
|
|
@ -26,8 +26,7 @@ class CommentNewValue extends TypedData {
|
||||||
if (!isset($this->parent)) {
|
if (!isset($this->parent)) {
|
||||||
throw new InvalidArgumentException('Computed properties require context for computation.');
|
throw new InvalidArgumentException('Computed properties require context for computation.');
|
||||||
}
|
}
|
||||||
$field = $this->parent->getParent();
|
$entity = $this->parent->getEntity();
|
||||||
$entity = $field->getParent();
|
|
||||||
$this->value = node_mark($entity->nid->target_id, $entity->changed->value);
|
$this->value = node_mark($entity->nid->target_id, $entity->changed->value);
|
||||||
}
|
}
|
||||||
return $this->value;
|
return $this->value;
|
||||||
|
|
|
@ -147,8 +147,7 @@ class ConfigEntityReferenceItemBase extends EntityReferenceItem implements Confi
|
||||||
if (function_exists($callback)) {
|
if (function_exists($callback)) {
|
||||||
// We are at the field item level, so we need to go two levels up to get
|
// We are at the field item level, so we need to go two levels up to get
|
||||||
// to the entity object.
|
// to the entity object.
|
||||||
$entity = $this->getParent()->getParent();
|
return $callback($this->getFieldDefinition(), $this->getEntity());
|
||||||
return $callback($this->getFieldDefinition(), $entity);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,8 +37,9 @@ class ConfigField extends Field implements ConfigFieldInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getFieldDefinition() {
|
public function getFieldDefinition() {
|
||||||
if (!isset($this->instance) && $parent = $this->getParent()) {
|
if (!isset($this->instance)) {
|
||||||
$instances = FieldAPI::fieldInfo()->getBundleInstances($parent->entityType(), $parent->bundle());
|
$entity = $this->getEntity();
|
||||||
|
$instances = FieldAPI::fieldInfo()->getBundleInstances($entity->entityType(), $entity->bundle());
|
||||||
$this->instance = $instances[$this->getName()];
|
$this->instance = $instances[$this->getName()];
|
||||||
}
|
}
|
||||||
return $this->instance;
|
return $this->instance;
|
||||||
|
@ -69,7 +70,7 @@ class ConfigField extends Field implements ConfigFieldInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
protected function getDefaultValue() {
|
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) {
|
public function defaultValuesForm(array &$form, array &$form_state) {
|
||||||
if (empty($this->getFieldDefinition()->default_value_function)) {
|
if (empty($this->getFieldDefinition()->default_value_function)) {
|
||||||
$entity = $this->getParent();
|
|
||||||
$widget = $this->defaultValueWidget($form_state);
|
$widget = $this->defaultValueWidget($form_state);
|
||||||
|
|
||||||
// Place the input in a separate place in the submitted values tree.
|
// Place the input in a separate place in the submitted values tree.
|
||||||
$element = array('#parents' => array('default_value_input'));
|
$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;
|
return $element;
|
||||||
}
|
}
|
||||||
|
@ -92,11 +92,11 @@ class ConfigField extends Field implements ConfigFieldInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function defaultValuesFormValidate(array $element, array &$form, array &$form_state) {
|
public function defaultValuesFormValidate(array $element, array &$form, array &$form_state) {
|
||||||
$entity = $this->getParent();
|
$entity = $this->getEntity();
|
||||||
$langcode = $entity->language()->id;
|
$langcode = $this->getLangcode();
|
||||||
$widget = $this->defaultValueWidget($form_state);
|
|
||||||
|
|
||||||
// Extract the submitted value, and validate it.
|
// Extract the submitted value, and validate it.
|
||||||
|
$widget = $this->defaultValueWidget($form_state);
|
||||||
$widget->extractFormValues($entity, $langcode, $this, $element, $form_state);
|
$widget->extractFormValues($entity, $langcode, $this, $element, $form_state);
|
||||||
$violations = $this->validate();
|
$violations = $this->validate();
|
||||||
|
|
||||||
|
@ -116,12 +116,9 @@ class ConfigField extends Field implements ConfigFieldInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) {
|
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.
|
// 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();
|
return $this->getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +133,7 @@ class ConfigField extends Field implements ConfigFieldInterface {
|
||||||
*/
|
*/
|
||||||
protected function defaultValueWidget(array &$form_state) {
|
protected function defaultValueWidget(array &$form_state) {
|
||||||
if (!isset($form_state['default_value_widget'])) {
|
if (!isset($form_state['default_value_widget'])) {
|
||||||
$entity = $this->getParent();
|
$entity = $this->getEntity();
|
||||||
|
|
||||||
// Force a non-required widget.
|
// Force a non-required widget.
|
||||||
$this->getFieldDefinition()->required = FALSE;
|
$this->getFieldDefinition()->required = FALSE;
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
|
|
||||||
namespace Drupal\field\Plugin\field\field_type;
|
namespace Drupal\field\Plugin\field\field_type;
|
||||||
|
|
||||||
use Drupal\Core\Entity\EntityInterface;
|
|
||||||
use Drupal\field\Plugin\Type\FieldType\ConfigField;
|
use Drupal\field\Plugin\Type\FieldType\ConfigField;
|
||||||
use Drupal\field\FieldInstanceInterface;
|
use Drupal\field\FieldInstanceInterface;
|
||||||
use Symfony\Component\Validator\ConstraintViolation;
|
use Symfony\Component\Validator\ConstraintViolation;
|
||||||
|
@ -40,8 +39,7 @@ class LegacyConfigField extends ConfigField {
|
||||||
$legacy_errors = array();
|
$legacy_errors = array();
|
||||||
$this->legacyCallback('validate', array(&$legacy_errors));
|
$this->legacyCallback('validate', array(&$legacy_errors));
|
||||||
|
|
||||||
$entity = $this->getParent();
|
$langcode = $this->getLangcode();
|
||||||
$langcode = $entity->language()->id;
|
|
||||||
$field_name = $this->getFieldDefinition()->getFieldName();
|
$field_name = $this->getFieldDefinition()->getFieldName();
|
||||||
|
|
||||||
if (isset($legacy_errors[$field_name][$langcode])) {
|
if (isset($legacy_errors[$field_name][$langcode])) {
|
||||||
|
@ -108,19 +106,16 @@ class LegacyConfigField extends ConfigField {
|
||||||
$module = $definition['provider'];
|
$module = $definition['provider'];
|
||||||
$callback = "{$module}_field_{$hook}";
|
$callback = "{$module}_field_{$hook}";
|
||||||
if (function_exists($callback)) {
|
if (function_exists($callback)) {
|
||||||
$entity = $this->getParent();
|
|
||||||
$langcode = $entity->language()->id;
|
|
||||||
|
|
||||||
// We need to remove the empty "prototype" item here.
|
// We need to remove the empty "prototype" item here.
|
||||||
// @todo Revisit after http://drupal.org/node/1988492.
|
// @todo Revisit after http://drupal.org/node/1988492.
|
||||||
$this->filterEmptyValues();
|
$this->filterEmptyValues();
|
||||||
// Legcacy callbacks alter $items by reference.
|
// Legacy callbacks alter $items by reference.
|
||||||
$items = (array) $this->getValue(TRUE);
|
$items = (array) $this->getValue(TRUE);
|
||||||
$args = array_merge(array(
|
$args = array_merge(array(
|
||||||
$entity,
|
$this->getEntity(),
|
||||||
$this->getFieldInstance()->getField(),
|
$this->getFieldInstance()->getField(),
|
||||||
$this->getFieldInstance(),
|
$this->getFieldInstance(),
|
||||||
$langcode,
|
$this->getLangcode(),
|
||||||
&$items
|
&$items
|
||||||
), $args);
|
), $args);
|
||||||
call_user_func_array($callback, $args);
|
call_user_func_array($callback, $args);
|
||||||
|
|
|
@ -88,8 +88,7 @@ abstract class LegacyConfigFieldItem extends ConfigFieldItemBase implements Prep
|
||||||
*/
|
*/
|
||||||
public function prepareCache() {
|
public function prepareCache() {
|
||||||
if ($callback = $this->getLegacyCallback('load')) {
|
if ($callback = $this->getLegacyCallback('load')) {
|
||||||
$entity = $this->getParent()->getParent();
|
$entity = $this->getEntity();
|
||||||
$langcode = $entity->language()->id;
|
|
||||||
$entity_id = $entity->id();
|
$entity_id = $entity->id();
|
||||||
|
|
||||||
// hook_field_load() receives items keyed by entity id, and alters then by
|
// 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),
|
array($entity_id => $entity),
|
||||||
$this->getFieldInstance()->getField(),
|
$this->getFieldInstance()->getField(),
|
||||||
array($entity_id => $this->getFieldInstance()),
|
array($entity_id => $this->getFieldInstance()),
|
||||||
$langcode,
|
$this->getLangcode(),
|
||||||
&$items,
|
&$items,
|
||||||
EntityStorageControllerInterface::FIELD_LOAD_CURRENT,
|
EntityStorageControllerInterface::FIELD_LOAD_CURRENT,
|
||||||
);
|
);
|
||||||
|
@ -120,8 +119,7 @@ abstract class LegacyConfigFieldItem extends ConfigFieldItemBase implements Prep
|
||||||
$definition = $this->getPluginDefinition();
|
$definition = $this->getPluginDefinition();
|
||||||
$callback = "{$definition['provider']}_options_list";
|
$callback = "{$definition['provider']}_options_list";
|
||||||
if (function_exists($callback)) {
|
if (function_exists($callback)) {
|
||||||
$entity = $this->getParent()->getParent();
|
return $callback($this->getFieldDefinition(), $this->getEntity());
|
||||||
return $callback($this->getFieldDefinition(), $entity);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
|
||||||
// entity so that the items are properly added to the _links and _embedded
|
// entity so that the items are properly added to the _links and _embedded
|
||||||
// objects.
|
// objects.
|
||||||
$field_name = $field_item->getParent()->getName();
|
$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);
|
$field_uri = $this->linkManager->getRelationUri($entity->entityType(), $entity->bundle(), $field_name);
|
||||||
return array(
|
return array(
|
||||||
'_links' => array(
|
'_links' => array(
|
||||||
|
|
|
@ -31,7 +31,7 @@ class FieldNormalizer extends NormalizerBase {
|
||||||
$normalized_field_items = array();
|
$normalized_field_items = array();
|
||||||
|
|
||||||
// Get the field definition.
|
// Get the field definition.
|
||||||
$entity = $field->getParent();
|
$entity = $field->getEntity();
|
||||||
$field_name = $field->getName();
|
$field_name = $field->getName();
|
||||||
$field_definition = $entity->getPropertyDefinition($field_name);
|
$field_definition = $entity->getPropertyDefinition($field_name);
|
||||||
|
|
||||||
|
|
|
@ -143,7 +143,7 @@ abstract class OptionsWidgetBase extends WidgetBase {
|
||||||
$module_handler = \Drupal::moduleHandler();
|
$module_handler = \Drupal::moduleHandler();
|
||||||
$context = array(
|
$context = array(
|
||||||
'fieldDefinition' => $this->fieldDefinition,
|
'fieldDefinition' => $this->fieldDefinition,
|
||||||
'entity' => $item->getParent()->getParent(),
|
'entity' => $item->getEntity(),
|
||||||
);
|
);
|
||||||
$module_handler->alter('options_list', $options, $context);
|
$module_handler->alter('options_list', $options, $context);
|
||||||
|
|
||||||
|
|
|
@ -391,12 +391,14 @@ class EntityFieldTest extends EntityUnitTestBase {
|
||||||
|
|
||||||
$field = $entity->user_id;
|
$field = $entity->user_id;
|
||||||
$this->assertIdentical($field->getRoot(), $entity, 'Entity is root object.');
|
$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->getPropertyPath(), 'user_id');
|
||||||
$this->assertEqual($field->getName(), 'user_id');
|
$this->assertEqual($field->getName(), 'user_id');
|
||||||
$this->assertIdentical($field->getParent(), $entity, 'Parent object matches.');
|
$this->assertIdentical($field->getParent(), $entity, 'Parent object matches.');
|
||||||
|
|
||||||
$field_item = $field[0];
|
$field_item = $field[0];
|
||||||
$this->assertIdentical($field_item->getRoot(), $entity, 'Entity is root object.');
|
$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->getPropertyPath(), 'user_id.0');
|
||||||
$this->assertEqual($field_item->getName(), '0');
|
$this->assertEqual($field_item->getName(), '0');
|
||||||
$this->assertIdentical($field_item->getParent(), $field, 'Parent object matches.');
|
$this->assertIdentical($field_item->getParent(), $field, 'Parent object matches.');
|
||||||
|
|
|
@ -75,7 +75,7 @@ class TaxonomyAutocompleteWidget extends WidgetBase {
|
||||||
'#default_value' => taxonomy_implode_tags($tags),
|
'#default_value' => taxonomy_implode_tags($tags),
|
||||||
'#autocomplete_route_name' => $this->getSetting('autocomplete_route_name'),
|
'#autocomplete_route_name' => $this->getSetting('autocomplete_route_name'),
|
||||||
'#autocomplete_route_parameters' => array(
|
'#autocomplete_route_parameters' => array(
|
||||||
'entity_type' => $items->getParent()->entityType(),
|
'entity_type' => $items->getEntity()->entityType(),
|
||||||
'field_name' => $this->fieldDefinition->getFieldName(),
|
'field_name' => $this->fieldDefinition->getFieldName(),
|
||||||
),
|
),
|
||||||
'#size' => $this->getSetting('size'),
|
'#size' => $this->getSetting('size'),
|
||||||
|
|
Loading…
Reference in New Issue