You are here

public function WebformUiElementTestForm::buildForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_ui/src/Form/WebformUiElementTestForm.php \Drupal\webform_ui\Form\WebformUiElementTestForm::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 WebformUiElementFormBase::buildForm

File

modules/webform_ui/src/Form/WebformUiElementTestForm.php, line 52

Class

WebformUiElementTestForm
Provides a test webform for webform elements.

Namespace

Drupal\webform_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, WebformInterface $webform = NULL, $key = NULL, $parent_key = NULL, $type = NULL) {

  // Create a temp webform.
  $this->webform = Webform::create([
    'id' => '_webform_ui_temp_form',
  ]);
  $this->type = $type;
  if (!$this->elementManager
    ->hasDefinition($type)) {
    throw new NotFoundHttpException();
  }
  if ($element = \Drupal::request()
    ->getSession()
    ->get('webform_ui_test_element_' . $type)) {
    $this->element = $element;
  }
  else {
    $element = [
      '#type' => $type,
    ] + $this
      ->getWebformElementPlugin()
      ->preview();
    $this->element = $element;
  }
  $webform_element = $this
    ->getWebformElementPlugin();
  $form['#title'] = $this
    ->t('Test %type element', [
    '%type' => $type,
  ]);
  if ($element) {
    $webform_submission = WebformSubmission::create([
      'webform' => $this->webform,
    ]);
    $webform_element
      ->initialize($this->element);
    $webform_element
      ->prepare($this->element, $webform_submission);
    $form['test'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Element test'),
      '#open' => TRUE,
      '#attributes' => [
        'style' => 'background-color: #f5f5f2',
      ],
      'element' => $this->element,
      'hr' => [
        '#markup' => '<hr />',
      ],
    ];
    if (isset($element['#default_value'])) {
      $html = $webform_element
        ->formatHtml($element + [
        '#value' => $element['#default_value'],
      ], $webform_submission);
      $form['test']['html'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('HTML'),
        '#markup' => is_array($html) ? $this->renderer
          ->render($html) : $html,
        '#allowed_tag' => Xss::getAdminTagList(),
      ];
      $form['test']['text'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Plain text'),
        '#markup' => '<pre>' . $webform_element
          ->formatText($element + [
          '#value' => $element['#default_value'],
        ], $webform_submission) . '</pre>',
        '#allowed_tag' => Xss::getAdminTagList(),
      ];
    }
    $form['test']['code'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Source'),
      'source' => [
        '#theme' => 'webform_codemirror',
        '#type' => 'yaml',
        '#code' => Yaml::encode($this
          ->convertTranslatableMarkupToStringRecursive($element)),
      ],
    ];
    $form['test']['render_array'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Render array'),
      '#desciption' => $this
        ->t("Below is the element's final render array."),
      'source' => [
        '#theme' => 'webform_codemirror',
        '#type' => 'yaml',
        '#code' => Yaml::encode($this
          ->convertTranslatableMarkupToStringRecursive($this->element)),
      ],
    ];
  }
  $form['key'] = [
    '#type' => 'value',
    '#value' => 'element',
  ];
  $form['parent_key'] = [
    '#type' => 'value',
    '#value' => '',
  ];
  $form['properties'] = $webform_element
    ->buildConfigurationForm([
    '#tabs' => FALSE,
  ], $form_state);
  $form['properties']['#tree'] = TRUE;
  $form['properties']['custom']['#open'] = TRUE;
  $form['properties']['element']['type'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Type'),
    '#markup' => $type,
    '#weight' => -100,
    '#parents' => [
      'type',
    ],
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Test'),
    '#button_type' => 'primary',
  ];
  if (\Drupal::request()
    ->getSession()
    ->get('webform_ui_test_element_' . $type)) {
    $form['actions']['reset'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Reset'),
      '#limit_validation_errors' => [],
      '#submit' => [
        '::reset',
      ],
    ];
  }

  // Clear all messages including 'Unable to display this webform…' which is
  // generated because we are using a temp webform.
  // drupal_get_messages();
  return $form;
}