How do I embed a form in a controller response? I have a controller that returns a normal renderable page array. I'd like to embed a form in that page, but the form is not appearing in the rendered output. The form is properly added to the renderable array correctly, but it's not in the final HTML for some reason. What's going on here?

Here's my controller (which extends ControllerBase through BlockController):

use Drupal\block\Controller\BlockController;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Render\Element;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LayoutController extends BlockController {

  /**
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * {@inheritdoc}
   */
  public function __construct(ThemeHandlerInterface $theme_handler, FormBuilderInterface $form_builder) {
    parent::__construct($theme_handler);
    $this-&gt;formBuilder = $form_builder;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container-&gt;get('theme_handler'),
      $container-&gt;get('form_builder')
    );
  }

  /**
   * Overrides the region demo page, adding placeholder elements for sortable
   * lists of blocks, plus a control panel form.
   *
   * @param string $theme
   *   The theme key.
   *
   * @return array
   *   The renderable page.
   */
  public function layout($theme) {
    $page = parent::demo($theme);

    foreach (Element::children($page) as $region) {
      if (isset($page[$region]['block_description'])) {
        $page[$region]['block_description']['#template'] .= '&lt;ul data-region="{{ region }}"&gt;&lt;/ul&gt;';
        $page[$region]['block_description']['#context']['region'] = $region;
      }
    }
    $page['layout_control'] = $this-&gt;formBuilder-&gt;getForm('Drupal\block_ui\Form\LayoutControlForm');
    $page['#attached']['library'][] = 'block_ui/backbone.collectionView';

    return $page;
  }

}
To embed a form in a renderable array, you use:

$form_class = '\Drupal\my_module\Form\MyForm';
$build['form'] = \Drupal::formBuilder()-&gt;getForm($form_class);


Which is similar to your code.

But the problem is of a different nature. You are extending a special controller, one that returns a renderable array of '#type' =&gt; 'page'. It uses the page template to render the output and that template does not contain the {{ layout_control }} variable to output your form. If you use $page['header']['layout_control'] = ... to add the form to the render array, it should show the form in the header region of the layout demo page.