7

When creating a link from yaml

ie: mymodule.links.action.yml

my_module.my_entity.add:
  route_name: node.add
  title: 'Add My Entity'
  route_parameters:
        node_type: 'my_entity'
  appears_on:
    - my_module.my_entity.admin_content

Is there a way that this links would be generated with the ?destination=path/to/redirect ?

3
  • You can try creating a custom route for that link. And add the destination in the route definition.
    – Eyal
    Feb 4, 2016 at 21:42
  • @eyal Can you be more specific about adding the destination in the route definition? Should the custom route be done by code using route callback ? Thanks Feb 4, 2016 at 22:17
  • Try and create a mymodule.routing.yml file. In that for you can define new routes and provide parameters for them. I'm not sure if it is possible to add a destination parameter but it is worth the try.
    – Eyal
    Feb 4, 2016 at 22:20

2 Answers 2

17

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
13

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.

Your Answer

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

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