You are here

public function YamlFormUiElementTypeChangeForm::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

File

modules/yamlform_ui/src/Form/YamlFormUiElementTypeChangeForm.php, line 26

Class

YamlFormUiElementTypeChangeForm
Provides a change element type form for a form element.

Namespace

Drupal\yamlform_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, YamlFormInterface $yamlform = NULL, $key = NULL) {
  $element = $yamlform
    ->getElement($key);

  /** @var \Drupal\yamlform\YamlFormElementInterface $yamlform_element */
  $yamlform_element = $this->elementManager
    ->getElementInstance($element);
  $related_types = $yamlform_element
    ->getRelatedTypes($element);
  if (empty($related_types)) {
    throw new NotFoundHttpException();
  }
  $headers = [
    [
      'data' => $this
        ->t('Element'),
    ],
    [
      'data' => $this
        ->t('Category'),
    ],
    [
      'data' => $this
        ->t('Operations'),
    ],
  ];
  $definitions = $this
    ->getDefinitions();
  $rows = [];
  foreach ($related_types as $related_type_name => $related_type_label) {
    $plugin_definition = $definitions[$related_type_name];
    $row = [];
    $row['title']['data'] = $plugin_definition['label'];
    $row['category']['data'] = isset($plugin_definition['category']) ? $plugin_definition['category'] : $this
      ->t('Other');
    $row['operations']['data'] = [
      '#type' => 'operations',
      '#links' => [
        'change' => [
          'title' => $this
            ->t('Change'),
          'url' => Url::fromRoute('entity.yamlform_ui.element.edit_form', [
            'yamlform' => $yamlform
              ->id(),
            'key' => $key,
          ], [
            'query' => [
              'type' => $related_type_name,
            ],
          ]),
          'attributes' => YamlFormDialogHelper::getModalDialogAttributes(800),
        ],
      ],
    ];

    // Issue #2741877 Nested modals don't work: when using CKEditor in a
    // modal, then clicking the image button opens another modal,
    // which closes the original modal.
    // @todo Remove the below workaround once this issue is resolved.
    if ($related_type_name == 'processed_text') {
      unset($row['operations']['data']['#links']['change']['attributes']);
    }
    $rows[] = $row;
  }
  $form = [];
  $form['elements'] = [
    '#type' => 'table',
    '#header' => $headers,
    '#rows' => $rows,
    '#attributes' => [
      'class' => [
        'yamlform-ui-element-type-table',
      ],
    ],
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['cancel'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Cancel'),
    '#attributes' => YamlFormDialogHelper::getModalDialogAttributes(800, [
      'button',
    ]),
    '#url' => Url::fromRoute('entity.yamlform_ui.element.edit_form', [
      'yamlform' => $yamlform
        ->id(),
      'key' => $key,
    ]),
  ];
  return $form;
}