You are here

class WebformVariantViewForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Form/WebformVariantViewForm.php \Drupal\webform\Form\WebformVariantViewForm

Provides a view and tests form for webform variants.

Hierarchy

Expanded class hierarchy of WebformVariantViewForm

1 string reference to 'WebformVariantViewForm'
webform.routing.yml in ./webform.routing.yml
webform.routing.yml

File

src/Form/WebformVariantViewForm.php, line 14

Namespace

Drupal\webform\Form
View source
class WebformVariantViewForm extends FormBase {

  /**
   * The current operation (view or test).
   *
   * @var string
   */
  protected $operation;

  /**
   * The webform.
   *
   * @var \Drupal\webform\WebformInterface
   */
  protected $webform;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'webform_variant_view_form';
  }

  /**
   * Form constructor.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param \Drupal\webform\WebformInterface $webform
   *   The webform.
   * @param string $operation
   *   The webform operation.
   *
   * @return array
   *   The form structure.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $variants = $form_state
      ->getValue('variants');

    // Build query string.
    $query = [];
    $element_keys = $this->webform
      ->getElementsVariant();
    foreach ($element_keys as $element_key) {
      if (empty($variants[$element_key])) {
        continue;
      }
      $variant_id = $variants[$element_key];
      $variant_element = $this->webform
        ->getElement($element_key);

      // If #prepopulate is disabled use '_webform_variant'
      // querystring parameter for view and test operations.
      // @see \Drupal\webform\Entity\Webform::getSubmissionForm
      if (empty($variant_element['#prepopulate'])) {
        $query += [
          '_webform_variant' => [],
        ];
        $query['_webform_variant'][$element_key] = $variant_id;
      }
      else {
        $query[$element_key] = $variant_id;
      }
    }
    $options = [
      'query' => $query,
    ];
    $rel = $form['#rel'];
    $redirect_url = $this->webform
      ->toUrl($rel, $options);
    $form_state
      ->setRedirectUrl($redirect_url);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 105
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebformVariantViewForm::$operation protected property The current operation (view or test).
WebformVariantViewForm::$webform protected property The webform.
WebformVariantViewForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
WebformVariantViewForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
WebformVariantViewForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm