W3cubDocs

/Drupal 8

protected function FieldItemList::defaultValueWidget

protected FieldItemList::defaultValueWidget(FormStateInterface $form_state)

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.

File

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

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 = entity_get_form_display($entity->getEntityTypeId(), $entity->bundle(), 'default');
    $widget = $entity_form_display->getRenderer($this->getFieldDefinition()->getName());
    if (!$widget) {
      $widget = \Drupal::service('plugin.manager.field.widget')->getInstance(array('field_definition' => $this->getFieldDefinition()));
    }

    $form_state->set('default_value_widget', $widget);
  }

  return $form_state->get('default_value_widget');
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Field!FieldItemList.php/function/FieldItemList::defaultValueWidget/8.1.x