Commit ed4b791c authored by alexpott's avatar alexpott

Issue #2192259 by andypost, amateescu, damiankloip: Remove ConfigField.. Item...

Issue #2192259 by andypost, amateescu, damiankloip: Remove ConfigField.. Item and List classes + interfaces.
parent 6ba9a500
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
use Drupal\Core\Field\PrepareCacheInterface; use Drupal\Core\Field\PrepareCacheInterface;
use Drupal\field\FieldConfigInterface; use Drupal\field\FieldConfigInterface;
use Drupal\field\FieldInstanceConfigInterface; use Drupal\field\FieldInstanceConfigInterface;
use Drupal\Core\Field\ConfigFieldItemListInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class FieldableEntityStorageControllerBase extends EntityStorageControllerBase implements FieldableEntityStorageControllerInterface { abstract class FieldableEntityStorageControllerBase extends EntityStorageControllerBase implements FieldableEntityStorageControllerInterface {
...@@ -167,7 +166,7 @@ protected function loadFieldItems(array $entities) { ...@@ -167,7 +166,7 @@ protected function loadFieldItems(array $entities) {
foreach ($entity->getTranslationLanguages() as $langcode => $language) { foreach ($entity->getTranslationLanguages() as $langcode => $language) {
$translation = $entity->getTranslation($langcode); $translation = $entity->getTranslation($langcode);
foreach ($translation as $field_name => $items) { foreach ($translation as $field_name => $items) {
if ($items instanceof ConfigFieldItemListInterface && !$items->isEmpty()) { if ($items->getFieldDefinition()->isConfigurable() && !$items->isEmpty()) {
foreach ($items as $delta => $item) { foreach ($items as $delta => $item) {
// If the field item needs to prepare the cache data, call the // If the field item needs to prepare the cache data, call the
// corresponding method, otherwise use the values as cache // corresponding method, otherwise use the values as cache
......
<?php
/**
* @file
* Contains \Drupal\Core\Field\ConfigFieldItemBase.
*/
namespace Drupal\Core\Field;
/**
* Base class for 'configurable field type' plugin implementations.
*/
abstract class ConfigFieldItemBase extends FieldItemBase implements ConfigFieldItemInterface {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state, $has_data) {
return array();
}
/**
* {@inheritdoc}
*/
public function instanceSettingsForm(array $form, array &$form_state) {
return array();
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Field\ConfigFieldItemInterface.
*/
namespace Drupal\Core\Field;
/**
* Interface definition for 'configurable field type' plugins.
*/
interface ConfigFieldItemInterface extends FieldItemInterface {
/**
* Returns a form for the field-level settings.
*
* Invoked from \Drupal\field_ui\Form\FieldEditForm to allow administrators to
* configure field-level settings.
*
* Field storage might reject field definition changes that affect the field
* storage schema if the field already has data. When the $has_data parameter
* is TRUE, the form should not allow changing the settings that take part in
* the schema() method. It is recommended to set #access to FALSE on the
* corresponding elements.
*
* @param array $form
* The form where the settings form is being included in.
* @param array $form_state
* The form state of the (entire) configuration form.
* @param bool $has_data
* TRUE if the field already has data, FALSE if not.
*
* @return
* The form definition for the field settings.
*/
public function settingsForm(array $form, array &$form_state, $has_data);
/**
* Returns a form for the instance-level settings.
*
* Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow
* administrators to configure instance-level settings.
*
* @param array $form
* The form where the settings form is being included in.
* @param array $form_state
* The form state of the (entire) configuration form.
*
* @return array
* The form definition for the field instance settings.
*/
public function instanceSettingsForm(array $form, array &$form_state);
}
<?php
/**
* @file
* Contains \Drupal\Core\Field\ConfigFieldItemList.
*/
namespace Drupal\Core\Field;
/**
* Represents a configurable entity field item list.
*/
class ConfigFieldItemList extends FieldItemList implements ConfigFieldItemListInterface {
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraints = parent::getConstraints();
// Check that the number of values doesn't exceed the field cardinality. For
// form submitted values, this can only happen with 'multiple value'
// widgets.
$cardinality = $this->getFieldDefinition()->getCardinality();
if ($cardinality != FieldDefinitionInterface::CARDINALITY_UNLIMITED) {
$constraints[] = \Drupal::typedDataManager()
->getValidationConstraintManager()
->create('Count', array(
'max' => $cardinality,
'maxMessage' => t('%name: this field cannot hold more than @count values.', array('%name' => $this->getFieldDefinition()->getLabel(), '@count' => $cardinality)),
));
}
return $constraints;
}
/**
* {@inheritdoc}
*/
public function defaultValuesForm(array &$form, array &$form_state) {
if (empty($this->getFieldDefinition()->default_value_function)) {
// Place the input in a separate place in the submitted values tree.
$widget = $this->defaultValueWidget($form_state);
$element = array('#parents' => array('default_value_input'));
$element += $widget->form($this, $element, $form_state);
return $element;
}
}
/**
* {@inheritdoc}
*/
public function defaultValuesFormValidate(array $element, array &$form, array &$form_state) {
// Extract the submitted value, and validate it.
$widget = $this->defaultValueWidget($form_state);
$widget->extractFormValues($this, $element, $form_state);
$violations = $this->validate();
if (count($violations)) {
// Store reported errors in $form_state.
$field_name = $this->getFieldDefinition()->getName();
$field_state = field_form_get_state($element['#parents'], $field_name, $form_state);
$field_state['constraint_violations'] = $violations;
field_form_set_state($element['#parents'], $field_name, $form_state, $field_state);
// Assign reported errors to the correct form element.
$widget->flagErrors($this, $element, $form_state);
}
}
/**
* {@inheritdoc}
*/
public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) {
// Extract the submitted value, and return it as an array.
$widget = $this->defaultValueWidget($form_state);
$widget->extractFormValues($this, $element, $form_state);
return $this->getValue();
}
/**
* Returns the widget object used in default value form.
*
* @param array $form_state
* The form state of the (entire) configuration form.
*
* @return \Drupal\Core\Field\WidgetInterface
* A Widget object.
*/
protected function defaultValueWidget(array &$form_state) {
if (!isset($form_state['default_value_widget'])) {
$entity = $this->getEntity();
// Force a non-required widget.
$this->getFieldDefinition()->required = FALSE;
$this->getFieldDefinition()->description = '';
// Use the widget currently configured for the 'default' form mode, or
// fallback to the default widget for the field type.
$entity_form_display = entity_get_form_display($entity->getEntityTypeId(), $entity->bundle(), 'default');
$widget = $entity_form_display->getRenderer($this->getFieldDefinition()->getName());
if (!$widget) {
$widget = \Drupal::service('plugin.manager.field.widget')->getInstance(array('field_definition' => $this->getFieldDefinition()));
}
$form_state['default_value_widget'] = $widget;
}
return $form_state['default_value_widget'];
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Field\ConfigFieldItemListInterface.
*/
namespace Drupal\Core\Field;
/**
* Interface definition for "configurable fields".
*/
interface ConfigFieldItemListInterface extends FieldItemListInterface {
/**
* Returns a form for the default value input.
*
* Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow
* administrators to configure instance-level default value.
*
* @param array $form
* The form where the settings form is being included in.
* @param array $form_state
* The form state of the (entire) configuration form.
*
* @return array
* The form definition for the field instance default value.
*/
public function defaultValuesForm(array &$form, array &$form_state);
/**
* Validates the submitted default value.
*
* Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow
* administrators to configure instance-level default value.
*
* @param array $element
* The default value form element.
* @param array $form
* The form where the settings form is being included in.
* @param array $form_state
* The form state of the (entire) configuration form.
*/
public function defaultValuesFormValidate(array $element, array &$form, array &$form_state);
/**
* Processes the submitted default value.
*
* Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow
* administrators to configure instance-level default value.
*
* @param array $element
* The default value form element.
* @param array $form
* The form where the settings form is being included in.
* @param array $form_state
* The form state of the (entire) configuration form.
*
* @return array
* The field instance default value.
*/
public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state);
}
...@@ -177,6 +177,22 @@ public function getCardinality() { ...@@ -177,6 +177,22 @@ public function getCardinality() {
return isset($this->definition['cardinality']) ? $this->definition['cardinality'] : 1; return isset($this->definition['cardinality']) ? $this->definition['cardinality'] : 1;
} }
/**
* Sets the maximum number of items allowed for the field.
*
* Possible values are positive integers or
* FieldDefinitionInterface::CARDINALITY_UNLIMITED.
*
* @param int $cardinality
* The field cardinality.
*
* @return $this
*/
public function setCardinality($cardinality) {
$this->definition['cardinality'] = $cardinality;
return $this;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -226,4 +226,18 @@ public function delete() { } ...@@ -226,4 +226,18 @@ public function delete() { }
*/ */
public function deleteRevision() { } public function deleteRevision() { }
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state, $has_data) {
return array();
}
/**
* {@inheritdoc}
*/
public function instanceSettingsForm(array $form, array &$form_state) {
return array();
}
} }
...@@ -212,4 +212,44 @@ public function delete(); ...@@ -212,4 +212,44 @@ public function delete();
*/ */
public function deleteRevision(); public function deleteRevision();
/**
* Returns a form for the field-level settings.
*
* Invoked from \Drupal\field_ui\Form\FieldEditForm to allow administrators to
* configure field-level settings.
*
* Field storage might reject field definition changes that affect the field
* storage schema if the field already has data. When the $has_data parameter
* is TRUE, the form should not allow changing the settings that take part in
* the schema() method. It is recommended to set #access to FALSE on the
* corresponding elements.
*
* @param array $form
* The form where the settings form is being included in.
* @param array $form_state
* The form state of the (entire) configuration form.
* @param bool $has_data
* TRUE if the field already has data, FALSE if not.
*
* @return
* The form definition for the field settings.
*/
public function settingsForm(array $form, array &$form_state, $has_data);
/**
* Returns a form for the instance-level settings.
*
* Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow
* administrators to configure instance-level settings.
*
* @param array $form
* The form where the settings form is being included in.
* @param array $form_state
* The form state of the (entire) configuration form.
*
* @return array
* The form definition for the field instance settings.
*/
public function instanceSettingsForm(array $form, array &$form_state);
} }
...@@ -286,4 +286,102 @@ public function view($display_options = array()) { ...@@ -286,4 +286,102 @@ public function view($display_options = array()) {
return $view_builder->viewField($this, $display_options); return $view_builder->viewField($this, $display_options);
} }
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraints = parent::getConstraints();
// Check that the number of values doesn't exceed the field cardinality. For
// form submitted values, this can only happen with 'multiple value'
// widgets.
$cardinality = $this->getFieldDefinition()->getCardinality();
if ($cardinality != FieldDefinitionInterface::CARDINALITY_UNLIMITED) {
$constraints[] = \Drupal::typedDataManager()
->getValidationConstraintManager()
->create('Count', array(
'max' => $cardinality,
'maxMessage' => t('%name: this field cannot hold more than @count values.', array('%name' => $this->getFieldDefinition()->getLabel(), '@count' => $cardinality)),
));
}
return $constraints;
}
/**
* {@inheritdoc}
*/
public function defaultValuesForm(array &$form, array &$form_state) {
if (empty($this->getFieldDefinition()->default_value_function)) {
// Place the input in a separate place in the submitted values tree.
$widget = $this->defaultValueWidget($form_state);
$element = array('#parents' => array('default_value_input'));
$element += $widget->form($this, $element, $form_state);
return $element;
}
}
/**
* {@inheritdoc}
*/
public function defaultValuesFormValidate(array $element, array &$form, array &$form_state) {
// Extract the submitted value, and validate it.
$widget = $this->defaultValueWidget($form_state);
$widget->extractFormValues($this, $element, $form_state);
$violations = $this->validate();
if (count($violations)) {
// Store reported errors in $form_state.
$field_name = $this->getFieldDefinition()->getName();
$field_state = field_form_get_state($element['#parents'], $field_name, $form_state);
$field_state['constraint_violations'] = $violations;
field_form_set_state($element['#parents'], $field_name, $form_state, $field_state);
// Assign reported errors to the correct form element.
$widget->flagErrors($this, $element, $form_state);
}
}
/**
* {@inheritdoc}
*/
public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) {
// Extract the submitted value, and return it as an array.
$widget = $this->defaultValueWidget($form_state);
$widget->extractFormValues($this, $element, $form_state);
return $this->getValue();
}
/**
* Returns the widget object used in default value form.
*
* @param array $form_state
* The form state of the (entire) configuration form.
*
* @return \Drupal\Core\Field\WidgetInterface
* A Widget object.
*/
protected function defaultValueWidget(array &$form_state) {
if (!isset($form_state['default_value_widget'])) {
$entity = $this->getEntity();
// Force a non-required widget.
$this->getFieldDefinition()->required = FALSE;
$this->getFieldDefinition()->description = '';
// Use the widget currently configured for the 'default' form mode, or
// fallback to the default widget for the field type.
$entity_form_display = entity_get_form_display($entity->getEntityTypeId(), $entity->bundle(), 'default');
$widget = $entity_form_display->getRenderer($this->getFieldDefinition()->getName());
if (!$widget) {
$widget = \Drupal::service('plugin.manager.field.widget')->getInstance(array('field_definition' => $this->getFieldDefinition()));
}
$form_state['default_value_widget'] = $widget;
}
return $form_state['default_value_widget'];
}
} }
...@@ -179,4 +179,53 @@ public function deleteRevision(); ...@@ -179,4 +179,53 @@ public function deleteRevision();
*/ */
public function view($display_options = array()); public function view($display_options = array());
/**
* Returns a form for the default value input.
*
* Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow
* administrators to configure instance-level default value.
*
* @param array $form
* The form where the settings form is being included in.
* @param array $form_state
* The form state of the (entire) configuration form.
*
* @return array
* The form definition for the field instance default value.
*/
public function defaultValuesForm(array &$form, array &$form_state);
/**
* Validates the submitted default value.
*
* Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow
* administrators to configure instance-level default value.
*
* @param array $element
* The default value form element.
* @param array $form
* The form where the settings form is being included in.
* @param array $form_state
* The form state of the (entire) configuration form.
*/
public function defaultValuesFormValidate(array $element, array &$form, array &$form_state);
/**
* Processes the submitted default value.
*
* Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow
* administrators to configure instance-level default value.
*
* @param array $element
* The default value form element.
* @param array $form
* The form where the settings form is being included in.
* @param array $form_state
* The form state of the (entire) configuration form.
*
* @return array
* The field instance default value.
*/
public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state);
} }
...@@ -50,14 +50,9 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac ...@@ -50,14 +50,9 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
public function processDefinition(&$definition, $plugin_id) { public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id); parent::processDefinition($definition, $plugin_id);
if (!isset($definition['list_class'])) { if (!isset($definition['list_class'])) {
if ($definition['configurable']) {
$definition['list_class'] = '\Drupal\Core\Field\ConfigFieldItemList';
}
else {
$definition['list_class'] = '\Drupal\Core\Field\FieldItemList'; $definition['list_class'] = '\Drupal\Core\Field\FieldItemList';
} }
} }
}
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\ConfigFieldItemBase; use Drupal\Core\Field\FieldItemBase;
/** /**
* Plugin implementation of the 'comment' field type. * Plugin implementation of the 'comment' field type.
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
* default_formatter = "comment_default" * default_formatter = "comment_default"
* ) * )
*/ */
class CommentItem extends ConfigFieldItemBase implements CommentItemInterface { class CommentItem extends FieldItemBase implements CommentItemInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -7,12 +7,10 @@ ...@@ -7,12 +7,10 @@
namespace Drupal\comment\Plugin\Field\FieldType; namespace Drupal\comment\Plugin\Field\FieldType;
use Drupal\Core\Field\ConfigFieldItemInterface;
/** /**
* Interface definition for Comment items. * Interface definition for Comment items.
*/ */
interface CommentItemInterface extends ConfigFieldItemInterface { interface CommentItemInterface {
/** /**
* Comments for this entity are hidden. * Comments for this entity are hidden.
......
...@@ -8,12 +8,12 @@ ...@@ -8,12 +8,12 @@
namespace Drupal\datetime\Plugin\Field\FieldType; namespace Drupal\datetime\Plugin\Field\FieldType;
use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\ConfigFieldItemList; use Drupal\Core\Field\FieldItemList;
/** /**
* Represents a configurable entity datetime field. * Represents a configurable entity datetime field.
*/ */
class DateTimeFieldItemList extends ConfigFieldItemList { class DateTimeFieldItemList extends FieldItemList {
/** /**
* Defines the default value as now. * Defines the default value as now.
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\PrepareCacheInterface; use Drupal\Core\Field\PrepareCacheInterface;
use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\ConfigFieldItemBase; use Drupal\Core\Field\FieldItemBase;
/**