View source
<?php
namespace Drupal\webform\Plugin;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\webform\Utility\WebformDialogHelper;
use Drupal\webform\WebformInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WebformVariantBase extends PluginBase implements WebformVariantInterface {
use WebformEntityInjectionTrait;
use WebformPluginSettingsTrait;
protected $variant_id;
protected $element_key = '';
protected $label;
protected $notes = '';
protected $status = 1;
protected $weight = '';
protected $configFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'));
}
public function getSummary() {
return [
'#theme' => 'webform_variant_' . $this->pluginId . '_summary',
'#settings' => $this->configuration,
'#variant' => $this,
];
}
public function label() {
return $this
->getLabel();
}
public function description() {
return $this->pluginDefinition['description'];
}
public function getMachineNameReplacePattern() {
return $this->pluginDefinition['machine_name_replace_pattern'];
}
public function getMachineNameReplace() {
return $this->pluginDefinition['machine_name_replace'];
}
public function getVariantId() {
return $this->variant_id;
}
public function setVariantId($variant_id) {
$this->variant_id = $variant_id;
return $this;
}
public function getElementKey() {
return $this->element_key;
}
public function setElementKey($element_key) {
$this->element_key = $element_key;
return $this;
}
public function setLabel($label) {
$this->label = $label;
return $this;
}
public function getLabel() {
return $this->label;
}
public function setNotes($notes) {
$this->notes = $notes;
return $this;
}
public function getNotes() {
return $this->notes;
}
public function setStatus($status) {
$this->status = $status;
return $this;
}
public function getStatus() {
return $this->status;
}
public function setWeight($weight) {
$this->weight = $weight;
return $this;
}
public function getWeight() {
return $this->weight;
}
public function enable() {
return $this
->setStatus(TRUE);
}
public function disable() {
return $this
->setStatus(FALSE);
}
public function isExcluded() {
return $this->configFactory
->get('webform.settings')
->get('variant.excluded_variants.' . $this->pluginDefinition['id']) ? TRUE : FALSE;
}
public function isEnabled() {
return $this->status ? TRUE : FALSE;
}
public function isDisabled() {
return !$this
->isEnabled();
}
public function isApplicable(WebformInterface $webform) {
return TRUE;
}
public function getConfiguration() {
return [
'id' => $this
->getPluginId(),
'label' => $this
->getLabel(),
'notes' => $this
->getNotes(),
'variant_id' => $this
->getVariantId(),
'element_key' => $this
->getElementKey(),
'status' => $this
->getStatus(),
'weight' => $this
->getWeight(),
'settings' => $this->configuration,
];
}
public function setConfiguration(array $configuration) {
$configuration += [
'variant_id' => '',
'element_key' => '',
'label' => '',
'notes' => '',
'status' => 1,
'weight' => '',
'settings' => [],
];
$this->configuration = $configuration['settings'] + $this
->defaultConfiguration();
$this->variant_id = $configuration['variant_id'];
$this->element_key = $configuration['element_key'];
$this->label = $configuration['label'];
$this->notes = $configuration['notes'];
$this->status = $configuration['status'];
$this->weight = $configuration['weight'];
return $this;
}
public function defaultConfiguration() {
return [];
}
public function calculateDependencies() {
return [];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function getOffCanvasWidth() {
return WebformDialogHelper::DIALOG_NORMAL;
}
public function applyVariant() {
$webform = $this
->getWebform();
if (!$this
->isApplicable($webform)) {
return FALSE;
}
return TRUE;
}
}