class WebformVariantViewForm in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Form/WebformVariantViewForm.php \Drupal\webform\Form\WebformVariantViewForm
Provides a view and tests form for webform variants.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\webform\Form\WebformVariantViewForm
Expanded class hierarchy of WebformVariantViewForm
1 string reference to 'WebformVariantViewForm'
File
- src/
Form/ WebformVariantViewForm.php, line 14
Namespace
Drupal\webform\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
105 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WebformVariantViewForm:: |
protected | property | The current operation (view or test). | |
WebformVariantViewForm:: |
protected | property | The webform. | |
WebformVariantViewForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
WebformVariantViewForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
WebformVariantViewForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |