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. 8.9.x 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 124

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);
    $form['#entity_builders'][] = 'field_form_field_config_edit_form_entity_builder';
    $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['field_storage'] = [
        '#type' => 'fieldset',
        '#title' => $this->t('Field Storage'),
        '#weight' => -15,
        '#tree' => TRUE,
    ];
    $form['field_storage']['subform'] = [
        '#parents' => [
            'field_storage',
            'subform',
        ],
    ];
    $form['field_storage']['subform']['field_storage_submit'] = [
        '#type' => 'submit',
        '#name' => 'field_storage_submit',
        '#attributes' => [
            'class' => [
                'js-hide',
            ],
        ],
        '#value' => $this->t('Update settings'),
        '#process' => [
            '::processFieldStorageSubmit',
        ],
        '#limit_validation_errors' => [
            $form['field_storage']['subform']['#parents'],
        ],
        '#submit' => [
            '::fieldStorageSubmit',
        ],
    ];
    $field_storage_form = $this->entityTypeManager
        ->getFormObject('field_storage_config', $this->operation);
    $field_storage_form->setEntity($field_storage);
    $subform_state = SubformState::createForSubform($form['field_storage']['subform'], $form, $form_state, $field_storage_form);
    $form['field_storage']['subform'] = $field_storage_form->buildForm($form['field_storage']['subform'], $subform_state, $this->entity);
    $form['#entity'] = _field_create_entity_from_ids($ids);
    $items = $this->getTypedData($this->entity, $form['#entity']);
    $item = $items->first() ?: $items->appendItem();
    $this->addAjaxCallbacks($form['field_storage']['subform']);
    if (isset($form['field_storage']['subform']['cardinality_container'])) {
        $form['field_storage']['subform']['cardinality_container']['#parents'] = [
            'field_storage',
            'subform',
        ];
    }
    // 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,
    ];
    // Create a new instance of typed data for the field to ensure that default
    // value widget is always rendered from a clean state.
    $items = $this->getTypedData($this->entity, $form['#entity']);
    // Add handling for default value.
    if ($element = $items->defaultValuesForm($form, $form_state)) {
        $has_required = $this->hasAnyRequired($element);
        $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,
        ]);
        if (!$has_required) {
            $has_default_value = count($this->entity
                ->getDefaultValue($form['#entity'])) > 0;
            $element['#states'] = [
                'invisible' => [
                    ':input[name="set_default_value"]' => [
                        'checked' => FALSE,
                    ],
                ],
            ];
            $form['set_default_value'] = [
                '#type' => 'checkbox',
                '#title' => $this->t('Set default value'),
                '#default_value' => $has_default_value,
                '#description' => $this->t('Provide a pre-filled value for the editing form.'),
                '#weight' => $element['#weight'],
            ];
        }
        $form['default_value'] = $element;
    }
    $form['#prefix'] = '<div id="field-combined">';
    $form['#suffix'] = '</div>';
    $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
    return $form;
}

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