function FieldStorageAddForm::buildForm

Same name and namespace in other branches
  1. 10 core/modules/field_ui/src/Form/FieldStorageAddForm.php \Drupal\field_ui\Form\FieldStorageAddForm::buildForm()
  2. 9 core/modules/field_ui/src/Form/FieldStorageAddForm.php \Drupal\field_ui\Form\FieldStorageAddForm::buildForm()
  3. 8.9.x core/modules/field_ui/src/Form/FieldStorageAddForm.php \Drupal\field_ui\Form\FieldStorageAddForm::buildForm()
  4. main core/modules/field_ui/src/Form/FieldStorageAddForm.php \Drupal\field_ui\Form\FieldStorageAddForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/field_ui/src/Form/FieldStorageAddForm.php, line 76

Class

FieldStorageAddForm
Provides a form for the "field storage" add subform.

Namespace

Drupal\field_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL, $selected_field_type = NULL, $display_as_group = 'false') {
  $display_as_group = str_contains($display_as_group, 'true');
  if (!$form_state->get('entity_type_id')) {
    $form_state->set('entity_type_id', $entity_type_id);
  }
  if (!$form_state->get('bundle')) {
    $form_state->set('bundle', $bundle);
  }
  if (!$form_state->get('field_type')) {
    $form_state->set('field_type', $selected_field_type);
  }
  if (!$form_state->get('display_as_group')) {
    $form_state->set('display_as_group', $display_as_group);
  }
  $this->entityTypeId = $form_state->get('entity_type_id');
  $this->bundle = $form_state->get('bundle');
  $entity_type = $this->entityTypeManager
    ->getDefinition($this->entityTypeId);
  $route_parameters_back = [] + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Label'),
    '#size' => 30,
    '#required' => TRUE,
    '#maxlength' => 255,
    '#weight' => -20,
  ];
  $field_prefix = $this->configFactory
    ->get('field_ui.settings')
    ->get('field_prefix');
  $form['field_name'] = [
    '#type' => 'machine_name',
    '#field_prefix' => $field_prefix,
    '#size' => 15,
    '#description' => $this->t('A unique machine-readable name containing letters, numbers, and underscores.'),
    // Calculate characters depending on the length of the field prefix
    // setting. Maximum length is 32.
'#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen($field_prefix),
    '#machine_name' => [
      'source' => [
        'label',
      ],
      'exists' => [
        $this,
        'fieldNameExists',
      ],
    ],
    '#required' => TRUE,
  ];
  $form['field_options_wrapper'] = [
    '#prefix' => '<div class="field-options-wrapper">',
    '#suffix' => '</div>',
  ];
  // Set the selected field to the form state by checking
  // the checked attribute.
  if (isset($selected_field_type)) {
    if ($display_as_group) {
      $form = $this->buildGroupForm($form, $form_state);
    }
    $form['actions']['previous'] = [
      '#type' => 'link',
      '#title' => $this->t('Change field type'),
      '#url' => Url::fromRoute("field_ui.field_storage_config_add_{$entity_type_id}", $route_parameters_back),
      '#attributes' => [
        'class' => [
          'button',
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => '1100',
        ]),
      ],
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Continue'),
      '#submit' => [
        '::submitForm',
      ],
      '#attributes' => [
        'class' => [
          'button',
          'button--primary',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => '1100',
        ]),
      ],
    ];
    if ($this->isAjax()) {
      $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
    }
  }
  // Place the 'translatable' property as an explicit value so that contrib
  // modules can form_alter() the value for newly created fields. By default,
  // we create field storage as translatable, so it will be possible to enable
  // translation at field level.
  $form['translatable'] = [
    '#type' => 'value',
    '#value' => TRUE,
  ];
  $form['#prefix'] = '<div id="field-storage-subfield">';
  $form['#suffix'] = '</div>';
  $form['#attached']['library'] = [
    'field_ui/drupal.field_ui',
    'field_ui/drupal.field_ui.manage_fields',
    'core/drupal.dialog.ajax',
  ];
  return $form;
}

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