Commit 52e07616 authored by jrockowitz's avatar jrockowitz Committed by jrockowitz

Issue #3000680 by jrockowitz: Create webform example handler module

parent 1b31c7fd
......@@ -68,6 +68,7 @@ echo 'true' > modules/webform_examples/webform_examples.features.yml
echo 'true' > modules/webform_examples_accessibility/webform_examples_accessibility.features.yml
echo 'true' > modules/webform_example_element/webform_example_element.features.yml
echo 'true' > modules/webform_example_composite/webform_example_composite.features.yml
echo 'true' > modules/webform_example_handler/webform_example_handler.features.yml
echo 'true' > modules/webform_example_element/webform_example_remote_post.features.yml
echo 'true' > modules/webform_templates/webform_templates.features.yml
......@@ -115,6 +116,7 @@ drush en -y webform\
webform_examples\
webform_examples_accessibility\
webform_example_element\
webform_example_handler\
webform_example_remote_post\
webform_image_select\
webform_node\
......@@ -145,6 +147,7 @@ drush features-export -y webform_examples
drush features-export -y webform_examples_accessibility
drush features-export -y webform_example_element
drush features-export -y webform_example_composite
drush features-export -y webform_example_handler
drush features-export -y webform_example_remote_post
drush features-export -y webform_node
drush features-export -y webform_image_select
......@@ -176,6 +179,7 @@ drush webform:tidy -y --dependencies webform_examples
drush webform:tidy -y --dependencies webform_examples_accessibility
drush webform:tidy -y --dependencies webform_example_element
drush webform:tidy -y --dependencies webform_example_composite
drush webform:tidy -y --dependencies webform_example_handler
drush webform:tidy -y --dependencies webform_example_remote_post
drush webform:tidy -y --dependencies webform_image_select
drush webform:tidy -y --dependencies webform_node
......@@ -204,6 +208,7 @@ drush features-import -y webform_examples
drush features-import -y webform_examples_accessibility
drush features-import -y webform_example_element
drush features-import -y webform_example_composite
drush features-import -y webform_example_handler
drush features-import -y webform_example_remote_post
drush features-import -y webform_node
drush features-import -y webform_image_select
......
uuid: ce0077fd-9eb1-4bb9-a270-e82713f8d2d3
langcode: en
status: open
dependencies:
enforced:
module:
- webform_example_handler
module:
- webform_example_handler
_core:
default_config_hash: tdCyBfZG8ZroUObP_Uc4R4mroXfqE2j2rY_pJd7ENT0
open: null
close: null
weight: 0
uid: null
template: false
archive: false
id: webform_example_handler
title: 'Example: Webform Handler'
description: 'An example of a custom Webform handler.'
category: Example
elements: |
value:
'#type': textfield
'#title': 'Value'
'#required': true
'#description': 'Enter a value to displayed in a custom message.'
css: ''
javascript: ''
settings:
ajax: false
ajax_scroll_top: form
page: true
page_submit_path: ''
page_confirm_path: ''
form_submit_once: false
form_exception_message: ''
form_open_message: ''
form_close_message: ''
form_previous_submissions: true
form_confidential: false
form_confidential_message: ''
form_convert_anonymous: false
form_prepopulate: false
form_prepopulate_source_entity: false
form_prepopulate_source_entity_required: false
form_prepopulate_source_entity_type: ''
form_reset: false
form_disable_autocomplete: false
form_novalidate: false
form_disable_inline_errors: false
form_required: false
form_unsaved: false
form_disable_back: false
form_submit_back: false
form_autofocus: false
form_details_toggle: false
form_access_denied: default
form_access_denied_title: ''
form_access_denied_message: ''
form_access_denied_attributes: { }
submission_label: ''
submission_log: false
submission_views: { }
submission_views_replace: { }
submission_user_columns: { }
submission_user_duplicate: false
submission_access_denied: default
submission_access_denied_title: ''
submission_access_denied_message: ''
submission_access_denied_attributes: { }
submission_exception_message: ''
submission_locked_message: ''
previous_submission_message: ''
previous_submissions_message: ''
autofill: false
autofill_message: ''
autofill_excluded_elements: { }
wizard_progress_bar: true
wizard_progress_pages: false
wizard_progress_percentage: false
wizard_progress_link: false
wizard_start_label: ''
wizard_preview_link: false
wizard_confirmation: true
wizard_confirmation_label: ''
wizard_track: ''
preview: 0
preview_label: ''
preview_title: ''
preview_message: ''
preview_attributes: { }
preview_excluded_elements: { }
preview_exclude_empty: true
preview_exclude_empty_checkbox: false
draft: none
draft_multiple: false
draft_auto_save: false
draft_saved_message: ''
draft_loaded_message: ''
confirmation_type: message
confirmation_title: ''
confirmation_message: ''
confirmation_url: ''
confirmation_attributes: { }
confirmation_back: true
confirmation_back_label: ''
confirmation_back_attributes: { }
confirmation_exclude_query: false
confirmation_exclude_token: false
limit_total: null
limit_total_interval: null
limit_total_message: ''
limit_user: null
limit_user_interval: null
limit_user_message: ''
entity_limit_total: null
entity_limit_total_interval: null
entity_limit_user: null
entity_limit_user_interval: null
purge: none
purge_days: null
results_disabled: false
results_disabled_ignore: false
token_update: false
access:
create:
roles:
- anonymous
- authenticated
users: { }
permissions: { }
view_any:
roles: { }
users: { }
permissions: { }
update_any:
roles: { }
users: { }
permissions: { }
delete_any:
roles: { }
users: { }
permissions: { }
purge_any:
roles: { }
users: { }
permissions: { }
view_own:
roles: { }
users: { }
permissions: { }
update_own:
roles: { }
users: { }
permissions: { }
delete_own:
roles: { }
users: { }
permissions: { }
administer:
roles: { }
users: { }
permissions: { }
test:
roles: { }
users: { }
permissions: { }
handlers:
example:
id: example
label: Example
handler_id: example
status: true
conditions: { }
weight: 0
settings:
message: 'You entered: <code>[webform_submission:values:value]</code>'
debug: true
webform.handler.example:
type: mapping
label: 'Example'
mapping:
message:
label: 'Message'
type: string
debug:
type: boolean
label: 'Enable debugging'
<?php
namespace Drupal\webform_example_handler\Plugin\WebformHandler;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Render\Markup;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionConditionsValidatorInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\WebformTokenManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Webform example handler.
*
* @WebformHandler(
* id = "example",
* label = @Translation("Example"),
* category = @Translation("Example"),
* description = @Translation("Example of a webform submission handler."),
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_IGNORED,
* submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
* )
*/
class ExampleWebformHandler extends WebformHandlerBase {
/**
* The token manager.
*
* @var \Drupal\webform\WebformTokenManagerInterface
*/
protected $tokenManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, WebformSubmissionConditionsValidatorInterface $conditions_validator, WebformTokenManagerInterface $token_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger_factory, $config_factory, $entity_type_manager, $conditions_validator);
$this->tokenManager = $token_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('logger.factory'),
$container->get('config.factory'),
$container->get('entity_type.manager'),
$container->get('webform_submission.conditions_validator'),
$container->get('webform.token_manager')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'message' => 'This is a custom message.',
'debug' => FALSE,
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// Message.
$form['message'] = [
'#type' => 'fieldset',
'#title' => $this->t('Message settings'),
];
$form['message']['message'] = [
'#type' => 'textfield',
'#title' => $this->t('Message to be displayed when form is completed'),
'#default_value' => $this->configuration['message'],
'#parents' => ['settings', 'message'],
'#required' => TRUE,
];
// 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, every handler method invoked will be displayed onscreen to all users.'),
'#return_value' => TRUE,
'#parents' => ['settings', 'debug'],
'#default_value' => $this->configuration['debug'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['message'] = $form_state->getValue('message');
$this->configuration['debug'] = (bool) $form_state->getValue('debug');
}
/**
* {@inheritdoc}
*/
public function alterElements(array &$elements, WebformInterface $webform) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function overrideSettings(array &$settings, WebformSubmissionInterface $webform_submission) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function alterForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$this->displayMessage(__FUNCTION__);
if ($value = $form_state->getValue('element')) {
$form_state->setErrorByName('element', $this->t('The element must be empty. You entered %value.', ['%value' => $value]));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function confirmForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$message = $this->configuration['message'];
$message = $this->tokenManager->replace($message, $this->getWebformSubmission());
$this->messenger()->addStatus(Markup::create($message), FALSE);
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function preCreate(array $values) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function postCreate(WebformSubmissionInterface $webform_submission) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function postLoad(WebformSubmissionInterface $webform_submission) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function preDelete(WebformSubmissionInterface $webform_submission) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function postDelete(WebformSubmissionInterface $webform_submission) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function preSave(WebformSubmissionInterface $webform_submission) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
$this->displayMessage(__FUNCTION__, $update ? 'update' : 'insert');
}
/**
* {@inheritdoc}
*/
public function preprocessConfirmation(array &$variables) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function createHandler() {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function updateHandler() {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function deleteHandler() {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function createElement($key, array $element) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function updateElement($key, array $element, array $original_element) {
$this->displayMessage(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function deleteElement($key, array $element) {
$this->displayMessage(__FUNCTION__);
}
/**
* Display the invoked plugin method to end user.
*
* @param string $method_name
* The invoked method name.
* @param string $context1
* Additional parameter passed to the invoked method name.
*/
protected function displayMessage($method_name, $context1 = NULL) {
if (!empty($this->configuration['debug'])) {
$t_args = [
'@id' => $this->getHandlerId(),
'@class_name' => get_class($this),
'@method_name' => $method_name,
'@context1' => $context1,
];
$this->messenger()->addWarning($this->t('Invoked @id: @class_name:@method_name @context1', $t_args), TRUE);
}
}
}
{#
/**
* @file
* Default theme implementation for a summary of an example webform handler.
*
* Available variables:
* - settings: The current configuration for this email handler:
* - message: The message.
* - debug: Debugging flag.
* - handler: The handler information, including:
* - id: The handler plugin id.
* - handler_id: The handler id.
* - label: The handler label.
* - description: The handler description.
*
* @ingroup themeable
*/
#}
{% if settings.debug %}<b class="color-error">{{ 'Debugging is enabled'|t }}</b><br />{% endif %}
<b>Message:</b> {{ settings.message }}
name: 'Webform Handler Example'
type: module
description: 'Provides an example of a webform handler.'
package: 'Webform example'
core: 8.x
dependencies:
- 'webform:webform'
<?php
/**
* @file
* Provides an example of a webform handler.
*/
/**
* Implements hook_theme().
*/
function webform_example_handler_theme() {
return [
'webform_handler_example_summary' => [
'variables' => ['settings' => NULL, 'handler' => []],
],
];
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment