You are here

public function FieldConfigEditForm::form in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field_ui/src/Form/FieldConfigEditForm.php \Drupal\field_ui\Form\FieldConfigEditForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/field_ui/src/Form/FieldConfigEditForm.php, line 57

Class

FieldConfigEditForm
Provides a form for the field settings form.

Namespace

Drupal\field_ui\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $field_storage = $this->entity
    ->getFieldStorageDefinition();
  $bundles = $this->entityTypeBundleInfo
    ->getBundleInfo($this->entity
    ->getTargetEntityTypeId());
  $form_title = $this
    ->t('%field settings for %bundle', [
    '%field' => $this->entity
      ->getLabel(),
    '%bundle' => $bundles[$this->entity
      ->getTargetBundle()]['label'],
  ]);
  $form['#title'] = $form_title;
  if ($field_storage
    ->isLocked()) {
    $form['locked'] = [
      '#markup' => $this
        ->t('The field %field is locked and cannot be edited.', [
        '%field' => $this->entity
          ->getLabel(),
      ]),
    ];
    return $form;
  }

  // Build the configurable field values.
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#default_value' => $this->entity
      ->getLabel() ?: $field_storage
      ->getName(),
    '#required' => TRUE,
    '#maxlength' => 255,
    '#weight' => -20,
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Help text'),
    '#default_value' => $this->entity
      ->getDescription(),
    '#rows' => 5,
    '#description' => $this
      ->t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', [
      '@tags' => FieldFilteredMarkup::displayAllowedTags(),
    ]) . '<br />' . $this
      ->t('This field supports tokens.'),
    '#weight' => -10,
  ];
  $form['required'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Required field'),
    '#default_value' => $this->entity
      ->isRequired(),
    '#weight' => -5,
  ];

  // Create an arbitrary entity object (used by the 'default value' widget).
  $ids = (object) [
    'entity_type' => $this->entity
      ->getTargetEntityTypeId(),
    'bundle' => $this->entity
      ->getTargetBundle(),
    'entity_id' => NULL,
  ];
  $form['#entity'] = _field_create_entity_from_ids($ids);
  $items = $form['#entity']
    ->get($this->entity
    ->getName());
  $item = $items
    ->first() ?: $items
    ->appendItem();

  // Add field settings for the field type and a container for third party
  // settings that modules can add to via hook_form_FORM_ID_alter().
  $form['settings'] = [
    '#tree' => TRUE,
    '#weight' => 10,
  ];
  $form['settings'] += $item
    ->fieldSettingsForm($form, $form_state);
  $form['third_party_settings'] = [
    '#tree' => TRUE,
    '#weight' => 11,
  ];

  // Add handling for default value.
  if ($element = $items
    ->defaultValuesForm($form, $form_state)) {
    $element = array_merge($element, [
      '#type' => 'details',
      '#title' => $this
        ->t('Default value'),
      '#open' => TRUE,
      '#tree' => TRUE,
      '#description' => $this
        ->t('The default value for this field, used when creating new content.'),
      '#weight' => 12,
    ]);
    $form['default_value'] = $element;
  }
  return $form;
}