0
route_with_two_nodes:
  path: '/foo/{node1}/{node2}'
  defaults:
    _controller: '\Drupal\example\Controller\ExampleController::foo'
  options:
    parameters:
      node1:
        type: entity:node
      node2:
        type: entity:node

I need to get list of all keys path, defaults, options etc, where is it defined in Drupal Core ? so that if I need to add custom key and extend it

3
  • Are you looking for a documentation of routes in Drupal? drupal.org/docs/8/api/routing-system/structure-of-routes
    – Hudri
    Commented Jul 31, 2019 at 8:22
  • 1
    You mean you don't want to read the documentation and find this in the code? So start with the Symfony Routing component Symfony\Component\Routing and then look at the Drupal specific stuff in Drupal\Core\Routing
    – 4uk4
    Commented Jul 31, 2019 at 8:22
  • I need to know where is it defined in Drupal and suppose I need to add my own custom key where can I do it?
    – harshal
    Commented Jul 31, 2019 at 8:24

1 Answer 1

1

Add a route enhancer:

Example how _form gets replaced by _controller:

Drupal\Core\Routing\Enhancer\FormRouteEnhancer:

<?php

namespace Drupal\Core\Routing\Enhancer;

use Drupal\Core\Routing\EnhancerInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;

/**
 * Enhancer to add a wrapping controller for _form routes.
 */
class FormRouteEnhancer implements EnhancerInterface {

  /**
   * Returns whether the enhancer runs on the current route.
   *
   * @param \Drupal\Core\Routing\Enhancer\Route $route
   *   The current route.
   *
   * @return bool
   */
  protected function applies(Route $route) {
    return $route->hasDefault('_form') && !$route->hasDefault('_controller');
  }

  /**
   * {@inheritdoc}
   */
  public function enhance(array $defaults, Request $request) {
    $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
    if (!$this->applies($route)) {
      return $defaults;
    }

    $defaults['_controller'] = 'controller.form:getContentResult';
    return $defaults;
  }

}
6
  • so does this replace the _form key with _controller ?
    – harshal
    Commented Jul 31, 2019 at 8:39
  • Yes, this is a virtual _form key, which is in reality a _controller key pointing to a service providing a controller.
    – 4uk4
    Commented Jul 31, 2019 at 8:43
  • so _form would be present in routing.yml but in the backend it will always use _controller and list for which can be found from the Symfony\Component\Routing\Annotation; which has private variables defined as $path, $name, $requirements, etc so if I need to add more private variables, I would have to extend this Class Route and add more variables which will be used further, is that correct?
    – harshal
    Commented Jul 31, 2019 at 8:49
  • The code above works on its own, it adds simply a _controller key for another key which otherwise wouldn't be recognized. You could do the same with your custom key. This is only an example, there are many other ways to extend routing. BTW annotation based route discovery is not used by Drupal, only YAML based.
    – 4uk4
    Commented Jul 31, 2019 at 9:16
  • 1
    This is a very Drupal specific thing, scanning module directories for YAML files, never looked into it, But it should be transparent for all keys you add, there is probably no processing at all happening in this part.
    – 4uk4
    Commented Jul 31, 2019 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.