Skip to content

Commit 72d41f0

Browse files
authored
Merge pull request #105 from itk-dev/feature/webform_encrypt_revision
Enabled encrypt and revisions at the same time
2 parents 65e1cb2 + 89c89b9 commit 72d41f0

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ before starting to add changes. Use example [placed in the end of the page](#exa
1111

1212
## [Unreleased]
1313

14+
- Added new webform submission storage class to handle enable both encryption
15+
and revision at the same time.
16+
1417
## [3.15.0] 2024-05-03
1518

1619
- Added webform encryption modules
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* This module enabled webform submission encryption as a default option.
6+
*/
7+
8+
/**
9+
* Implements hook_install().
10+
*
11+
* We need to change the modules weight to ensure that our webform_submission
12+
* class is used and thereby overriding the webform_encrypt and webform_revision
13+
* classes.
14+
*
15+
* The class is set in os2forms_encrypt_entity_type_alter().
16+
*/
17+
function os2forms_encrypt_install() {
18+
module_set_weight('os2forms_encrypt', 9999);
19+
}

modules/os2forms_encrypt/os2forms_encrypt.module

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ function os2forms_encrypt_element_info_alter(array &$definitions): void {
2121
}
2222
}
2323
}
24+
25+
/**
26+
* Implements hook_entity_type_alter().
27+
*
28+
* Override webform submission class to enable encryption and revision.
29+
*/
30+
function os2forms_encrypt_entity_type_alter(array &$entity_types): void {
31+
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
32+
if (isset($entity_types['webform_submission'])) {
33+
$entity_types['webform_submission']->setStorageClass('Drupal\os2forms_encrypt\WebformOs2FormsEncryptSubmissionStorage');
34+
}
35+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_encrypt;
4+
5+
use Drupal\Core\Entity\EntityInterface;
6+
use Drupal\Core\Session\AccountInterface;
7+
use Drupal\webform\WebformInterface;
8+
use Drupal\webform_encrypt\WebformEncryptSubmissionStorage;
9+
use Drupal\webform_revisions\Controller\WebformRevisionsController;
10+
11+
/**
12+
* This class extension WebformEncryptSubmissionStorage.
13+
*
14+
* This is to enabled encryption and decryption for data and checks if webform
15+
* revisions are enabled and runs the same code (copied here as multiple
16+
* inherits is not a thing in PHP).
17+
*
18+
* So the getColumns is an moduleExists check and the rest is copied from
19+
* webform revision storage class.
20+
*/
21+
class WebformOs2FormsEncryptSubmissionStorage extends WebformEncryptSubmissionStorage {
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function getColumns(WebformInterface $webform = NULL, EntityInterface $source_entity = NULL, AccountInterface $account = NULL, $include_elements = TRUE) {
27+
if (!\Drupal::moduleHandler()->moduleExists('webform_revisions')) {
28+
return parent::getColumns($webform, $source_entity, $account, $include_elements);
29+
}
30+
else {
31+
$view_any = $webform && $webform->access('submission_view_any');
32+
33+
$columns = [];
34+
35+
// Serial number.
36+
$columns['serial'] = [
37+
'title' => $this->t('#'),
38+
];
39+
40+
// Submission ID.
41+
$columns['sid'] = [
42+
'title' => $this->t('SID'),
43+
];
44+
45+
// Submission label.
46+
$columns['label'] = [
47+
'title' => $this->t('Submission title'),
48+
'sort' => FALSE,
49+
];
50+
51+
// UUID.
52+
$columns['uuid'] = [
53+
'title' => $this->t('UUID'),
54+
];
55+
56+
// Draft.
57+
$columns['in_draft'] = [
58+
'title' => $this->t('In draft'),
59+
];
60+
61+
if (empty($account)) {
62+
// Sticky (Starred/Unstarred).
63+
$columns['sticky'] = [
64+
'title' => $this->t('Starred'),
65+
];
66+
67+
// Locked.
68+
$columns['locked'] = [
69+
'title' => $this->t('Locked'),
70+
];
71+
72+
// Notes.
73+
$columns['notes'] = [
74+
'title' => $this->t('Notes'),
75+
];
76+
}
77+
78+
// Created.
79+
$columns['created'] = [
80+
'title' => $this->t('Created'),
81+
];
82+
83+
// Completed.
84+
$columns['completed'] = [
85+
'title' => $this->t('Completed'),
86+
];
87+
88+
// Changed.
89+
$columns['changed'] = [
90+
'title' => $this->t('Changed'),
91+
];
92+
93+
// Source entity.
94+
if ($view_any && empty($source_entity)) {
95+
$columns['entity'] = [
96+
'title' => $this->t('Submitted to'),
97+
'sort' => FALSE,
98+
];
99+
}
100+
101+
// Submitted by.
102+
if (empty($account)) {
103+
$columns['uid'] = [
104+
'title' => $this->t('User'),
105+
];
106+
}
107+
108+
// Submission language.
109+
if ($view_any && \Drupal::moduleHandler()->moduleExists('language')) {
110+
$columns['langcode'] = [
111+
'title' => $this->t('Language'),
112+
];
113+
}
114+
115+
// Remote address.
116+
$columns['remote_addr'] = [
117+
'title' => $this->t('IP address'),
118+
];
119+
120+
// Webform and source entity for entity.webform_submission.collection.
121+
// @see /admin/structure/webform/submissions/manage
122+
if (empty($webform) && empty($source_entity)) {
123+
$columns['webform_id'] = [
124+
'title' => $this->t('Webform'),
125+
];
126+
$columns['entity'] = [
127+
'title' => $this->t('Submitted to'),
128+
'sort' => FALSE,
129+
];
130+
}
131+
132+
// Webform elements.
133+
if ($webform && $include_elements) {
134+
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
135+
$element_manager = \Drupal::service('plugin.manager.webform.element');
136+
$content_entity_id = $webform->getContentEntityID();
137+
$revision_ids = $this->database->query(
138+
'SELECT revision FROM {config_entity_revisions_revision} WHERE id = :id',
139+
[':id' => $content_entity_id]
140+
)->fetchCol();
141+
if (!$revision_ids) {
142+
return parent::getColumns($webform, $source_entity, $account, $include_elements);
143+
}
144+
145+
foreach ($revision_ids as $revision_id) {
146+
$revisionController = WebformRevisionsController::create(\Drupal::getContainer());
147+
$webform = $revisionController->loadConfigEntityRevision($revision_id, $webform->id());
148+
$elements = $webform->getElementsInitializedFlattenedAndHasValue('view');
149+
foreach ($elements as $element) {
150+
/** @var \Drupal\webform\Plugin\WebformElementInterface $element_plugin */
151+
$element_plugin = $element_manager->createInstance($element['#type']);
152+
// Replace tokens which can be used in an element's #title.
153+
$element_plugin->replaceTokens($element, $webform);
154+
$columns += $element_plugin->getTableColumn($element);
155+
}
156+
}
157+
}
158+
159+
// Operations.
160+
$columns['operations'] = [
161+
'title' => $this->t('Operations'),
162+
'sort' => FALSE,
163+
];
164+
165+
// Add name and format to all columns.
166+
foreach ($columns as $name => &$column) {
167+
$column['name'] = $name;
168+
$column['format'] = 'value';
169+
}
170+
171+
return $columns;
172+
}
173+
}
174+
175+
}

0 commit comments

Comments
 (0)