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 c451fdb..8222113 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 @@ -148,6 +148,27 @@ public function instanceSettingsForm(array $form, array &$form_state) { } /** + * {@inheritdoc} + */ + public function defaultValuesForm(array $element, array &$form, array &$form_state) { + return array(); + } + + /** + * {@inheritdoc} + */ + public function defaultValuesFormValidate(array &$form, array &$form_state) { + return array(); + } + + /** + * {@inheritdoc} + */ + public function defaultValuesFormSubmit(array &$form, array &$form_state) { + return array(); + } + + /** * Returns the legacy callback for a given field type "hook". * * Copied from \Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem, diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php index 743ad5c..92362a0 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemBase.php @@ -8,6 +8,7 @@ namespace Drupal\field\Plugin\Type\FieldType; use Drupal\Core\Entity\Field\FieldItemBase; +use Drupal\Core\Language\Language; /** * Base class for 'configurable field type' plugin implementations. @@ -45,4 +46,79 @@ public function instanceSettingsForm(array $form, array &$form_state) { return array(); } + /** + * {@inheritdoc} + */ + public function defaultValuesForm(array $element, array &$form, array &$form_state) { + $entity = $form['#entity']; + $entity_form_display = $form['#entity_form_display']; + $default_value = $this->getFieldDefinition()->default_value; + $field_name = $this->getFieldDefinition()->getFieldName(); + + $this->getFieldDefinition()->required = 0; + + // Insert the widget. Since we do not use the "official" instance definition, + // the whole flow cannot use field_invoke_method(). + $items = $entity->getNGEntity()->{$field_name}; + if (!empty($default_value)) { + $items->setValue($default_value); + } + + // When field is hidden, use the default widget and store it in the form + if (!$renderer = $entity_form_display->getRenderer($field_name)) { + $field_type = \Drupal::service('plugin.manager.entity.field.field_type')->getDefinition($this->getFieldDefinition()->getFieldType()); + $default_widget = \Drupal::service('plugin.manager.field.widget')->getDefinition($field_type['default_widget']); + $configuration = array( + 'field_definition' => $this->getFieldDefinition(), + 'settings' => $default_widget['settings'] + ); + $renderer = \Drupal::service('plugin.manager.field.widget')->createInstance($field_type['default_widget'], $configuration); + } + $form['#renderer'] = $renderer; + return $renderer->form($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); + } + + /** + * {@inheritdoc} + */ + public function defaultValuesFormValidate(array &$form, array &$form_state) { + $entity = $form['#entity']; + $renderer = $form['#renderer']; + $field_name = $this->getFieldDefinition()->getFieldName(); + + $element = $form['instance']['default_value_widget']; + + // Extract the 'default value'. + $items = $entity->getNGEntity()->{$field_name}; + $renderer->extractFormValues($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); + $violations = $items->validate(); + + // Report errors. + if (count($violations)) { + $field_state = field_form_get_state($element['#parents'], $field_name, Language::LANGCODE_NOT_SPECIFIED, $form_state); + // Store reported errors in $form_state. + $field_state['constraint_violations'] = $violations; + field_form_set_state($element['#parents'], $field_name, Language::LANGCODE_NOT_SPECIFIED, $form_state, $field_state); + + // Assign reported errors to the correct form element. + $renderer->flagErrors($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); + } + } + /** + * {@inheritdoc} + */ + public function defaultValuesFormSubmit(array &$form, array &$form_state) { + $entity = $form['#entity']; + $renderer = $form['#renderer']; + $field_name = $this->getFieldDefinition()->getFieldName(); + + $element = $form['instance']['default_value_widget']; + + // Extract field values. + $items = $entity->getNGEntity()->{$field_name}; + $renderer->extractFormValues($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); + + return $items->getValue() ?: NULL; + } + } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php index 394ec01..23bc5e5 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemInterface.php @@ -89,4 +89,54 @@ public function settingsForm(array $form, array &$form_state); */ public function instanceSettingsForm(array $form, array &$form_state); + /** + * Builds the default value widget for a given field instance. + * + * Invoked from \Drupal\field_ui\Form\FieldInstanceEditForm to allow + * administrators to configure instance-level default value. + * + * @param array $element + * The default value form base 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 form definition for the field instance default value. + */ + public function defaultValuesForm(array $element, array &$form, array &$form_state); + + /** + * Validates the default value widget for a given field instance. + * + * 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 field instance default value. + */ + public function defaultValuesFormValidate(array &$form, array &$form_state); + + /** + * Submits the default value widget for a given field instance. + * + * 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 field instance default value. + */ + public function defaultValuesFormSubmit(array &$form, array &$form_state); + } diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php b/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php index 046e012..1ada1ab 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php @@ -32,13 +32,6 @@ class FieldInstanceEditForm implements FormInterface, ControllerInterface { protected $instance; /** - * The field widget plugin manager. - * - * @var \Drupal\field\Plugin\Type\Widget\WidgetPluginManager - */ - protected $widgetManager; - - /** * The entity manager. * * @var \Drupal\Core\Entity\EntityManager @@ -46,26 +39,13 @@ class FieldInstanceEditForm implements FormInterface, ControllerInterface { protected $entityManager; /** - * The field type manager. - * - * @var \Drupal\Core\Entity\Field\FieldTypePluginManager - */ - protected $fieldTypeManager; - - /** * Constructs a new field instance form. * * @param \Drupal\Core\Entity\EntityManager $entity_manager * The entity manager. - * @param \Drupal\field\Plugin\Type\Widget\WidgetPluginManager $widget_manager - * The field widget plugin manager. - * @param \Drupal\Core\Entity\Field\FieldTypePluginManager $field_type_manager - * The field type manager. */ - public function __construct(EntityManager $entity_manager, WidgetPluginManager $widget_manager, FieldTypePluginManager $field_type_manager) { + public function __construct(EntityManager $entity_manager) { $this->entityManager = $entity_manager; - $this->widgetManager = $widget_manager; - $this->fieldTypeManager = $field_type_manager; } /** @@ -73,9 +53,7 @@ public function __construct(EntityManager $entity_manager, WidgetPluginManager $ */ public static function create(ContainerInterface $container) { return new static( - $container->get('plugin.manager.entity'), - $container->get('plugin.manager.field.widget'), - $container->get('plugin.manager.entity.field.field_type') + $container->get('plugin.manager.entity') ); } @@ -187,30 +165,8 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac * {@inheritdoc} */ public function validateForm(array &$form, array &$form_state) { - // Take the incoming values as the $this->instance definition, so that the 'default - // value' gets validated using the instance settings being submitted. - $field_name = $this->instance['field_name']; - $entity = $form['#entity']; - $entity_form_display = $form['#entity_form_display']; - if (isset($form['instance']['default_value_widget'])) { - $element = $form['instance']['default_value_widget']; - - // Extract the 'default value'. - $items = $entity->getNGEntity()->{$field_name}; - $entity_form_display->getRenderer($this->instance->getField()->id)->extractFormValues($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); - $violations = $items->validate(); - - // Report errors. - if (count($violations)) { - $field_state = field_form_get_state($element['#parents'], $field_name, Language::LANGCODE_NOT_SPECIFIED, $form_state); - // Store reported errors in $form_state. - $field_state['constraint_violations'] = $violations; - field_form_set_state($element['#parents'], $field_name, Language::LANGCODE_NOT_SPECIFIED, $form_state, $field_state); - - // Assign reported errors to the correct form element. - $entity_form_display->getRenderer($this->instance->getField()->id)->flagErrors($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); - } + $this->getFieldItem($form['#entity'], $this->instance['field_name'])->defaultValuesFormValidate($form, $form_state); } } @@ -224,13 +180,7 @@ public function submitForm(array &$form, array &$form_state) { // Handle the default value. if (isset($form['instance']['default_value_widget'])) { - $element = $form['instance']['default_value_widget']; - - // Extract field values. - $items = $entity->getNGEntity()->{$field_name}; - $entity_form_display->getRenderer($this->instance->getField()->id)->extractFormValues($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); - - $this->instance['default_value'] = $items->getValue() ?: NULL; + $this->instance['default_value'] = $this->getFieldItem($form['#entity'], $this->instance['field_name'])->defaultValuesFormSubmit($form, $form_state); } // Merge incoming values into the instance. @@ -274,34 +224,7 @@ protected function getDefaultValueWidget($field, array &$form, &$form_state) { '#parents' => array(), ); - // Adjust the instance definition used for the form element. We want a - // non-required input and no description. - $this->instance['required'] = FALSE; - $this->instance['description'] = ''; - - // Adjust the instance definition to use the default widget of this field type - // instead of the hidden widget. - // @todo Clean this up since we don't have $this->instance['widget'] anymore. - // see https://drupal.org/node/2028759 - if ($this->instance['widget']['type'] == 'hidden') { - $field_type = $this->fieldTypeManager->getDefinition($field['type']); - $default_widget = $this->widgetManager->getDefinition($field_type['default_widget']); - - $this->instance['widget'] = array( - 'type' => $default_widget['id'], - 'settings' => $default_widget['settings'], - 'weight' => 0, - ); - } - - // Insert the widget. Since we do not use the "official" instance definition, - // the whole flow cannot use field_invoke_method(). - $items = $entity->getNGEntity()->{$this->instance->getField()->id}; - if (!empty($this->instance['default_value'])) { - $items->setValue((array) $this->instance['default_value']); - } - $element += $entity_form_display->getRenderer($this->instance->getField()->id)->form($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); - + $element += $this->getFieldItem($form['#entity'], $this->instance['field_name'])->defaultValuesForm($element, $form, $form_state); return $element; }