Score:0

Creating dynamic controllers for the same route

bj flag
Iza

I have a route defined as

my_node_action.go:
  path: '/node/{node}/go'
  defaults:
    _title_callback: '\Drupal\my_node_action\Form\Go::title'
    _form: '\Drupal\my_node_action\Form\Go'
  options:
    parameters:
      node:
        type: entity:node
  requirements:
    _user_is_logged_in: 'TRUE'

where Go is extending FormBase.

I want to add 2 different classes: NodeType1Go and NodeType2Go which extend Go So that when the node->bundle() is type1 the form will be NodeType1Go and when its type2 the form is NodeType2Go

What is the best way to do this and still keep one single routeName

Thank you for your help

ru flag
Logic can not be done with YAML config. Use a `_controller` instead of the `_form` and implement your logic / return the desired form there.
ru flag
Here is an example https://www.drupal.org/forum/support/module-development-and-code-questions/2020-10-23/displaying-form-with-a-controller-in
Score:1
id flag

As you already imply in your question, you'll want to use a custom controller action that returns the form depending on your node type.

First, alter the route to point to a controller method:

my_node_action.go:
  path: '/node/{node}/go'
  defaults:
    _title_callback: '\Drupal\my_node_action\Controller\GoController::goFormTitle'
    _controller: '\Drupal\my_node_action\Controller\GoController::goForm'
  options:
    parameters:
      node:
        type: entity:node
  requirements:
    _user_is_logged_in: 'TRUE'

Then you create the GoController.php file in your module's src/Controller folder:

<?php

namespace Drupal\my_node_action\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class GoController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * Constructs a GoController instance.
   *
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder.
   */
  public function __construct(
    FormBuilderInterface $form_builder
  ) {
    $this->formBuilder = $form_builder;
  }

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

  /**
   * Return the go form.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node.
   *
   * @return array
   *   Form render array for the go form.
   */
  public function goForm(NodeInterface $node) {
    $form = [];

    switch ($node->getType()) {
      case 'type1':
        $form = $this->formBuilder()->getForm('Drupal\my_node_action\Form\NodeType1Go', $node);
        break;

      // Further types/forms and/or a default form here...
    }
    
    return $form;
  }

  /**
   * Return the go form title.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node.
   *
   * @return string
   *   Form title.
   */
  public function goFormTitle(NodeInterface $node) {
    // Whatever logic may apply here...
  }

}
ru flag
The `formBuilder` is already injected by `ControllerBase`, so `implements ContainerInjectionInterface` (and therefore `__construct()` and `create()`) are not necessary
Mario Steinitz avatar
id flag
It actually isn't. Yes, ControllerBase provides the protected property and a getter. But gets it from the container as well, rather than through dependency injection/the static object factory method. Though it definitely isn't a must, in our company we made it a habit to initialize such used properties with dependency injection in order to make it visible at first glance which other services are in use. ;)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.