protected function FieldInstanceEditForm::getDefaultValueWidget

Builds the default value widget for a given field instance.

1 call to FieldInstanceEditForm::getDefaultValueWidget()
FieldInstanceEditForm::buildForm in drupal/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php
Form constructor.

File

drupal/core/modules/field_ui/lib/Drupal/field_ui/Form/FieldInstanceEditForm.php, line 224
Contains \Drupal\field_ui\Form\FieldInstanceEditForm.

Class

FieldInstanceEditForm
Provides a form for the field instance settings form.

Namespace

Drupal\field_ui\Form

Code

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(),
  );

  // Adjust the instance definition used for the form element. We want a
  // non-required input and no description.
  $this->instance['required'] = FALSE;
  $this->instance['description'] = '';

  // Adjust the instance definition to use the default widget of this field type
  // instead of the hidden widget.
  if ($this->instance['widget']['type'] == 'field_hidden') {
    $field_type = field_info_field_types($field['type']);
    $default_widget = $this->widgetManager
      ->getDefinition($field_type['default_widget']);
    $this->instance['widget'] = array(
      'type' => $default_widget['id'],
      'settings' => $default_widget['settings'],
      'weight' => 0,
    );
  }

  // Insert the widget. Since we do not use the "official" instance definition,
  // the whole flow cannot use field_invoke_method().
  $items = array();
  if (!empty($this->instance['default_value'])) {
    $items = (array) $this->instance['default_value'];
  }
  $element += $entity_form_display
    ->getWidget($this->instance
    ->getField()->id)
    ->form($entity, Language::LANGCODE_NOT_SPECIFIED, $items, $element, $form_state);
  return $element;
}