You are here

public function WebformDevelEntitySchemaForm::form in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_devel/src/Form/WebformDevelEntitySchemaForm.php \Drupal\webform_devel\Form\WebformDevelEntitySchemaForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

modules/webform_devel/src/Form/WebformDevelEntitySchemaForm.php, line 49

Class

WebformDevelEntitySchemaForm
Get webform schema.

Namespace

Drupal\webform_devel\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $webform_ui_exists = $this->moduleHandler
    ->moduleExists('webform_ui');

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $this
    ->getEntity();

  // Header.
  $header = $this->scheme
    ->getColumns();
  if ($webform_ui_exists) {
    $header['operations'] = $this
      ->t('Operations');
  }

  // Rows.
  $rows = [];
  $elements = $this->scheme
    ->getElements($webform);
  foreach ($elements as $element_key => $element) {
    $rows[$element_key] = [];
    foreach ($element as $key => $value) {
      if ($key === 'options') {
        $value = implode('; ', array_slice($value, 0, 12)) . (count($value) > 12 ? '; …' : '');
      }
      $rows[$element_key][$key] = [
        '#markup' => $value,
      ];
    }
    if ($element['datatype'] === 'Composite') {
      $rows[$element_key]['#attributes']['class'][] = 'webform-devel-schema-composite';
    }
    if ($webform_ui_exists) {

      // Only add 'Edit' link to main element and not composite sub-elements.
      if (strpos($element_key, '.') === FALSE) {
        $element_url = new Url('entity.webform_ui.element.edit_form', [
          'webform' => $webform
            ->id(),
          'key' => $element_key,
        ], [
          'query' => [
            'destination' => Url::fromRoute('<current>')
              ->toString(),
          ],
        ]);
        $rows[$element_key]['name'] = [
          '#type' => 'link',
          '#title' => $element_key,
          '#url' => $element_url,
          '#attributes' => WebformDialogHelper::getModalDialogAttributes(),
        ];
        $rows[$element_key]['operations'] = [
          '#type' => 'link',
          '#title' => $this
            ->t('Edit'),
          '#url' => $element_url,
          '#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NORMAL, [
            'button',
            'button--small',
          ]),
        ];
      }
      else {
        $rows[$element_key]['operations'] = [
          '#markup' => '',
        ];
      }

      // Add webform key used by Ajax callback.
      $rows[$element_key]['#attributes']['data-webform-key'] = explode('.', $element_key)[0];
    }
  }

  // Table.
  $form['schema'] = [
    '#type' => 'table',
    '#header' => $header,
    '#attributes' => [
      'class' => [
        'webform-devel-schema-table',
      ],
    ],
  ] + $rows;
  WebformDialogHelper::attachLibraries($form);
  $form['#attached']['library'][] = 'webform_devel/webform_devel';
  return $form;
}