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 8222113..4d88341 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 @@ -150,7 +150,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { /** * {@inheritdoc} */ - public function defaultValuesForm(array $element, array &$form, array &$form_state) { + public function defaultValuesForm(array &$form, array &$form_state) { return array(); } 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 92362a0..98e40c9 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 @@ -49,11 +49,11 @@ public function instanceSettingsForm(array $form, array &$form_state) { /** * {@inheritdoc} */ - public function defaultValuesForm(array $element, array &$form, array &$form_state) { + public function defaultValuesForm(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(); + $default_widget = $this->defaultValueWidget(); $this->getFieldDefinition()->required = 0; @@ -64,18 +64,18 @@ public function defaultValuesForm(array $element, array &$form, array &$form_sta $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); + $element = array( + '#type' => 'details', + '#title' => t('Default value'), + '#tree' => TRUE, + '#description' => t('The default value for this field, used when creating new content.'), + // Stick to an empty 'parents' on this form in order not to breaks widgets + // that do not use field_widget_[field|instance]() and still access + // $form_state['field'] directly. + '#parents' => array(), + ); + + return $element += $default_widget->form($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); } /** @@ -83,14 +83,14 @@ public function defaultValuesForm(array $element, array &$form, array &$form_sta */ public function defaultValuesFormValidate(array &$form, array &$form_state) { $entity = $form['#entity']; - $renderer = $form['#renderer']; + $default_widget = $this->defaultValueWidget(); $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); + $default_widget->extractFormValues($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); $violations = $items->validate(); // Report errors. @@ -101,7 +101,7 @@ public function defaultValuesFormValidate(array &$form, array &$form_state) { 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); + $default_widget->flagErrors($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); } } /** @@ -109,16 +109,37 @@ public function defaultValuesFormValidate(array &$form, array &$form_state) { */ public function defaultValuesFormSubmit(array &$form, array &$form_state) { $entity = $form['#entity']; - $renderer = $form['#renderer']; + $default_widget = $this->defaultValueWidget(); $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); + $default_widget->extractFormValues($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state); return $items->getValue() ?: NULL; } + protected function defaultValueWidget() { + static $widget = NULL; + + if (!isset($widget)) { + $field_name = $this->getFieldDefinition()->getFieldName(); + $entity_form_display = entity_get_form_display($this->getFieldDefinition()->entity_type, $this->getFieldDefinition()->bundle, 'default'); + + // When field is hidden, use the default widget and store it in the form + if (!$widget = $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'] + ); + $widget = \Drupal::service('plugin.manager.field.widget')->createInstance($field_type['default_widget'], $configuration); + } + } + + return $widget; + } } 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 23bc5e5..a82e6df 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 @@ -105,7 +105,7 @@ public function instanceSettingsForm(array $form, array &$form_state); * @return array * The form definition for the field instance default value. */ - public function defaultValuesForm(array $element, array &$form, array &$form_state); + public function defaultValuesForm(array &$form, array &$form_state); /** * Validates the default value widget for a given field instance. 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 1ada1ab..2cb783c 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 @@ -73,7 +73,6 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac $bundle = $this->instance['bundle']; $entity_type = $this->instance['entity_type']; $field = $this->instance->getField(); - $entity_form_display = entity_get_form_display($entity_type, $bundle, 'default'); $bundles = entity_get_bundles(); drupal_set_title(t('%instance settings for %bundle', array( @@ -82,11 +81,9 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac )), PASS_THROUGH); $form['#field'] = $field; - $form['#entity_form_display'] = $entity_form_display; // Create an arbitrary entity object (used by the 'default value' widget). $ids = (object) array('entity_type' => $this->instance['entity_type'], 'bundle' => $this->instance['bundle'], 'entity_id' => NULL); $form['#entity'] = _field_create_entity_from_ids($ids); - $form['#entity']->field_ui_default_value = TRUE; if (!empty($field['locked'])) { $form['locked'] = array( @@ -145,7 +142,7 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac // Add handling for default value if not provided by any other module. if (field_behaviors_widget('default_value', $this->instance) == FIELD_BEHAVIOR_DEFAULT && empty($this->instance['default_value_function'])) { - $form['instance']['default_value_widget'] = $this->getDefaultValueWidget($field, $form, $form_state); + $form['instance']['default_value_widget'] = $this->getDefaultValueWidget($form, $form_state); } $form['actions'] = array('#type' => 'actions'); @@ -174,10 +171,6 @@ public function validateForm(array &$form, array &$form_state) { * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { - $field_name = $this->instance['field_name']; - $entity = $form['#entity']; - $entity_form_display = $form['#entity_form_display']; - // Handle the default value. if (isset($form['instance']['default_value_widget'])) { $this->instance['default_value'] = $this->getFieldItem($form['#entity'], $this->instance['field_name'])->defaultValuesFormSubmit($form, $form_state); @@ -209,22 +202,8 @@ public function delete(array &$form, array &$form_state) { /** * Builds the default value widget for a given field instance. */ - protected function getDefaultValueWidget($field, array &$form, &$form_state) { - $entity = $form['#entity']; - $entity_form_display = $form['#entity_form_display']; - - $element = array( - '#type' => 'details', - '#title' => t('Default value'), - '#tree' => TRUE, - '#description' => t('The default value for this field, used when creating new content.'), - // Stick to an empty 'parents' on this form in order not to breaks widgets - // that do not use field_widget_[field|instance]() and still access - // $form_state['field'] directly. - '#parents' => array(), - ); - - $element += $this->getFieldItem($form['#entity'], $this->instance['field_name'])->defaultValuesForm($element, $form, $form_state); + protected function getDefaultValueWidget(array &$form, &$form_state) { + $element = $this->getFieldItem($form['#entity'], $this->instance['field_name'])->defaultValuesForm($form, $form_state); return $element; }