WebformVariantAddForm.php in Webform 8.5
File
src/Form/WebformVariantAddForm.php
View source
<?php
namespace Drupal\webform\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformVariantManagerInterface;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformTokenManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class WebformVariantAddForm extends WebformVariantFormBase {
protected $webformVariantManager;
public function __construct(WebformTokenManagerInterface $token_manager, WebformVariantManagerInterface $webform_variant) {
parent::__construct($token_manager);
$this->webformVariantManager = $webform_variant;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('webform.token_manager'), $container
->get('plugin.manager.webform.variant'));
}
public function buildForm(array $form, FormStateInterface $form_state, WebformInterface $webform = NULL, $webform_variant = NULL) {
$form = parent::buildForm($form, $form_state, $webform, $webform_variant);
if ($this->webformVariant
->isExcluded()) {
throw new AccessDeniedHttpException();
}
$form['#title'] = $this
->t('Add @label variant', [
'@label' => $this->webformVariant
->label(),
]);
return $form;
}
protected function prepareWebformVariant($webform_variant) {
$webform_variant = $this->webformVariantManager
->createInstance($webform_variant);
$webform_variant
->setWebform($this->webform);
$variants = $this->webform
->getVariants();
$weight = 0;
foreach ($variants as $variant) {
if ($weight < $variant
->getWeight()) {
$weight = $variant
->getWeight() + 1;
}
}
$webform_variant
->setWeight($weight);
return $webform_variant;
}
}