function FieldItemList::defaultValueWidget

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/FieldItemList.php \Drupal\Core\Field\FieldItemList::defaultValueWidget()
  2. 8.9.x core/lib/Drupal/Core/Field/FieldItemList.php \Drupal\Core\Field\FieldItemList::defaultValueWidget()
  3. 11.x core/lib/Drupal/Core/Field/FieldItemList.php \Drupal\Core\Field\FieldItemList::defaultValueWidget()

Returns the widget object used in default value form.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

Return value

\Drupal\Core\Field\WidgetInterface|null A Widget object or NULL if no widget is available.

3 calls to FieldItemList::defaultValueWidget()
FieldItemList::defaultValuesForm in core/lib/Drupal/Core/Field/FieldItemList.php
Returns a form for the default value input.
FieldItemList::defaultValuesFormSubmit in core/lib/Drupal/Core/Field/FieldItemList.php
Processes the submitted default value.
FieldItemList::defaultValuesFormValidate in core/lib/Drupal/Core/Field/FieldItemList.php
Validates the submitted default value.

File

core/lib/Drupal/Core/Field/FieldItemList.php, line 344

Class

FieldItemList
Represents an entity field; that is, a list of field item objects.

Namespace

Drupal\Core\Field

Code

protected function defaultValueWidget(FormStateInterface $form_state) {
    if (!$form_state->has('default_value_widget')) {
        $entity = $this->getEntity();
        // Force a non-required widget.
        $definition = $this->getFieldDefinition();
        $definition->setRequired(FALSE);
        $definition->setDescription('');
        
        /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */
        $entity_form_display = \Drupal::service('entity_display.repository')->getFormDisplay($entity->getEntityTypeId(), $entity->bundle());
        
        /** @var \Drupal\Core\Field\WidgetPluginManager $field_widget_plugin_manager */
        $field_widget_plugin_manager = \Drupal::service('plugin.manager.field.widget');
        // Use the widget currently configured for the 'default' form mode, or
        // fallback to the default widget for the field type.
        if (($configuration = $entity_form_display->getComponent($definition->getName())) && isset($configuration['type'])) {
            // Get the plugin instance manually to ensure an up-to-date field
            // definition is used.
            // @see \Drupal\Core\Entity\Entity\EntityFormDisplay::getRenderer
            $widget = $field_widget_plugin_manager->getInstance([
                'field_definition' => $definition,
                'form_mode' => $entity_form_display->getOriginalMode(),
                'prepare' => FALSE,
                'configuration' => $configuration,
            ]);
        }
        else {
            $options = [
                'field_definition' => $this->getFieldDefinition(),
            ];
            // If the field does not have a widget configured in the 'default' form
            // mode, check if there are default entity form display options defined
            // for the 'default' form mode in the form state.
            // @see \Drupal\field_ui\Controller\FieldConfigAddController::fieldConfigAddConfigureForm
            if (($default_options = $form_state->get('default_options')) && isset($default_options['entity_form_display']['default'])) {
                $options['configuration'] = $default_options['entity_form_display']['default'];
            }
            $widget = $field_widget_plugin_manager->getInstance($options);
        }
        $form_state->set('default_value_widget', $widget);
    }
    return $form_state->get('default_value_widget');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.