Drupal - Can we add the destination query parameter to a link from yml configuration?

Yes, you can add the destination query directly in the yml file. @see : \Drupal\Core\Menu\LocalActionDefault::getOptions

ie: mymodule.links.action.yml

my_module.my_entity.add:
  route_name: node.add
  title: 'Add My Entity'
  route_parameters:
        node_type: 'my_entity'
  options:
    query:
      destination: '/path/to/redirect'
  appears_on:
    - my_module.my_entity.admin_content

The query string is not part of the route. The controller fetches the query parameters from the webserver request and does the processing, in this case returns a redirect:

$request = \Drupal::request();
$destination = $request->query->get('destination');
return new RedirectResponse($destination);

When generating an url for the example mentioned in the question, the node_type is a parameter that goes into the route as configured in the routing yml file and the destination is a query parameter that is appended to the url and is not part of the route:

$url = \Drupal\Core\Url::fromRoute(
    'node.add',
     array('node_type' => $type->id()),
     array(
       'query' => array('destination' => $destination),
       'absolute' => TRUE,
     )
);

On both sides the routing system ignores whatever you put in the query of the url, you have to handle this in the code yourself.

Tags:

Navigation

8