Displaying forms via routes or blocksfor Drupal 8 , 9 , 10 , and 11

Last updated :  

Having understood how to utilize the formBuilder service to render forms, this lesson will focus on displaying these forms across your Drupal website. By leveraging routes and blocks, you can insert forms exactly where they’re needed, ensuring they’re both accessible and strategically placed for optimal user interaction.

Understanding Routes and Blocks

Routes

In Drupal, routes are a system for mapping URLs to actions. Using routes, you direct users to specific parts of your website, where a form can be an integral part of the displayed content. Each route is defined within a YAML file, associating a URL path with a controller and permissions.

Blocks

Blocks are reusable content containers that can be dynamically placed in defined regions of your site’s layout. They offer a versatile way of displaying forms in sidebars, footers, or any other place with flexible visibility settings for different user roles or conditions.

Displaying Forms Via Routes

To continue from the previous lesson, we use the form you created with the formBuilder service. Here, we’ll define a route that displays the form on a new page.

Defining a Route

First, ensure that the custom module is set up and active. Then, add a new route to the my_custom_form.routing.yml file to map a URL to your form:


my_custom_form.route_display:
  path: '/my-custom-form-route'
  defaults:
    _controller: '\Drupal\my_custom_form\Controller\CustomFormController::displayForm'
    _title: 'Custom Form Via Route'
  requirements:
    _permission: 'access content'

    

This example sets up a route that maps to /my-custom-form-route, where the form is displayed using the controller method from the previous lesson.

Displaying Forms Via Blocks

Drupal blocks provide another convenient way to display forms. Including a form in a block makes it reusable across different pages and in various site sections.

Creating a Block with a Form

Create a custom block plugin by defining a new class in your module's src/Plugin/Block directory. Here is how to implement a block that contains a form:

  1. Create a file named CustomFormBlock.php within src/Plugin/Block.

namespace Drupal\my_custom_form\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a custom form block.
 *
 * @Block(
 *   id = "custom_form_block",
 *   admin_label = @Translation("Custom Form Block"),
 * )
 */
class CustomFormBlock extends BlockBase {

    protected $formBuilder;

    public function __construct(FormBuilderInterface $form_builder) {
        $this->formBuilder = $form_builder;
    }

    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static(
            $container->get('form_builder')
        );
    }

    public function build() {
        return $this->formBuilder->getForm('Drupal\my_custom_form\Form\CustomForm');
    }
}

    

Adding and Placing a Block

With the block plugin set up, go to the Drupal admin interface, navigate to Structure > Block layout, and locate your custom block under "Custom blocks". Place this block in a region of your choice.

Benefits of Using Routes and Blocks

  • Flexibility: Appropriately situating forms through routes and blocks grants you the flexibility to display forms exactly where needed on your site.
  • Accessibility: As blocks, forms are easily configurable and can be made available to specific user roles or depending on pages.
  • Reusability: By using blocks, manage form instances independently from the URL structure, thus making site management easier.

Conclusion

This lesson equipped you with the knowledge to display Drupal forms through routes and blocks, offering versatile approaches to placing forms throughout your site. Such strategies improve usability, increase engagement, and streamline content management.

Next Steps

In the next lesson, we will explore "Learning the structure of #type, #title, and #default_value", diving deeper into the comprehensive setup of form elements and how to best use these controls within Drupal’s Form API.