Skip to content

Commit c1e52b2

Browse files
committed
Fixed closure serialization error when editing a webform element with ajax.
1 parent cac5cdf commit c1e52b2

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

src/WebformFormAlterBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(
5151
* @return \Drupal\webform\WebformInterface|null
5252
* The webform.
5353
*/
54-
protected function getWebformFromFormState(FormStateInterface $form_state): ?WebformInterface {
54+
protected static function getWebformFromFormState(FormStateInterface $form_state): ?WebformInterface {
5555
$webform = NULL;
5656
$form_object = $form_state->getFormObject();
5757
if ($form_object instanceof EntityFormInterface) {

src/WebformThirdPartySettingsFormAlter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class WebformThirdPartySettingsFormAlter extends WebformFormAlterBase {
1919
* {@inheritdoc}
2020
*/
2121
public function alterForm(array &$form, FormStateInterface $form_state): void {
22-
$webform = $this->getWebformFromFormState($form_state);
22+
$webform = static::getWebformFromFormState($form_state);
2323
if (!$webform instanceof WebformInterface) {
2424
// @codeCoverageIgnoreStart
2525
return;
@@ -167,7 +167,7 @@ public function alterForm(array &$form, FormStateInterface $form_state): void {
167167
* Form state.
168168
*/
169169
public function validateForm(array $form, FormStateInterface $form_state): void {
170-
$webform = $this->getWebformFromFormState($form_state);
170+
$webform = static::getWebformFromFormState($form_state);
171171
if (!$webform instanceof WebformInterface) {
172172
// @codeCoverageIgnoreStart
173173
return;

src/WebformUiElementFormAlter.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ class WebformUiElementFormAlter extends WebformFormAlterBase {
1919
* {@inheritdoc}
2020
*/
2121
public function alterForm(array &$form, FormStateInterface $form_state) : void {
22-
$webform = $this->getWebformFromFormState($form_state);
22+
$webform = static::getWebformFromFormState($form_state);
2323
if (!$webform instanceof WebformInterface) {
2424
// @codeCoverageIgnoreStart
2525
return;
2626
// @codeCoverageIgnoreEnd
2727
}
2828

29-
$element_key = $this->getWebformElementKey($form_state);
29+
$element_key = static::getWebformElementKey($form_state);
3030
if (empty($element_key)) {
3131
// @codeCoverageIgnoreStart
3232
return;
@@ -86,7 +86,9 @@ public function alterForm(array &$form, FormStateInterface $form_state) : void {
8686
'#default_value' => $openfisca_settings->fieldHasImmediateResponse($element_key),
8787
];
8888

89-
$form['#submit'][] = [$this, 'submitForm'];
89+
// Must use static callback here to avoid Closure serialization error upon
90+
// Ajax calls when editing a webform element.
91+
$form['#submit'][] = [static::class, 'submitForm'];
9092
}
9193

9294
/**
@@ -97,15 +99,15 @@ public function alterForm(array &$form, FormStateInterface $form_state) : void {
9799
* @param \Drupal\Core\Form\FormStateInterface $form_state
98100
* Form state.
99101
*/
100-
public function submitForm(array &$form, FormStateInterface $form_state) : void {
101-
$webform = $this->getWebformFromFormState($form_state);
102+
public static function submitForm(array &$form, FormStateInterface $form_state) : void {
103+
$webform = static::getWebformFromFormState($form_state);
102104
if (!$webform instanceof WebformInterface) {
103105
// @codeCoverageIgnoreStart
104106
return;
105107
// @codeCoverageIgnoreEnd
106108
}
107109

108-
$element_key = $this->getWebformElementKey($form_state);
110+
$element_key = static::getWebformElementKey($form_state);
109111
if (empty($element_key)) {
110112
// @codeCoverageIgnoreStart
111113
return;
@@ -128,14 +130,16 @@ public function submitForm(array &$form, FormStateInterface $form_state) : void
128130
$fisca_variables = $openfisca_settings->getVariables();
129131
$fisca_entity_roles = $openfisca_settings->getEntityRoles();
130132

131-
$openfisca_client = $openfisca_settings->getOpenFiscaClient($this->openFiscaClientFactory);
133+
/** @var \Drupal\webform_openfisca\OpenFisca\ClientFactory $openfisca_client_factory */
134+
$openfisca_client_factory = \Drupal::service('webform_openfisca.openfisca_client_factory');
135+
$openfisca_client = $openfisca_settings->getOpenFiscaClient($openfisca_client_factory);
132136

133137
$fisca_machine_name = $form_state->getValue('fisca_machine_name');
134138
$fisca_entity_key = $form_state->getValue('fisca_entity_key');
135139
$fisca_entity_role = $form_state->getValue('fisca_entity_role');
136140
$fisca_entity_role_array = (bool) $form_state->getValue('fisca_entity_role_array');
137141
// Update OpenFisca variables and field mappings.
138-
if (!array_key_exists($fisca_machine_name, $this->getGenericOpenFiscaVariables())) {
142+
if (!array_key_exists($fisca_machine_name, static::getGenericOpenFiscaVariables())) {
139143
// Get OpenFisca entities.
140144
$fisca_entities = $openfisca_client->getEntities();
141145
// Get OpenFisca variable.
@@ -204,7 +208,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) : void
204208
}
205209
// @codeCoverageIgnoreStart
206210
catch (EntityStorageException $entity_storage_exception) {
207-
$this->messenger->addError($entity_storage_exception->getMessage());
211+
\Drupal::messenger()->addError($entity_storage_exception->getMessage());
208212
}
209213
// @codeCoverageIgnoreEnd
210214
}
@@ -218,7 +222,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) : void
218222
* @return string|null
219223
* The key.
220224
*/
221-
protected function getWebformElementKey(FormStateInterface $form_state) : ?string {
225+
protected static function getWebformElementKey(FormStateInterface $form_state) : ?string {
222226
$form_object = $form_state->getFormObject();
223227
return ($form_object instanceof WebformUiElementFormInterface) ? $form_object->getKey() : NULL;
224228
}
@@ -242,7 +246,7 @@ protected function getOpenFiscaVariables(WebformOpenFiscaSettings $settings) : a
242246
$openfisca_client = $settings->getOpenFiscaClient($this->openFiscaClientFactory);
243247

244248
// Allow a generic key to be allocated to identify a person.
245-
$fisca_variables = $this->getGenericOpenFiscaVariables();
249+
$fisca_variables = static::getGenericOpenFiscaVariables();
246250

247251
$variables = $openfisca_client->getVariables();
248252
if ($variables === NULL) {
@@ -264,11 +268,11 @@ protected function getOpenFiscaVariables(WebformOpenFiscaSettings $settings) : a
264268
* @return array<string, \Drupal\Component\Render\MarkupInterface|string>
265269
* The variables.
266270
*/
267-
protected function getGenericOpenFiscaVariables() : array {
271+
protected static function getGenericOpenFiscaVariables() : array {
268272
return [
269-
'_nil' => $this->t('- Exclude from mapping -'),
270-
'name_key' => $this->t('Name'),
271-
'period' => $this->t('Period'),
273+
'_nil' => t('- Exclude from mapping -'),
274+
'name_key' => t('Name'),
275+
'period' => t('Period'),
272276
];
273277
}
274278

0 commit comments

Comments
 (0)