Commit 0f0302f1 authored by gambry's avatar gambry Committed by Manuel Garcia

Issue #2943469 by gambry: "preSave" elements and handlers called twice

parent c62f0bc9
......@@ -151,8 +151,6 @@ class WebformEncryptSubmissionStorage extends WebformSubmissionStorage {
$encrypted_data = $this->encryptElements($data_original, $webform);
$entity->setData($encrypted_data);
$this->invokeWebformElements('preSave', $entity);
$this->invokeWebformHandlers('preSave', $entity);
return $id;
}
......
......@@ -125,4 +125,12 @@ access:
roles: { }
users: { }
permissions: { }
handlers: { }
handlers:
test_handler:
id: text_webform_handler
label: 'Test Handler'
handler_id: test_handler
status: true
conditions: { }
weight: 0
settings: { }
<?php
namespace Drupal\webform_encrypt_test\Plugin\WebformHandler;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionConditionsValidatorInterface;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Webform submission action handler.
*
* @WebformHandler(
* id = "text_webform_handler",
* label = @Translation("Test Handler"),
* category = @Translation("Test"),
* description = @Translation("A Test webform handler."),
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_IGNORED,
* submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_OPTIONAL,
* )
*/
class TestWebformHandler extends WebformHandlerBase {
/**
* The State system service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, WebformSubmissionConditionsValidatorInterface $conditions_validator, StateInterface $state) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger_factory, $config_factory, $entity_type_manager, $conditions_validator);
$this->state = $state;
}
/**
* {@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('state')
);
}
/**
* {@inheritdoc}
*/
public function preSave(WebformSubmissionInterface $webform_submission) {
$counter = $this->state->get('test_webform_handler_presave_counter', 0);
$this->state->set('test_webform_handler_presave_counter', ++$counter);
}
}
......@@ -122,4 +122,48 @@ class WebformEncryptSubmissionStorageTest extends KernelTestBase {
}
/**
* Tests 'preSave' handlers are called only once.
*/
public function testPreSaveHandlersAreCalledOnce() {
$this->assertEquals(NULL, $this->container->get('state')->get('test_webform_handler_counter'));
$webform = Webform::load('test_encryption');
$values = [
'id' => 'webform_submission_test',
'webform_id' => $webform->id(),
'data' => [
'test_text_field' => 'Test text field value',
'test_multiple_text_field' => [
0 => 'Test multiple text field value 1',
],
'test_text_area' => 'Test text area value',
'test_not_encrypted' => 'Test not encrypted value',
'test_address_field' => [
'address' => 'Test address field address',
'address_2' => 'Test address field address 2',
'city' => 'Test address field city',
'state_province' => 'California',
'postal_code' => 'AA11AA',
'country' => 'United Kingdom',
],
'test_multiple_address_field' => [
0 => [
'address' => 'Test multiple address field address',
'address_2' => 'Test multiple address field address 2',
'city' => 'Test multiple address field city',
'state_province' => 'California',
'postal_code' => 'AA11AA',
'country' => 'United Kingdom',
],
],
],
];
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = WebformSubmission::create($values);
$webform_submission->save();
$this->assertEquals(1, $this->container->get('state')->get('test_webform_handler_presave_counter'));
}
}
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