You are here

public function WebformVariantViewForm::buildForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Form/WebformVariantViewForm.php \Drupal\webform\Form\WebformVariantViewForm::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.

\Drupal\webform\WebformInterface $webform: The webform.

string $operation: The webform operation.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/WebformVariantViewForm.php, line 52

Class

WebformVariantViewForm
Provides a view and tests form for webform variants.

Namespace

Drupal\webform\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, WebformInterface $webform = NULL, $operation = 'view') {
  $this->operation = $operation;
  $this->webform = $webform;
  switch ($operation) {
    case 'view':
      $form['#rel'] = 'canonical';
      $submit_label = $this
        ->t('View');
      $t_args = [
        '@operation' => $this
          ->t('view'),
      ];
      break;
    case 'test':
      $form['#rel'] = 'test-form';
      $submit_label = $this
        ->t('Test');
      $t_args = [
        '@operation' => $this
          ->t('test'),
      ];
      break;
    case 'share':
      $form['#rel'] = 'share-embed';
      $submit_label = $this
        ->t('Share');
      $t_args = [
        '@operation' => $this
          ->t('share'),
      ];
      break;
    default:
      throw new NotFoundHttpException();
  }
  $form['description'] = [
    '#type' => 'container',
    'text' => [
      '#markup' => $this
        ->t('Select variants and then click submit, which will redirect you to the @operation form.', $t_args),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    ],
  ];
  $element_keys = $webform
    ->getElementsVariant();
  if (isset($element_keys)) {
    $form['variants'] = [
      '#tree' => TRUE,
    ];
    foreach ($element_keys as $element_key) {
      $element = $webform
        ->getElement($element_key);
      $variants = $webform
        ->getVariants(NULL, TRUE, $element_key);
      if (!$variants
        ->count()) {
        continue;
      }
      $variant_options = [];
      foreach ($variants as $variant) {
        $variant_options[$variant
          ->getVariantId()] = $variant
          ->label();
      }
      $form['variants'][$element_key] = [
        '#type' => 'select',
        '#title' => WebformElementHelper::getAdminTitle($element),
        '#options' => $variant_options,
        '#empty_option' => $this
          ->t('- None -'),
      ];
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $submit_label,
    '#button_type' => 'primary',
  ];
  return $form;
}