public function WebformVariantApplyForm::submitForm in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Form/WebformVariantApplyForm.php \Drupal\webform\Form\WebformVariantApplyForm::submitForm()
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- src/
Form/ WebformVariantApplyForm.php, line 203
Class
- WebformVariantApplyForm
- Form for apply a webform variant.
Namespace
Drupal\webform\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$webform = $this->webform;
// Apply variant(s).
$variants = array_filter($form_state
->getValue('variants') ?: []);
if (empty($variants) && $this->webformVariant) {
$element_key = $this->webformVariant
->getElementKey();
$variant_id = $this->webformVariant
->getVariantId();
$variants = [
$element_key => $variant_id,
];
}
$webform
->applyVariants(NULL, $variants, TRUE);
$webform
->setOverride(FALSE);
$webform
->save();
$variant_plugin = $this->webformVariant;
if (!$this->hasMultipleVariants && empty($variant_plugin)) {
$variant_plugin = $webform
->getVariant(reset($variants));
}
// Delete variant and display a status message.
$t_args = [
'%title' => $variant_plugin
->label(),
];
switch ($form_state
->getValue('delete')) {
case 'selected':
foreach ($variants as $element_key => $variant_id) {
$variant_plugin = $webform
->getVariant($variant_id);
$this->webform
->deleteWebformVariant($variant_plugin);
}
$this->hasMultipleVariants ? $this
->messenger()
->addStatus($this
->t('The selected webform variants have been applied and deleted.')) : $this
->messenger()
->addStatus($this
->t('The webform variant %title has been applied and deleted.', $t_args));
break;
case 'all':
$variant_plugins = $webform
->getVariants();
foreach ($variant_plugins as $variant_plugin) {
$this->webform
->deleteWebformVariant($variant_plugin);
}
$this->hasMultipleVariants ? $this
->messenger()
->addStatus($this
->t('The selected webform variants have been applied and all variants have been deleted.')) : $this
->messenger()
->addStatus($this
->t('The webform variant %title has been applied and all variants have been deleted.', $t_args));
break;
case 'none':
default:
$this->hasMultipleVariants ? $this
->messenger()
->addStatus($this
->t('The selected webform variants have been applied.')) : $this
->messenger()
->addStatus($this
->t('The webform variant %title has been applied.', $t_args));
break;
}
$form_state
->setRedirectUrl($this->webform
->toUrl());
}