View source
<?php
namespace Drupal\webform\Form;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform\Utility\WebformFormHelper;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformTokenManagerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WebformVariantFormBase extends FormBase {
use WebformDialogFormTrait;
const MACHINE_NAME_MAXLENGHTH = 64;
protected $tokenManager;
protected $webform;
protected $webformVariant;
public function getFormId() {
return 'webform_variant_form';
}
public function __construct(WebformTokenManagerInterface $token_manager) {
$this->tokenManager = $token_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('webform.token_manager'));
}
public function buildForm(array $form, FormStateInterface $form_state, WebformInterface $webform = NULL, $webform_variant = NULL) {
$this->webform = $webform;
try {
$this->webformVariant = $this
->prepareWebformVariant($webform_variant);
} catch (PluginNotFoundException $e) {
throw new NotFoundHttpException("Invalid variant id: '{$webform_variant}'.");
}
$form['#webform_id'] = $this->webform
->id();
$form['#webform_variant_id'] = $this->webformVariant
->getVariantId();
$form['#webform_variant_plugin_id'] = $this->webformVariant
->getPluginId();
$request = $this
->getRequest();
$form['description'] = [
'#type' => 'container',
'text' => [
'#markup' => $this->webformVariant
->description(),
'#prefix' => '<p>',
'#suffix' => '</p>',
],
'#weight' => -20,
];
$form['id'] = [
'#type' => 'value',
'#value' => $this->webformVariant
->getPluginId(),
];
$form['general'] = [
'#type' => 'fieldset',
'#title' => $this
->t('General settings'),
'#weight' => -10,
];
$form['general']['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#maxlength' => 255,
'#default_value' => $this->webformVariant
->getLabel(),
'#required' => TRUE,
'#attributes' => [
'autofocus' => 'autofocus',
],
];
$t_args = [
'@requirements' => $this
->t('letters, numbers, underscores, and dashes'),
];
$form['general']['variant_id'] = [
'#type' => 'machine_name',
'#maxlength' => static::MACHINE_NAME_MAXLENGHTH,
'#description' => $this
->t('A unique name for this variant instance. Can only contain @requirements.', $t_args),
'#default_value' => $this->webformVariant
->getVariantId(),
'#required' => TRUE,
'#disabled' => $this->webformVariant
->getVariantId() ? TRUE : FALSE,
'#machine_name' => [
'source' => [
'general',
'label',
],
'exists' => [
$this,
'exists',
],
'replace_pattern' => $this->webformVariant
->getMachineNameReplacePattern(),
'replace' => $this->webformVariant
->getMachineNameReplace(),
'error' => $this
->t('The element key name must contain only @requirements.', $t_args),
],
];
$variant_options = $this
->getVariantElementsAsOptions();
if (count($variant_options) === 1) {
$form['general']['element_key'] = [
'#type' => 'value',
'#value' => key($variant_options),
];
$form['general']['element_key_item'] = [
'#title' => $this
->t('Element'),
'#type' => 'item',
'#markup' => reset($variant_options),
'#access' => TRUE,
];
}
else {
$form['general']['element_key'] = [
'#type' => 'select',
'#title' => $this
->t('Element'),
'#options' => $variant_options,
'#default_value' => $this->webformVariant
->getElementKey(),
'#required' => TRUE,
];
}
$form['general']['notes'] = [
'#type' => 'textarea',
'#title' => $this
->t('Administrative notes'),
'#description' => $this
->t("Entered text will be displayed on the variants administrative page."),
'#rows' => 2,
'#default_value' => $this->webformVariant
->getNotes(),
];
$form['advanced'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Advanced settings'),
'#weight' => -10,
];
$form['advanced']['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable the %name variant', [
'%name' => $this->webformVariant
->label(),
]),
'#return_value' => TRUE,
'#default_value' => $this->webformVariant
->isEnabled(),
'#disabled' => $this->webformVariant
->getPluginId() === 'broken',
];
$form['#parents'] = [];
$form['settings'] = [
'#tree' => TRUE,
'#parents' => [
'settings',
],
];
$subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
$form['settings'] = $this->webformVariant
->buildConfigurationForm($form['settings'], $subform_state);
if (isset($form['settings']['#attributes']['novalidate'])) {
$form['#attributes']['novalidate'] = 'novalidate';
}
$form['settings']['#tree'] = TRUE;
$form['weight'] = [
'#type' => 'hidden',
'#value' => $request->query
->has('weight') ? (int) $request->query
->get('weight') : $this->webformVariant
->getWeight(),
];
$tabs = [
'advanced' => [
'title' => $this
->t('Advanced'),
'elements' => [
'advanced',
'additional',
'development',
],
'weight' => 20,
],
];
$form = WebformFormHelper::buildTabs($form, $tabs);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
];
$form['token_tree_link'] = $this->tokenManager
->buildTreeElement();
if ($form['token_tree_link']) {
$form['token_tree_link'] += [
'#weight' => 101,
];
}
return $this
->buildDialogForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
$this->webformVariant
->validateConfigurationForm($form, $subform_state);
$this
->processVariantFormErrors($subform_state, $form_state);
$form_state
->setValue('settings', $subform_state
->getValues());
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->cleanValues();
$subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
$this->webformVariant
->submitConfigurationForm($form, $subform_state);
$form_state
->setValue('settings', $subform_state
->getValues());
$this->webformVariant
->setVariantId($form_state
->getValue('variant_id'));
$this->webformVariant
->setLabel($form_state
->getValue('label'));
$this->webformVariant
->setNotes($form_state
->getValue('notes'));
$this->webformVariant
->setElementKey($form_state
->getValue('element_key'));
$this->webformVariant
->setStatus($form_state
->getValue('status'));
$this->webformVariant
->setWeight($form_state
->getValue('weight'));
if ($this instanceof WebformVariantAddForm) {
$this->webform
->addWebformVariant($this->webformVariant);
$this
->messenger()
->addStatus($this
->t('The webform variant was successfully added.'));
}
else {
$this->webform
->updateWebformVariant($this->webformVariant);
$this
->messenger()
->addStatus($this
->t('The webform variant was successfully updated.'));
}
$form_state
->setRedirectUrl($this->webform
->toUrl('variants', [
'query' => [
'update' => $this->webformVariant
->getVariantId(),
],
]));
}
public function exists($variant_id) {
$instance_ids = $this->webform
->getVariants()
->getInstanceIds();
return isset($instance_ids[$variant_id]) ? TRUE : FALSE;
}
public function getWebform() {
return $this->webform;
}
public function getWebformVariant() {
return $this->webformVariant;
}
protected function processVariantFormErrors(FormStateInterface $variant_state, FormStateInterface &$form_state) {
foreach ($variant_state
->getErrors() as $name => $message) {
$form_state
->setErrorByName($name, $message);
}
}
protected function getVariantElementsAsOptions() {
$webform = $this
->getWebform();
$variant_plugin_id = $this
->getWebformVariant()
->getPluginId();
$elements = $this
->getWebform()
->getElementsVariant();
$options = [];
foreach ($elements as $element_key) {
$element = $webform
->getElement($element_key);
if ($element['#variant'] === $variant_plugin_id) {
$options[$element_key] = WebformElementHelper::getAdminTitle($element);
}
}
return $options;
}
}