Commit f6cc0f98 authored by Dries's avatar Dries

Issue #2074547 by plopesc, effulgentsia, yched: Remove ConfigFieldItemInterface::getInstance().

parent 1c545591
...@@ -128,7 +128,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) { ...@@ -128,7 +128,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function instanceSettingsForm(array $form, array &$form_state) { public function instanceSettingsForm(array $form, array &$form_state) {
$field_definition = isset($form_state['instance']) ? $form_state['instance'] : $this->getInstance(); $instance = $form_state['instance'];
// Get all selection plugins for this entity type. // Get all selection plugins for this entity type.
$selection_plugins = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionGroups($this->getFieldSetting('target_type')); $selection_plugins = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionGroups($this->getFieldSetting('target_type'));
...@@ -166,7 +166,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { ...@@ -166,7 +166,7 @@ public function instanceSettingsForm(array $form, array &$form_state) {
'#type' => 'select', '#type' => 'select',
'#title' => t('Reference method'), '#title' => t('Reference method'),
'#options' => $handlers_options, '#options' => $handlers_options,
'#default_value' => $field_definition->getFieldSetting('handler') ?: 'default', '#default_value' => $instance->getFieldSetting('handler'),
'#required' => TRUE, '#required' => TRUE,
'#ajax' => TRUE, '#ajax' => TRUE,
'#limit_validation_errors' => array(), '#limit_validation_errors' => array(),
...@@ -186,8 +186,8 @@ public function instanceSettingsForm(array $form, array &$form_state) { ...@@ -186,8 +186,8 @@ public function instanceSettingsForm(array $form, array &$form_state) {
'#attributes' => array('class' => array('entity_reference-settings')), '#attributes' => array('class' => array('entity_reference-settings')),
); );
$handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($field_definition); $handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($instance);
$form['handler']['handler_settings'] += $handler->settingsForm($field_definition); $form['handler']['handler_settings'] += $handler->settingsForm($instance);
return $form; return $form;
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
use Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem; use Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem;
use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface; use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface;
use Drupal\field\FieldInterface; use Drupal\field\FieldInterface;
use Drupal\field\FieldInstanceInterface;
/** /**
* A common base class for configurable entity reference fields. * A common base class for configurable entity reference fields.
...@@ -31,28 +32,6 @@ class ConfigEntityReferenceItemBase extends EntityReferenceItem implements Confi ...@@ -31,28 +32,6 @@ class ConfigEntityReferenceItemBase extends EntityReferenceItem implements Confi
*/ */
static $propertyDefinitions; static $propertyDefinitions;
/**
* The Field instance definition.
*
* @var \Drupal\field\Entity\FieldInstance
*/
protected $instance;
/**
* Returns the field instance definition.
*
* Copied from \Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase,
* since we cannot extend it.
*
* @var \Drupal\field\Entity\FieldInstance
*/
public function getInstance() {
if (!isset($this->instance) && $parent = $this->getParent()) {
$this->instance = $parent->getInstance();
}
return $this->instance;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -127,9 +106,13 @@ public function isEmpty() { ...@@ -127,9 +106,13 @@ public function isEmpty() {
*/ */
public function settingsForm(array $form, array &$form_state, $has_data) { public function settingsForm(array $form, array &$form_state, $has_data) {
if ($callback = $this->getLegacyCallback('settings_form')) { if ($callback = $this->getLegacyCallback('settings_form')) {
$instance = $this->getFieldDefinition();
if (!($instance instanceof FieldInstanceInterface)) {
throw new \UnexpectedValueException('ConfigEntityReferenceItemBase::settingsForm() called for a field whose definition is not a field instance.');
}
// hook_field_settings_form() used to receive the $instance (not actually // hook_field_settings_form() used to receive the $instance (not actually
// needed), and the value of field_has_data(). // needed), and the value of field_has_data().
return $callback($this->getInstance()->getField(), $this->getInstance(), $has_data); return $callback($instance->getField(), $instance, $has_data);
} }
return array(); return array();
} }
...@@ -142,7 +125,11 @@ public function settingsForm(array $form, array &$form_state, $has_data) { ...@@ -142,7 +125,11 @@ public function settingsForm(array $form, array &$form_state, $has_data) {
*/ */
public function instanceSettingsForm(array $form, array &$form_state) { public function instanceSettingsForm(array $form, array &$form_state) {
if ($callback = $this->getLegacyCallback('instance_settings_form')) { if ($callback = $this->getLegacyCallback('instance_settings_form')) {
return $callback($this->getInstance()->getField(), $this->getInstance(), $form_state); $instance = $this->getFieldDefinition();
if (!($instance instanceof FieldInstanceInterface)) {
throw new \UnexpectedValueException('ConfigEntityReferenceItemBase::instanceSettingsForm() called for a field whose definition is not a field instance.');
}
return $callback($instance->getField(), $instance, $form_state);
} }
return array(); return array();
} }
...@@ -161,7 +148,7 @@ public function getSettableOptions() { ...@@ -161,7 +148,7 @@ public function getSettableOptions() {
// 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(); $entity = $this->getParent()->getParent();
return $callback($this->getInstance(), $entity); return $callback($this->getFieldDefinition(), $entity);
} }
} }
......
...@@ -36,7 +36,7 @@ public function __construct(array $definition, $name = NULL, TypedDataInterface ...@@ -36,7 +36,7 @@ public function __construct(array $definition, $name = NULL, TypedDataInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getInstance() { public function getFieldDefinition() {
if (!isset($this->instance) && $parent = $this->getParent()) { if (!isset($this->instance) && $parent = $this->getParent()) {
$instances = FieldAPI::fieldInfo()->getBundleInstances($parent->entityType(), $parent->bundle()); $instances = FieldAPI::fieldInfo()->getBundleInstances($parent->entityType(), $parent->bundle());
$this->instance = $instances[$this->getName()]; $this->instance = $instances[$this->getName()];
...@@ -44,13 +44,6 @@ public function getInstance() { ...@@ -44,13 +44,6 @@ public function getInstance() {
return $this->instance; return $this->instance;
} }
/**
* {@inheritdoc}
*/
public function getFieldDefinition() {
return $this->getInstance();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -76,14 +69,14 @@ public function getConstraints() { ...@@ -76,14 +69,14 @@ public function getConstraints() {
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDefaultValue() { protected function getDefaultValue() {
return $this->getInstance()->getFieldDefaultValue($this->getParent()); return $this->getFieldDefinition()->getFieldDefaultValue($this->getParent());
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function defaultValuesForm(array &$form, array &$form_state) { public function defaultValuesForm(array &$form, array &$form_state) {
if (empty($this->getInstance()->default_value_function)) { if (empty($this->getFieldDefinition()->default_value_function)) {
$entity = $this->getParent(); $entity = $this->getParent();
$widget = $this->defaultValueWidget($form_state); $widget = $this->defaultValueWidget($form_state);
......
...@@ -14,13 +14,6 @@ ...@@ -14,13 +14,6 @@
*/ */
interface ConfigFieldInterface extends FieldInterface { interface ConfigFieldInterface extends FieldInterface {
/**
* Returns the field instance definition.
*
* @var \Drupal\field\Entity\FieldInstance
*/
public function getInstance();
/** /**
* Returns a form for the default value input. * Returns a form for the default value input.
* *
......
...@@ -14,23 +14,6 @@ ...@@ -14,23 +14,6 @@
*/ */
abstract class ConfigFieldItemBase extends FieldItemBase implements ConfigFieldItemInterface { abstract class ConfigFieldItemBase extends FieldItemBase implements ConfigFieldItemInterface {
/**
* The Field instance definition.
*
* @var \Drupal\field\Entity\FieldInstance
*/
public $instance;
/**
* {@inheritdoc}
*/
public function getInstance() {
if (!isset($this->instance) && $parent = $this->getParent()) {
$this->instance = $parent->getInstance();
}
return $this->instance;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -15,13 +15,6 @@ ...@@ -15,13 +15,6 @@
*/ */
interface ConfigFieldItemInterface extends FieldItemInterface { interface ConfigFieldItemInterface extends FieldItemInterface {
/**
* Returns the field instance definition.
*
* @var \Drupal\field\FieldInstanceInterface
*/
public function getInstance();
/** /**
* Returns the schema for the field. * Returns the schema for the field.
* *
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\FieldType\ConfigField; use Drupal\field\Plugin\Type\FieldType\ConfigField;
use Drupal\field\FieldInstanceInterface;
use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolation;
/** /**
...@@ -41,14 +42,16 @@ public function validate() { ...@@ -41,14 +42,16 @@ public function validate() {
$entity = $this->getParent(); $entity = $this->getParent();
$langcode = $entity->language()->id; $langcode = $entity->language()->id;
$field_name = $this->getFieldDefinition()->getFieldName();
if (isset($legacy_errors[$this->getInstance()->getField()->name][$langcode])) { if (isset($legacy_errors[$field_name][$langcode])) {
foreach ($legacy_errors[$this->getInstance()->getField()->name][$langcode] as $delta => $item_errors) { foreach ($legacy_errors[$field_name][$langcode] as $delta => $item_errors) {
foreach ($item_errors as $item_error) { foreach ($item_errors as $item_error) {
// We do not have the information about which column triggered the // We do not have the information about which column triggered the
// error, so assume the first column... // error, so assume the first column...
$column = key($this->getInstance()->getField()->getColumns()); $property_names = $this->getFieldDefinition()->getFieldPropertyNames();
$violations->add(new ConstraintViolation($item_error['message'], $item_error['message'], array(), $this, $delta . '.' . $column, $this->offsetGet($delta)->get($column)->getValue(), NULL, $item_error['error'])); $property_name = $property_names[0];
$violations->add(new ConstraintViolation($item_error['message'], $item_error['message'], array(), $this, $delta . '.' . $property_name, $this->offsetGet($delta)->get($property_name)->getValue(), NULL, $item_error['error']));
} }
} }
} }
...@@ -115,8 +118,8 @@ protected function legacyCallback($hook, $args = array()) { ...@@ -115,8 +118,8 @@ protected function legacyCallback($hook, $args = array()) {
$items = (array) $this->getValue(TRUE); $items = (array) $this->getValue(TRUE);
$args = array_merge(array( $args = array_merge(array(
$entity, $entity,
$this->getInstance()->getField(), $this->getFieldInstance()->getField(),
$this->getInstance(), $this->getFieldInstance(),
$langcode, $langcode,
&$items &$items
), $args); ), $args);
...@@ -125,4 +128,18 @@ protected function legacyCallback($hook, $args = array()) { ...@@ -125,4 +128,18 @@ protected function legacyCallback($hook, $args = array()) {
} }
} }
/**
* Returns the field instance.
*
* @return \Drupal\field\FieldInstanceInterface
* The field instance.
*/
protected function getFieldInstance() {
$instance = $this->getFieldDefinition();
if (!($instance instanceof FieldInstanceInterface)) {
throw new \UnexpectedValueException('LegacyConfigField::getFieldInstance() called for a field whose definition is not a field instance.');
}
return $instance;
}
} }
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
use Drupal\Core\Entity\Field\PrepareCacheInterface; use Drupal\Core\Entity\Field\PrepareCacheInterface;
use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase; use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase;
use Drupal\field\FieldInterface; use Drupal\field\FieldInterface;
use Drupal\field\FieldInstanceInterface;
/** /**
* Plugin implementation for legacy field types. * Plugin implementation for legacy field types.
...@@ -49,7 +50,7 @@ public function isEmpty() { ...@@ -49,7 +50,7 @@ public function isEmpty() {
$item = $this->getValue(TRUE); $item = $this->getValue(TRUE);
// The previous hook was never called on an empty item, but EntityNG always // The previous hook was never called on an empty item, but EntityNG always
// creates a FieldItem element for an empty field. // creates a FieldItem element for an empty field.
return empty($item) || $callback($item, $this->getInstance()->getField()->type); return empty($item) || $callback($item, $this->getFieldDefinition()->getFieldType());
} }
/** /**
...@@ -59,7 +60,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) { ...@@ -59,7 +60,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) {
if ($callback = $this->getLegacyCallback('settings_form')) { if ($callback = $this->getLegacyCallback('settings_form')) {
// hook_field_settings_form() used to receive the $instance (not actually // hook_field_settings_form() used to receive the $instance (not actually
// needed), and the value of field_has_data(). // needed), and the value of field_has_data().
return $callback($this->getInstance()->getField(), $this->getInstance(), $has_data); return $callback($this->getFieldInstance()->getField(), $this->getFieldInstance(), $has_data);
} }
return array(); return array();
} }
...@@ -69,7 +70,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) { ...@@ -69,7 +70,7 @@ public function settingsForm(array $form, array &$form_state, $has_data) {
*/ */
public function instanceSettingsForm(array $form, array &$form_state) { public function instanceSettingsForm(array $form, array &$form_state) {
if ($callback = $this->getLegacyCallback('instance_settings_form')) { if ($callback = $this->getLegacyCallback('instance_settings_form')) {
return $callback($this->getInstance()->getField(), $this->getInstance(), $form_state); return $callback($this->getFieldInstance()->getField(), $this->getFieldInstance(), $form_state);
} }
return array(); return array();
} }
...@@ -96,8 +97,8 @@ public function prepareCache() { ...@@ -96,8 +97,8 @@ public function prepareCache() {
$args = array( $args = array(
$entity->entityType(), $entity->entityType(),
array($entity_id => $entity), array($entity_id => $entity),
$this->getInstance()->getField(), $this->getFieldInstance()->getField(),
array($entity_id => $this->getInstance()), array($entity_id => $this->getFieldInstance()),
$langcode, $langcode,
&$items, &$items,
FIELD_LOAD_CURRENT, FIELD_LOAD_CURRENT,
...@@ -119,7 +120,7 @@ public function getSettableOptions() { ...@@ -119,7 +120,7 @@ public function getSettableOptions() {
$callback = "{$definition['provider']}_options_list"; $callback = "{$definition['provider']}_options_list";
if (function_exists($callback)) { if (function_exists($callback)) {
$entity = $this->getParent()->getParent(); $entity = $this->getParent()->getParent();
return $callback($this->getInstance(), $entity); return $callback($this->getFieldDefinition(), $entity);
} }
} }
...@@ -141,4 +142,18 @@ protected function getLegacyCallback($hook) { ...@@ -141,4 +142,18 @@ protected function getLegacyCallback($hook) {
} }
} }
/**
* Returns the field instance.
*
* @return \Drupal\field\Entity\FieldInstanceInterface
* The field instance.
*/
protected function getFieldInstance() {
$instance = $this->getFieldDefinition();
if (!($instance instanceof FieldInstanceInterface)) {
throw new \UnexpectedValueException('LegacyConfigFieldItem::getFieldInstance() called for a field whose definition is not a field instance.');
}
return $instance;
}
} }
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