function FieldConfigEditForm::form

Same name in other branches
  1. 9 core/modules/field_ui/src/Form/FieldConfigEditForm.php \Drupal\field_ui\Form\FieldConfigEditForm::form()
  2. 10 core/modules/field_ui/src/Form/FieldConfigEditForm.php \Drupal\field_ui\Form\FieldConfigEditForm::form()
  3. 11.x core/modules/field_ui/src/Form/FieldConfigEditForm.php \Drupal\field_ui\Form\FieldConfigEditForm::form()

Overrides EntityForm::form

File

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

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,
        '#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;
}

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