Skip to content

Commit

Permalink
Fixed closure serialization error when editing a webform element with…
Browse files Browse the repository at this point in the history
… ajax.
  • Loading branch information
sonnykt committed Jan 31, 2025
1 parent cac5cdf commit c1e52b2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/WebformFormAlterBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(
* @return \Drupal\webform\WebformInterface|null
* The webform.
*/
protected function getWebformFromFormState(FormStateInterface $form_state): ?WebformInterface {
protected static function getWebformFromFormState(FormStateInterface $form_state): ?WebformInterface {
$webform = NULL;
$form_object = $form_state->getFormObject();
if ($form_object instanceof EntityFormInterface) {
Expand Down
4 changes: 2 additions & 2 deletions src/WebformThirdPartySettingsFormAlter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class WebformThirdPartySettingsFormAlter extends WebformFormAlterBase {
* {@inheritdoc}
*/
public function alterForm(array &$form, FormStateInterface $form_state): void {
$webform = $this->getWebformFromFormState($form_state);
$webform = static::getWebformFromFormState($form_state);
if (!$webform instanceof WebformInterface) {
// @codeCoverageIgnoreStart
return;
Expand Down Expand Up @@ -167,7 +167,7 @@ public function alterForm(array &$form, FormStateInterface $form_state): void {
* Form state.
*/
public function validateForm(array $form, FormStateInterface $form_state): void {
$webform = $this->getWebformFromFormState($form_state);
$webform = static::getWebformFromFormState($form_state);
if (!$webform instanceof WebformInterface) {
// @codeCoverageIgnoreStart
return;
Expand Down
34 changes: 19 additions & 15 deletions src/WebformUiElementFormAlter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class WebformUiElementFormAlter extends WebformFormAlterBase {
* {@inheritdoc}
*/
public function alterForm(array &$form, FormStateInterface $form_state) : void {
$webform = $this->getWebformFromFormState($form_state);
$webform = static::getWebformFromFormState($form_state);
if (!$webform instanceof WebformInterface) {
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}

$element_key = $this->getWebformElementKey($form_state);
$element_key = static::getWebformElementKey($form_state);
if (empty($element_key)) {
// @codeCoverageIgnoreStart
return;
Expand Down Expand Up @@ -86,7 +86,9 @@ public function alterForm(array &$form, FormStateInterface $form_state) : void {
'#default_value' => $openfisca_settings->fieldHasImmediateResponse($element_key),
];

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

/**
Expand All @@ -97,15 +99,15 @@ public function alterForm(array &$form, FormStateInterface $form_state) : void {
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*/
public function submitForm(array &$form, FormStateInterface $form_state) : void {
$webform = $this->getWebformFromFormState($form_state);
public static function submitForm(array &$form, FormStateInterface $form_state) : void {
$webform = static::getWebformFromFormState($form_state);
if (!$webform instanceof WebformInterface) {
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}

$element_key = $this->getWebformElementKey($form_state);
$element_key = static::getWebformElementKey($form_state);
if (empty($element_key)) {
// @codeCoverageIgnoreStart
return;
Expand All @@ -128,14 +130,16 @@ public function submitForm(array &$form, FormStateInterface $form_state) : void
$fisca_variables = $openfisca_settings->getVariables();
$fisca_entity_roles = $openfisca_settings->getEntityRoles();

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

$fisca_machine_name = $form_state->getValue('fisca_machine_name');
$fisca_entity_key = $form_state->getValue('fisca_entity_key');
$fisca_entity_role = $form_state->getValue('fisca_entity_role');
$fisca_entity_role_array = (bool) $form_state->getValue('fisca_entity_role_array');
// Update OpenFisca variables and field mappings.
if (!array_key_exists($fisca_machine_name, $this->getGenericOpenFiscaVariables())) {
if (!array_key_exists($fisca_machine_name, static::getGenericOpenFiscaVariables())) {
// Get OpenFisca entities.
$fisca_entities = $openfisca_client->getEntities();
// Get OpenFisca variable.
Expand Down Expand Up @@ -204,7 +208,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) : void
}
// @codeCoverageIgnoreStart
catch (EntityStorageException $entity_storage_exception) {
$this->messenger->addError($entity_storage_exception->getMessage());
\Drupal::messenger()->addError($entity_storage_exception->getMessage());
}
// @codeCoverageIgnoreEnd
}
Expand All @@ -218,7 +222,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) : void
* @return string|null
* The key.
*/
protected function getWebformElementKey(FormStateInterface $form_state) : ?string {
protected static function getWebformElementKey(FormStateInterface $form_state) : ?string {
$form_object = $form_state->getFormObject();
return ($form_object instanceof WebformUiElementFormInterface) ? $form_object->getKey() : NULL;
}
Expand All @@ -242,7 +246,7 @@ protected function getOpenFiscaVariables(WebformOpenFiscaSettings $settings) : a
$openfisca_client = $settings->getOpenFiscaClient($this->openFiscaClientFactory);

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

$variables = $openfisca_client->getVariables();
if ($variables === NULL) {
Expand All @@ -264,11 +268,11 @@ protected function getOpenFiscaVariables(WebformOpenFiscaSettings $settings) : a
* @return array<string, \Drupal\Component\Render\MarkupInterface|string>
* The variables.
*/
protected function getGenericOpenFiscaVariables() : array {
protected static function getGenericOpenFiscaVariables() : array {
return [
'_nil' => $this->t('- Exclude from mapping -'),
'name_key' => $this->t('Name'),
'period' => $this->t('Period'),
'_nil' => t('- Exclude from mapping -'),
'name_key' => t('Name'),
'period' => t('Period'),
];
}

Expand Down

0 comments on commit c1e52b2

Please sign in to comment.