You are here

public function FieldGroupAddForm::buildFormatterSelectionForm in Field Group 8.3

Same name and namespace in other branches
  1. 8 src/Form/FieldGroupAddForm.php \Drupal\field_group\Form\FieldGroupAddForm::buildFormatterSelectionForm()

Build the formatter selection step.

1 call to FieldGroupAddForm::buildFormatterSelectionForm()
FieldGroupAddForm::buildForm in src/Form/FieldGroupAddForm.php
Form constructor.

File

src/Form/FieldGroupAddForm.php, line 143

Class

FieldGroupAddForm
Provides a form for adding a fieldgroup to a bundle.

Namespace

Drupal\field_group\Form

Code

public function buildFormatterSelectionForm(array &$form, FormStateInterface $form_state) {

  // Gather group formatters.
  $formatter_options = FormatterHelper::formatterOptions($this->context);
  $form['add'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'form--inline',
        'clearfix',
      ],
    ],
  ];
  $form['add']['group_formatter'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Add a new group'),
    '#options' => $formatter_options,
    '#empty_option' => $this
      ->t('- Select a field group type -'),
    '#required' => TRUE,
  ];

  // Field label and field_name.
  $form['new_group_wrapper'] = [
    '#type' => 'container',
    '#states' => [
      '!visible' => [
        ':input[name="group_formatter"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $form['new_group_wrapper']['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#size' => 15,
    '#required' => TRUE,
  ];
  $form['new_group_wrapper']['group_name'] = [
    '#type' => 'machine_name',
    '#size' => 15,
    // This field should stay LTR even for RTL languages.
    '#field_prefix' => '<span dir="ltr">' . self::GROUP_PREFIX,
    '#field_suffix' => '</span>&lrm;',
    '#description' => $this
      ->t('A unique machine-readable name containing letters, numbers, and underscores.'),
    '#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen(self::GROUP_PREFIX),
    '#machine_name' => [
      'source' => [
        'new_group_wrapper',
        'label',
      ],
      'exists' => [
        $this,
        'groupNameExists',
      ],
    ],
    '#required' => TRUE,
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save and continue'),
    '#button_type' => 'primary',
    '#validate' => [
      [
        $this,
        'validateFormatterSelection',
      ],
    ],
  ];
  $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
}