diff --git a/core/modules/field/lib/Drupal/field/FieldInstance.php b/core/modules/field/lib/Drupal/field/FieldInstance.php index bf0656e..75040c0 100644 --- a/core/modules/field/lib/Drupal/field/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/FieldInstance.php @@ -39,31 +39,15 @@ public function __construct(array $definition) { /** * Returns the Widget plugin for the instance. * - * @param bool $allow_hidden - * (optional) Whether we are allowed to return the 'field_hidden' widget. - * Defaults to TRUE. + * @param bool $skip_cache + * (optional) Whether to skiup the static cache. Defaults to FALSE. * * @return \Drupal\field\Plugin\Type\Widget\WidgetInterface * The Widget plugin to be used for the instance. */ - public function getWidget($allow_hidden = TRUE) { - // Skip the static cache if we are getting the widget for the field instance - // settings form. - if (empty($this->widget) || !$allow_hidden) { - if (!$allow_hidden && $this->definition['widget']['type'] == 'field_hidden') { - $field = field_info_field_by_id($this->definition['field_id']); - $field_type = field_info_field_types($field['type']); - $default_widget = field_info_widget_types($field_type['default_widget']); - - $widget_properties = array( - 'type' => $default_widget['id'], - 'settings' => $default_widget['settings'], - 'weight' => 0, - ); - } - else { - $widget_properties = $this->definition['widget']; - } + public function getWidget($skip_cache = FALSE) { + if (empty($this->widget) || $skip_cache) { + $widget_properties = $this->definition['widget']; // Let modules alter the widget properties. $context = array( diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/LegacyDiscoveryDecorator.php b/core/modules/field/lib/Drupal/field/Plugin/Type/LegacyDiscoveryDecorator.php index 6271209..1f94d7c 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/LegacyDiscoveryDecorator.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/LegacyDiscoveryDecorator.php @@ -67,9 +67,8 @@ public function getDefinitions() { unset($definition['behaviors']['default value']); } - if (!isset($definition['id'])) { - $definition['id'] = $plugin_id; - } + // Legacy widgets also need a plugin id. + $definition['id'] = $plugin_id; $definitions[$plugin_id] = $definition; } diff --git a/core/modules/field/lib/Drupal/field/Tests/FormTest.php b/core/modules/field/lib/Drupal/field/Tests/FormTest.php index 5565699..eeb4ae0 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FormTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FormTest.php @@ -591,10 +591,11 @@ function testFieldFormHiddenWidget() { field_create_instance($this->instance); $langcode = LANGUAGE_NOT_SPECIFIED; - // Display creation form. + // Display the entity creation form. $this->drupalGet('test-entity/add/test_bundle'); - // Create an entity. + // Create an entity and test that the default value is assigned correctly to + // the field that uses the hidden widget. $this->assertNoField("{$this->field_name}[$langcode][0][value]", 'The hidden widget is not displayed'); $this->drupalPost(NULL, array(), t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); diff --git a/core/modules/field_ui/field_ui.admin.inc b/core/modules/field_ui/field_ui.admin.inc index 149c38e..7c779fb 100644 --- a/core/modules/field_ui/field_ui.admin.inc +++ b/core/modules/field_ui/field_ui.admin.inc @@ -692,7 +692,7 @@ function field_ui_widget_type_form_submit($form, &$form_state) { field_update_instance($instance); drupal_set_message(t('Changed the widget for field %label.', array('%label' => $instance['label']))); - if ($instance['required'] && empty($instance['default_value']) && $instance['widget']['type'] == 'field_hidden') { + if ($instance['required'] && empty($instance['default_value']) && empty($instance['default_value_function']) && $instance['widget']['type'] == 'field_hidden') { drupal_set_message(t('Field %label is required and uses the "hidden" widget. You might want to configure a default value.', array('%label' => $instance['label'])), 'warning'); } } @@ -938,10 +938,20 @@ function field_ui_default_value_widget($field, $instance, &$form, &$form_state) $instance['required'] = FALSE; $instance['description'] = ''; + // Adjust the instance definition to use the default widget of this field type + // instead of the hidden widget. + if ($instance['widget']['type'] == 'field_hidden') { + $field_type = field_info_field_types($field['type']); + $default_widget = field_info_widget_types($field_type['default_widget']); + + $instance['widget']['type'] = $default_widget['id']; + $instance['widget']['settings'] = $default_widget['settings']; + } + // Insert the widget. Since we do not use the "official" instance definition, // the whole flow cannot use field_invoke_method(). $items = (array) $instance['default_value']; - $element += $instance->getWidget(FALSE)->form($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); + $element += $instance->getWidget(TRUE)->form($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); return $element; } @@ -963,7 +973,7 @@ function field_ui_field_edit_form_validate($form, &$form_state) { // Extract the 'default value'. $items = array(); - $instance->getWidget(FALSE)->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); + $instance->getWidget()->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); // Grab the field definition from $form_state. $field_state = field_form_get_state($element['#parents'], $field_name, LANGUAGE_NOT_SPECIFIED, $form_state); @@ -983,7 +993,7 @@ function field_ui_field_edit_form_validate($form, &$form_state) { field_form_set_state($element['#parents'], $field_name, LANGUAGE_NOT_SPECIFIED, $form_state, $field_state); // Assign reported errors to the correct form element. - $instance->getWidget(FALSE)->flagErrors($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); + $instance->getWidget()->flagErrors($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); } } } @@ -1004,7 +1014,7 @@ function field_ui_field_edit_form_submit($form, &$form_state) { // Extract field values. $items = array(); - $instance->getWidget(FALSE)->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); + $instance->getWidget()->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); $instance['default_value'] = $items ? $items : NULL; } @@ -1017,7 +1027,7 @@ function field_ui_field_edit_form_submit($form, &$form_state) { drupal_set_message(t('Saved %label configuration.', array('%label' => $instance['label']))); - if ($instance['required'] && empty($instance['default_value']) && $instance['widget']['type'] == 'field_hidden') { + if ($instance['required'] && empty($instance['default_value']) && empty($instance['default_value_function']) && $instance['widget']['type'] == 'field_hidden') { drupal_set_message(t('Field %label is required and uses the "hidden" widget. You might want to configure a default value.', array('%label' => $instance['label'])), 'warning'); }