Skip to content

Commit

Permalink
Fix missing protected form state properties (#124)
Browse files Browse the repository at this point in the history
I encountered this when working with views exposed filters.
  • Loading branch information
darrenoh authored Dec 9, 2023
1 parent daaaa1f commit cc7c019
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,6 @@ parameters:
count: 1
path: src/Plugin/Field/FieldType/DecoratedFieldItem.php

-
message: "#^Method Retrofit\\\\Drupal\\\\Plugin\\\\Field\\\\FieldType\\\\DecoratedFieldItem\\:\\:applyDefaultValue\\(\\) return type has no value type specified in iterable type Drupal\\\\Core\\\\Field\\\\FieldItemInterface\\.$#"
count: 1
path: src/Plugin/Field/FieldType/DecoratedFieldItem.php

-
message: "#^Method Retrofit\\\\Drupal\\\\Plugin\\\\Field\\\\FieldType\\\\DecoratedFieldItem\\:\\:calculateDependencies\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down
5 changes: 5 additions & 0 deletions src/Form/ArrayAccessFormState.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

final class ArrayAccessFormState extends FormState implements \ArrayAccess
{
public function __construct(bool $errors = false)
{
static::setAnyErrors($errors);
}

public function offsetExists(mixed $offset): bool
{
return isset($this->$offset);
Expand Down
45 changes: 41 additions & 4 deletions src/Form/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,47 @@ class FormBuilder extends CoreFormBuilder
*/
public function buildForm(mixed $form_arg, FormStateInterface|array &$form_state): array
{
$original_form_state = $form_state;
$form_state = new ArrayAccessFormState();
if ($original_form_state instanceof FormStateInterface) {
$original_form_state = $original_form_state->getCacheableArray() + get_object_vars($original_form_state);
if ($form_state instanceof FormStateInterface) {
$original_form_state = $form_state->getCacheableArray() + get_object_vars($form_state) + [
'always_process' => $form_state->getAlwaysProcess(),
'buttons' => $form_state->getButtons(),
'cleanValueKeys' => $form_state->getCleanValueKeys(),
'complete_form' => $form_state->getCompleteForm(),
'errors' => $form_state->getErrors(),
'executed' => $form_state->isExecuted(),
'groups' => $form_state->getGroups(),
'input' => $form_state->getUserInput(),
'invalidToken' => $form_state->hasInvalidToken(),
'limit_validation_errors' => $form_state->getLimitValidationErrors(),
'method' => match (true) {
$form_state->isMethodType('POST') => 'POST',
$form_state->isMethodType('GET') => 'GET',
$form_state->isMethodType('HEAD') => 'HEAD',
$form_state->isMethodType('OPTIONS') => 'OPTIONS',
$form_state->isMethodType('PUT') => 'PUT',
$form_state->isMethodType('DELETE') => 'DELETE',
$form_state->isMethodType('TRACE') => 'TRACE',
$form_state->isMethodType('CONNECT') => 'CONNECT',
$form_state->isMethodType('PATCH') => 'PATCH',
default => 'POST',
},
'must_validate' => $form_state->isValidationEnforced(),
'no_redirect' => $form_state->isRedirectDisabled(),
'rebuild' => $form_state->isRebuilding(),
'rebuild_info' => $form_state->getRebuildInfo(),
'redirect' => $form_state->getRedirect(),
'submitted' => $form_state->isSubmitted(),
'submit_handlers' => $form_state->getSubmitHandlers(),
'temporary' => $form_state->getTemporary(),
'triggering_element' => $form_state->getTriggeringElement(),
'validate_handlers' => $form_state->getValidateHandlers(),
'validation_complete' => $form_state->isValidationComplete(),
'values' => $form_state->getValues(),
];
$form_state = new ArrayAccessFormState(call_user_func([get_class($form_state), 'hasAnyErrors']));
} else {
$original_form_state = $form_state;
$form_state = new ArrayAccessFormState();
}
foreach ($original_form_state as $offset => $value) {
$form_state[$offset] = $value;
Expand Down
1 change: 0 additions & 1 deletion src/functions/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Drupal\Core\Form\FormStateInterface;
use Retrofit\Drupal\Form\DrupalGetForm;
use Retrofit\Drupal\Form\ArrayAccessFormState;

/**
* @param mixed[] $form_state
Expand Down

0 comments on commit cc7c019

Please sign in to comment.