You are here

protected function FieldItemList::defaultValueWidget in Drupal 9

Same name and namespace in other branches
  1. 8 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('');

    // Use the widget currently configured for the 'default' form mode, or
    // fallback to the default widget for the field type.
    $entity_form_display = \Drupal::service('entity_display.repository')
      ->getFormDisplay($entity
      ->getEntityTypeId(), $entity
      ->bundle());
    $widget = $entity_form_display
      ->getRenderer($this
      ->getFieldDefinition()
      ->getName());
    if (!$widget) {
      $widget = \Drupal::service('plugin.manager.field.widget')
        ->getInstance([
        'field_definition' => $this
          ->getFieldDefinition(),
      ]);
    }
    $form_state
      ->set('default_value_widget', $widget);
  }
  return $form_state
    ->get('default_value_widget');
}