Skip to content
Snippets Groups Projects

Issue #3352946: Get forms are always validated

3 files
+ 77
2
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 60
0
 
<?php
 
 
declare(strict_types = 1);
 
 
namespace Drupal\ui_suite_bootstrap\HookHandler;
 
 
use Drupal\Core\Form\FormStateInterface;
 
use Drupal\Core\Render\Element;
 
 
/**
 
* Alter forms.
 
*/
 
class FormAlter {
 
 
/**
 
* Alter forms.
 
*
 
* @param array $form
 
* The form structure.
 
* @param \Drupal\Core\Form\FormStateInterface $formState
 
* The form state.
 
* @param string $form_id
 
* The form ID.
 
*/
 
public function alter(array &$form, FormStateInterface $formState, string $form_id): void {
 
// See \Drupal\Core\Form\FormBuilder::processForm().
 
if ($formState->isMethodType('get') && $formState->getAlwaysProcess()) {
 
$form['#after_build'][] = [
 
static::class,
 
'afterBuildAddGetFormMethod',
 
];
 
}
 
}
 
 
/**
 
* Form element #after_build callback.
 
*/
 
public static function afterBuildAddGetFormMethod(array $element, FormStateInterface $form_state): array {
 
static::addGetFormMethod($element);
 
return $element;
 
}
 
 
/**
 
* Set form method to all form elements.
 
*
 
* To allow other processing to act based on this information.
 
*
 
* @param array $form
 
* The form or form element which children should have form method attached.
 
*/
 
protected static function addGetFormMethod(array &$form): void {
 
foreach (Element::children($form) as $child) {
 
if (!isset($form[$child]['#form_method'])) {
 
$form[$child]['#form_method'] = 'get';
 
}
 
static::addGetFormMethod($form[$child]);
 
}
 
}
 
 
}
Loading