I've created a simple form in Drupal 8. When I create a route towards it, it's working fine. The values I submit are dumped in the submitForm function, but as soon as I render the form within a block the $form_state variable is always empty in submitForm.
I've done this a hundred times and it works on every site, but not on this Drupal 8.9 install, so I am wondering if something might have changed.
Here is my form:
<?php
/**
* @file
* Contains \Drupal\example_filters\Form\FilterForm.
*/
namespace Drupal\example_filters\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
class FilterForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'example_filter_form';
}
/**
* Build the form
*
* @param array $form
* @param FormStateInterface $form_state
* @return array
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#tree'] = FALSE;
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);
$form['city'] = array(
'#type' => 'textfield',
'#title' => t('City'),
);
$form['#cache'] = [
'max-age' => 0
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Filter'),
];
return $form;
}
/**
* Submit handler
*
* @param array $form
* @param FormStateInterface $form_state
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
dump($values);
}
}
and my block code:
<?php
/**
* @file
* Contains \Drupal\example_filters\Plugin\Block\FilterBlock.
*/
namespace Drupal\example_filters\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Filter' block.
*
* @Block(
* id = "search_filter_block",
* admin_label = @Translation("Filter block")
* )
*/
class FilterBlock extends BlockBase {
public function build() {
return \Drupal::formBuilder()->getForm('Drupal\example_filters\Form\FilterForm');
}
}