-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebformHelper.php
More file actions
213 lines (189 loc) · 6.77 KB
/
WebformHelper.php
File metadata and controls
213 lines (189 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Drupal\os2forms_sync\Helper;
use Drupal\Component\Utility\Random;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\webform\Entity\Webform;
use Drupal\webform\WebformEntityStorageInterface;
use Drupal\webform\WebformInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* The webform helper.
*/
final class WebformHelper {
use StringTranslationTrait;
/**
* The webform entity storage.
*
* @var \Drupal\webform\WebformEntityStorageInterface
*/
private WebformEntityStorageInterface $webformEntityStorage;
/**
* The import helper.
*
* @var ImportHelper
*/
private ImportHelper $importHelper;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
private RequestStack $requestStack;
/**
* The constructor.
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
ImportHelper $importHelper,
RequestStack $requestStack,
) {
$this->webformEntityStorage = $entityTypeManager->getStorage('webform');
$this->importHelper = $importHelper;
$this->requestStack = $requestStack;
}
/**
* Load webform.
*/
public function loadWebform(string $id): ?WebformInterface {
return $this->webformEntityStorage->load($id);
}
/**
* Load published webforms.
*
* @return \Drupal\webform\Entity\WebformInterface[]|array
* The webforms.
*
* @phpstan-return array<WebformInterface>
*/
public function loadPublishedWebforms(): array {
return array_values(array_filter(
$this->webformEntityStorage->loadMultiple(),
[$this, 'webformIsPublished']
));
}
/**
* Decide if webform is published.
*/
public function webformIsPublished(WebformInterface $webform): bool {
return (bool) ($webform->getThirdPartySetting('os2forms', 'os2forms_sync')['publish'] ?? FALSE);
}
/**
* Implements hook_webform_third_party_settings_form_alter().
*
* @phpstan-param array<string, mixed> $form
*/
public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterface $form_state): void {
/** @var \Drupal\Core\Entity\EntityForm $formObject */
$formObject = $form_state->getFormObject();
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $formObject->getEntity();
$defaultValues = $webform->getThirdPartySetting('os2forms', 'os2forms_sync');
$form['third_party_settings']['os2forms']['os2forms_sync'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this->t('OS2Forms sync'),
'#tree' => TRUE,
];
$form['third_party_settings']['os2forms']['os2forms_sync']['publish'] = [
'#type' => 'checkbox',
'#title' => $this->t('Publish'),
'#default_value' => (bool) ($defaultValues['publish'] ?? FALSE),
'#description' => $this->t('If checked this form will be listed on <a href=":url_index">:url_index</a>. Share this url with others that may import all public webforms on this site.', [
':url_index' => Url::fromRoute('os2forms_sync.jsonapi.webform.index')->setAbsolute()->toString(),
]),
];
if ($info = $this->importHelper->loadImportedWebform($webform)) {
$form['third_party_settings']['os2forms']['os2forms_sync']['message'] = [
'#prefix' => '<div>',
'#suffix' => '</div>',
'#markup' => $this->t('Webform updated from <a href=":url">:url</a> at @updated_at.', [
':url' => $info->sourceUrl,
'@updated_at' => $info->updatedAt->format(DrupalDateTime::FORMAT),
]),
];
$form['third_party_settings']['os2forms']['os2forms_sync']['update_interval'] = [
'#type' => 'select',
'#title' => $this->t('Update'),
'#options' => [
60 * 60 => $this->t('hourly'),
24 * 60 * 60 => $this->t('daily'),
7 * 24 * 60 * 60 => $this->t('weekly'),
30 * 24 * 60 * 60 => $this->t('every 30 days'),
],
'#empty_value' => 0,
'#empty_option' => $this->t('manually'),
'#default_value' => (bool) ($defaultValues['update_interval'] ?? 0),
];
$form['third_party_settings']['os2forms']['os2forms_sync']['update_now'] = [
'#prefix' => '<div>',
'#suffix' => '</div>',
'#type' => 'button',
'#value' => $this->t('Update webform now'),
'#attributes' => [
'formmethod' => 'post',
'formaction' => Url::fromRoute('os2forms_sync.webform.import', [
'url' => $info->sourceUrl,
'referer' => Url::fromUri($this->requestStack->getCurrentRequest()->getUri(), ['fragment' => 'edit-third-party-settings-os2forms-os2forms-sync'])->toString(TRUE)->getGeneratedUrl(),
])->toString(TRUE)->getGeneratedUrl(),
],
];
}
}
/**
* Implements hook_theme().
*
* @phpstan-param array<string, mixed> $existing
* @phpstan-return array<string, mixed>
*/
public function theme(array $existing, string $type, string $theme, string $path): array {
return [
'os2forms_sync_webform_index' => [
'variables' => [
'webforms' => NULL,
'settings_url' => NULL,
],
],
];
}
/**
* Get submission form render array from webform elements.
*
* @phpstan-param array<string, mixed> $elements
* @phpstan-return array<string, mixed>
*/
public function getSubmissionForm(array $elements): array {
// Webforms that are rendered here are not a part of the database yet.
// As a consequence, any element that attempts to load the webform will be
// unable to do so.
// Wizard pages attempt to do so in WebformWizardPage::showPage, so we
// display these as 'details' instead and indicate that they originally
// are pages in their titles.
$isFirst = TRUE;
foreach ($elements as &$element) {
if (($element['#type'] ?? NULL) === 'webform_wizard_page') {
if ($isFirst) {
$element['#open'] = TRUE;
$isFirst = FALSE;
}
$element['#type'] = 'details';
$element['#title'] = (string) $this->t('%title% (wizard page)', ['%title%' => $element['#title']]);
}
}
$webform = Webform::create([
'id' => (new Random())->name(32),
'elements' => Yaml::encode($elements),
]);
// Hack: Needed to prevent an error in the webform module:
// Warning: array_intersect_key(): Expected parameter 2 to be an array, null
// given in Drupal\webform\Entity\Webform->invokeHandlers() ….
$prop = new \ReflectionProperty($webform, 'settingsOriginal');
$prop->setAccessible(TRUE);
$prop->setValue($webform, []);
return $webform->getSubmissionForm();
}
}