You are here

public function YamlFormUiElementFormBase::buildForm in YAML Form 8

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

3 calls to YamlFormUiElementFormBase::buildForm()
YamlFormUiElementAddForm::buildForm in modules/yamlform_ui/src/Form/YamlFormUiElementAddForm.php
Form constructor.
YamlFormUiElementDuplicateForm::buildForm in modules/yamlform_ui/src/Form/YamlFormUiElementDuplicateForm.php
Form constructor.
YamlFormUiElementEditForm::buildForm in modules/yamlform_ui/src/Form/YamlFormUiElementEditForm.php
Form constructor.
4 methods override YamlFormUiElementFormBase::buildForm()
YamlFormUiElementAddForm::buildForm in modules/yamlform_ui/src/Form/YamlFormUiElementAddForm.php
Form constructor.
YamlFormUiElementDuplicateForm::buildForm in modules/yamlform_ui/src/Form/YamlFormUiElementDuplicateForm.php
Form constructor.
YamlFormUiElementEditForm::buildForm in modules/yamlform_ui/src/Form/YamlFormUiElementEditForm.php
Form constructor.
YamlFormUiElementTestForm::buildForm in modules/yamlform_ui/src/Form/YamlFormUiElementTestForm.php
Form constructor.

File

modules/yamlform_ui/src/Form/YamlFormUiElementFormBase.php, line 122

Class

YamlFormUiElementFormBase
Provides a base class for form element forms.

Namespace

Drupal\yamlform_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, YamlFormInterface $yamlform = NULL, $key = NULL, $parent_key = '') {
  $this->yamlform = $yamlform;
  $yamlform_element = $this
    ->getYamlFormElement();
  $form['properties'] = $yamlform_element
    ->buildConfigurationForm([], $form_state);

  // Move messages to the top of the form.
  if (isset($form['properties']['messages'])) {
    $form['messages'] = $form['properties']['messages'];
    $form['messages']['#weight'] = -100;
    unset($form['properties']['messages']);
  }

  // Set parent key.
  $form['parent_key'] = [
    '#type' => 'value',
    '#value' => $parent_key,
  ];

  // Set element type.
  $form['properties']['element']['type'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Type'),
    'label' => [
      '#markup' => $yamlform_element
        ->getPluginLabel(),
    ],
    '#weight' => -100,
    '#parents' => [
      'type',
    ],
  ];

  // Set change element type.
  if ($key && $yamlform_element
    ->getRelatedTypes($this->element)) {
    $route_parameters = [
      'yamlform' => $yamlform
        ->id(),
      'key' => $key,
    ];
    if ($this->originalType) {
      $original_yamlform_element = $this->elementManager
        ->createInstance($this->originalType);
      $route_parameters = [
        'yamlform' => $yamlform
          ->id(),
        'key' => $key,
      ];
      $form['properties']['element']['type']['cancel'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('Cancel'),
        '#url' => new Url('entity.yamlform_ui.element.edit_form', $route_parameters),
        '#attributes' => YamlFormDialogHelper::getModalDialogAttributes(800, [
          'button',
          'button--small',
        ]),
      ];
      $form['properties']['element']['type']['#description'] = '(' . $this
        ->t('Changing from %type', [
        '%type' => $original_yamlform_element
          ->getPluginLabel(),
      ]) . ')';
    }
    else {
      $form['properties']['element']['type']['change_type'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('Change'),
        '#url' => new Url('entity.yamlform_ui.change_element', $route_parameters),
        '#attributes' => YamlFormDialogHelper::getModalDialogAttributes(800, [
          'button',
          'button--small',
        ]),
      ];
    }
  }

  // Set element key reserved word warning message.
  if (!$key) {
    $reserved_keys = [
      'form_build_id',
      'form_token',
      'form_id',
      'data',
      'op',
    ];
    $reserved_keys = array_merge($reserved_keys, array_keys(\Drupal::service('entity_field.manager')
      ->getBaseFieldDefinitions('yamlform_submission')));
    $form['#attached']['drupalSettings']['yamlform_ui']['reserved_keys'] = $reserved_keys;
    $form['#attached']['library'][] = 'yamlform_ui/yamlform_ui.element';
    $form['properties']['element']['key_warning'] = [
      '#type' => 'yamlform_message',
      '#message_type' => 'warning',
      '#message_message' => $this
        ->t("Please avoid using the reserved word '@key' as the element's key."),
      '#weight' => -99,
      '#attributes' => [
        'style' => 'display:none',
      ],
    ];
  }

  // Set element key.
  $form['properties']['element']['key'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Key'),
    '#machine_name' => [
      'label' => $this
        ->t('Key'),
      'exists' => [
        $this,
        'exists',
      ],
      'source' => [
        'title',
      ],
    ],
    '#required' => TRUE,
    '#parents' => [
      'key',
    ],
    '#disabled' => $key ? TRUE : FALSE,
    '#default_value' => $key,
    '#weight' => -98,
  ];

  // Remove the key's help text (aka description) once it has been set.
  if ($key) {
    $form['properties']['element']['key']['#description'] = NULL;
  }

  // Use title for key (machine_name).
  if (isset($form['properties']['element']['title'])) {
    $form['properties']['element']['key']['#machine_name']['source'] = [
      'properties',
      'element',
      'title',
    ];
    $form['properties']['element']['title']['#id'] = 'title';
  }

  // Set flex.
  // Hide #flex property if parent element is not a 'yamlform_flexbox'.
  if (isset($form['properties']['flex']) && !$this
    ->isParentElementFlexbox($key, $parent_key)) {
    $form['properties']['flex']['#access'] = FALSE;
  }

  // Set actions.
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#button_type' => 'primary',
    '#_validate_form' => TRUE,
  ];
  $form = $this
    ->buildDialog($form, $form_state);
  return $form;
}