class OverrideWebformVariant in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Plugin/WebformVariant/OverrideWebformVariant.php \Drupal\webform\Plugin\WebformVariant\OverrideWebformVariant
Webform override variant.
Plugin annotation
@WebformVariant(
id = "override",
label = @Translation("Override"),
category = @Translation("Override"),
description = @Translation("Override a webform's settings, elements, and handlers."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\webform\Plugin\WebformVariantBase implements WebformVariantInterface uses WebformEntityInjectionTrait, WebformPluginSettingsTrait
- class \Drupal\webform\Plugin\WebformVariant\OverrideWebformVariant
- class \Drupal\webform\Plugin\WebformVariantBase implements WebformVariantInterface uses WebformEntityInjectionTrait, WebformPluginSettingsTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of OverrideWebformVariant
File
- src/
Plugin/ WebformVariant/ OverrideWebformVariant.php, line 23
Namespace
Drupal\webform\Plugin\WebformVariantView source
class OverrideWebformVariant extends WebformVariantBase {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->currentUser = $container
->get('current_user');
return $instance;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'settings' => [],
'elements' => '',
'handlers' => [],
'debug' => FALSE,
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$webform = $this
->getWebform();
$form['overrides'] = [
'#type' => 'details',
'#title' => $this
->t('Overrides'),
'#open' => TRUE,
'#access' => $this->currentUser
->hasPermission('edit webform source'),
];
// Settings.
$form['overrides']['settings'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t('Settings (YAML)'),
'#description' => $this
->t('Enter the setting name and value as YAML.'),
'#more_title' => $this
->t('Default settings'),
'#more' => [
'#theme' => 'webform_codemirror',
'#type' => 'yaml',
'#code' => WebformYaml::encode($webform
->getSettings()),
],
'#parents' => [
'settings',
'settings',
],
'#default_value' => $this->configuration['settings'],
];
// Elements.
$form['overrides']['elements'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t('Elements (YAML)'),
'#description' => $this
->t('Enter the element name and properties as YAML.'),
'#more_title' => $this
->t('Default elements'),
'#more' => [
'#theme' => 'webform_codemirror',
'#type' => 'yaml',
'#code' => WebformYaml::encode($webform
->getElementsDecodedAndFlattened()),
],
'#parents' => [
'settings',
'elements',
],
'#default_value' => $this->configuration['elements'],
];
// Handlers.
$handlers = $webform
->get('handlers');
foreach ($handlers as &$handler) {
unset($handler['id'], $handler['handler_id']);
}
$form['overrides']['handlers'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t('Handlers (YAML)'),
'#description' => $this
->t('Enter the handler id and settings as YAML.'),
'#more_title' => $this
->t('Default handlers'),
'#more' => [
'#theme' => 'webform_codemirror',
'#type' => 'yaml',
'#code' => WebformYaml::encode($handlers),
],
'#parents' => [
'settings',
'handlers',
],
'#default_value' => $this->configuration['handlers'],
];
// Development.
$form['development'] = [
'#type' => 'details',
'#title' => $this
->t('Development settings'),
];
$form['development']['debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable debugging'),
'#description' => $this
->t('If checked, settings will be displayed onscreen to all users.'),
'#return_value' => TRUE,
'#parents' => [
'settings',
'debug',
],
'#default_value' => $this->configuration['debug'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$webform = $this
->getWebform();
$values = $form_state
->getValues();
// Validate settings names.
$settings = $webform
->getSettings();
foreach ($values['settings'] as $setting_name => $setting_value) {
if (!isset($settings[$setting_name])) {
$form_state
->setErrorByName('settings', $this
->t('Setting %name is not a valid setting name.', [
'%name' => $setting_name,
]));
}
}
// Validate element keys.
$elements = Yaml::decode($values['elements']) ?: [];
if ($elements) {
foreach ($elements as $element_key => $element_properties) {
// Skip custom form property.
if (WebformElementHelper::property($element_key)) {
continue;
}
$element = $webform
->getElement($element_key);
if (!$element) {
$form_state
->setErrorByName('elements', $this
->t('Element %key is not a valid element key.', [
'%key' => $element_key,
]));
}
}
}
// Validate handler ids.
foreach ($values['handlers'] as $handler_id => $handler_configuration) {
if (!$webform
->getHandlers()
->has($handler_id)) {
$form_state
->setErrorByName('handlers', $this
->t('Handler %id is not a valid handler id.', [
'%id' => $handler_id,
]));
}
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration = $form_state
->getValues();
$this->configuration['debug'] = (bool) $this->configuration['debug'];
}
/**
* {@inheritdoc}
*/
public function applyVariant() {
$webform = $this
->getWebform();
// Override settings.
if ($this->configuration['settings']) {
$settings = $webform
->getSettings();
foreach ($this->configuration['settings'] as $setting_name => $setting_value) {
if (isset($settings[$setting_name])) {
$settings[$setting_name] = $setting_value;
}
}
$webform
->setSettings($settings);
}
// Override elements.
$elements = Yaml::decode($this->configuration['elements']) ?: [];
if ($elements) {
foreach ($elements as $element_key => $element_properties) {
if (WebformElementHelper::property($element_key)) {
// Set custom form property.
$webform
->setElements([
$element_key => $element_properties,
] + $webform
->getElementsDecoded());
}
else {
$element = $webform
->getElement($element_key);
if (!$element) {
continue;
}
$webform
->setElementProperties($element_key, $element_properties + $element);
}
}
}
// Override handlers.
if ($this->configuration['handlers']) {
foreach ($this->configuration['handlers'] as $handler_id => $handler_configuration) {
if (!$webform
->getHandlers()
->has($handler_id)) {
continue;
}
$handler = $webform
->getHandler($handler_id);
$configuration = $handler
->getConfiguration();
foreach ($handler_configuration as $configuration_key => $configuration_value) {
if (!isset($configuration[$configuration_key])) {
continue;
}
if ($configuration_key === 'settings') {
$configuration[$configuration_key] = $configuration_value + $configuration[$configuration_key];
}
else {
$configuration[$configuration_key] = $configuration_value;
}
}
$handler
->setConfiguration($configuration);
}
}
// Debug.
$this
->debug();
return TRUE;
}
/****************************************************************************/
// Debug and exception handlers.
/****************************************************************************/
/**
* Display debugging information.
*/
protected function debug() {
if (empty($this->configuration['debug'])) {
return;
}
$build = [
'#type' => 'details',
'#title' => $this
->t('Debug: Override: @title', [
'@title' => $this
->label(),
]),
];
// Notes.
if ($notes = $this
->getNotes()) {
$build['notes'] = [
'#type' => 'item',
'#title' => $this
->t('Notes'),
'notes' => WebformHtmlEditor::checkMarkup($notes),
];
}
// Settings.
if ($this->configuration['settings']) {
$build['settings'] = [
'#type' => 'item',
'#title' => $this
->t('Settings'),
'yaml' => [
'#theme' => 'webform_codemirror',
'#type' => 'yaml',
'#code' => WebformYaml::encode($this->configuration['settings']),
],
];
}
// Elements.
if ($this->configuration['elements']) {
$build['elements'] = [
'#type' => 'item',
'#title' => $this
->t('Elements'),
'yaml' => [
'#theme' => 'webform_codemirror',
'#type' => 'yaml',
'#code' => $this->configuration['elements'],
],
];
}
// Handlers.
if ($this->configuration['handlers']) {
$build['handlers'] = [
'#type' => 'item',
'#title' => $this
->t('Handlers'),
'yaml' => [
'#theme' => 'webform_codemirror',
'#type' => 'yaml',
'#code' => WebformYaml::encode($this->configuration['handlers']),
],
];
}
$this
->messenger()
->addWarning(\Drupal::service('renderer')
->renderPlain($build));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OverrideWebformVariant:: |
protected | property | The current user. | |
OverrideWebformVariant:: |
public | function |
Apply variant to the webform. Overrides WebformVariantBase:: |
|
OverrideWebformVariant:: |
public | function |
Form constructor. Overrides WebformVariantBase:: |
|
OverrideWebformVariant:: |
public static | function |
IMPORTANT:
Webform handlers are initialized and serialized when they are attached to a
webform. Make sure not include any services as a dependency injection
that directly connect to the database. This will prevent
"LogicException: The database… Overrides WebformVariantBase:: |
|
OverrideWebformVariant:: |
protected | function | Display debugging information. | |
OverrideWebformVariant:: |
public | function |
Gets default configuration for this plugin. Overrides WebformVariantBase:: |
|
OverrideWebformVariant:: |
public | function |
Form submission handler. Overrides WebformVariantBase:: |
|
OverrideWebformVariant:: |
public | function |
Form validation handler. Overrides WebformVariantBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
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. | |
WebformEntityInjectionTrait:: |
protected | property | The webform. | 1 |
WebformEntityInjectionTrait:: |
protected | property | The webform submission. | 1 |
WebformEntityInjectionTrait:: |
public | function | Get the webform that this handler is attached to. | |
WebformEntityInjectionTrait:: |
public | function | Set webform and webform submission entity. | |
WebformEntityInjectionTrait:: |
public | function | Reset webform and webform submission entity. | |
WebformEntityInjectionTrait:: |
public | function | ||
WebformEntityInjectionTrait:: |
public | function | Set the webform that this is handler is attached to. | |
WebformEntityInjectionTrait:: |
public | function | Get the webform submission that this handler is handling. | |
WebformPluginSettingsTrait:: |
public | function | ||
WebformPluginSettingsTrait:: |
public | function | ||
WebformPluginSettingsTrait:: |
public | function | ||
WebformPluginSettingsTrait:: |
public | function | ||
WebformVariantBase:: |
protected | property | The configuration factory. | |
WebformVariantBase:: |
protected | property | The element key of the webform variant. | |
WebformVariantBase:: |
protected | property | The webform variant label. | |
WebformVariantBase:: |
protected | property | The webform variant notes. | |
WebformVariantBase:: |
protected | property | The webform variant status. | |
WebformVariantBase:: |
protected | property | The webform variant ID. | |
WebformVariantBase:: |
protected | property | The weight of the webform variant. | |
WebformVariantBase:: |
public | function | ||
WebformVariantBase:: |
public | function |
Returns the webform variant description. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Disables the webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Enables the webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
WebformVariantBase:: |
public | function |
Returns the element key of the webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Returns the label of the webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Returns the webform variant machine name replacement character. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Returns the webform variant machine name replacement pattern. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Returns notes of the webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Get configuration form's off-canvas width. Overrides WebformVariantInterface:: |
1 |
WebformVariantBase:: |
public | function |
Returns the status of the webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Returns a render array summarizing the configuration of the webform variant. Overrides WebformVariantInterface:: |
1 |
WebformVariantBase:: |
public | function |
Returns the unique ID representing the webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Returns the weight of the webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Determine if this variant is applicable to the webform. Overrides WebformVariantInterface:: |
2 |
WebformVariantBase:: |
public | function |
Returns the webform variant disabled indicator. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Returns the webform variant enabled indicator. Overrides WebformVariantInterface:: |
1 |
WebformVariantBase:: |
public | function |
Checks if the variant is excluded via webform.settings. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Returns the webform variant label. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
WebformVariantBase:: |
public | function |
Sets the element key of this webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Sets the label for this webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Set notes for this webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Sets the status for this webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Sets the id for this webform variant. Overrides WebformVariantInterface:: |
|
WebformVariantBase:: |
public | function |
Sets the weight for this webform variant. Overrides WebformVariantInterface:: |