Problem/Motivation

While attempting to render a custom form inside of a block, I discovered that using \Drupal::formBuilder()->getForm() has varying results depending on "where" you call it from.

I originally was inserting it directly into my hook_theme() function in the .module file.

/**
 * Implements hook_theme().
 */
function my_theme($existing, $type, $theme, $path) {
  return [
    'my_block' =>
      [
        'render element' => 'form',
        'base hook' => 'form',
        'variables' => [
          'label' => NULL,
          'description' => NULL,
          'form' => \Drupal::formBuilder()->getForm('Drupal\my_module\Form\MyForm'),
        ],
      ],
  ];
}

While I make no argument that this was the "right" place to put this code, the form DID render. However, it did NOT submit or validate using the form's custom submission / validation hooks.

Once I moved the same basic code into the build() method of my Block's controller, it worked fine (I changed the hook_theme to have a NULL entry for the form variable instead of the \Drupal::formBuilder()->getForm() method.

  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#theme' => 'my_block',
      '#description' => $this->configuration['description'],
      '#form' => \Drupal::formBuilder()->getForm('Drupal\my_module\Form\MyForm'),
      '#cache' => ['max-age' => 0],

    ];
  }

A few additional details:

  • When rendered through the block, the form's build method WAS being called (verified via debugger), although it required a cache clear to get it to trip the debugger
  • When rendered through the block, the form's submit/validate methods were NOT being called (verified via debugger) even after a cache clear. Rather, the post from the form was being shoved into the browser's URL bar and the form was reloading empty.
  • When rendered via a custom route (and not through the block rendering), the form worked fine (and tripped all expected submit/validation behaviors)
  • The block was placed on a landing page via panelizer

Proposed resolution

Correct form rendering so that the submit / validation handlers fire properly, or update form rendering so that Drupal triggers an error and warns the user that they have implemented the method from a location that will not fully render the form.

Remaining tasks

TBD

User interface changes

TBD

API changes

TBD

Data model changes

TBD

Original report by mikemadison

TBD

Comments

mikemadison created an issue. See original summary.

mikemadison’s picture

Issue summary: View changes
mikemadison’s picture

Issue summary: View changes
mikemadison’s picture

Issue summary: View changes
mikemadison’s picture

Issue summary: View changes

Version: 8.3.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.