Skip to main content
when possible, keep the code on Drupal Answers; this prevents future link rot; also, there is no need to specify the timing of questions (e.g., holidays), as future readers may come at any time.
Source Link
Patrick Kenny
  • 29.4k
  • 19
  • 139
  • 316

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: https://pastebin.com/KShaLJZ4

<?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 https://pastebin.com/1AGGRZkR

Hope someone can help me out these holidays.:

Thanks!

<?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');
  }
}

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: https://pastebin.com/KShaLJZ4 and my block code https://pastebin.com/1AGGRZkR

Hope someone can help me out these holidays.

Thanks!

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');
  }
}
Source Link

Form values empty in submitForm function when form is in a block

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: https://pastebin.com/KShaLJZ4 and my block code https://pastebin.com/1AGGRZkR

Hope someone can help me out these holidays.

Thanks!