2

The url to display node 100 is <site>/node/100. I want to pass a parameter to this node via the url. For example: <site>/node/100/3.

Question:

  • What must I do to get Drupal 8 to see /node/100/3 as /node/100 with the parameter 3 instead of just the url /node/100/3 (which fails)?

Efforts:

  • I've read many posts, but didn't find any that seem to directly address this issue.
  • I realize this can probably be accomplished via /node/100?param=3. I want to avoid using a query parameter if possible, just for cleanness.
  • I realize this can probably be accomplished by displaying the node within a view and then using a contextual filter. I want to avoid using a view if possible, again just for cleanness.
2
  • 1
    To do this you need to define a route and tell Drupal not only about your new route, but what that parameter is supposed to be, or if its cast to an entity. Then you would have to load that into a hook or function that creates variables for the twig template being used.
    – Kevin
    Commented Mar 13, 2019 at 17:32
  • Thanks @Kevin. I have defined a route "/node/{nid}/{param}". Also a controller receiving arguments $nid and $param. I can display the values received by returning #markup from the controller, so I know I'm getting them. I already had a preprocess_node hook where I can create variables for the twig template being used. What I don't know is how to make the connection between my controller and my hook. Commented Mar 13, 2019 at 22:47

1 Answer 1

1

try this in twig:

{% set url =  url('<current>')['#markup'] %}
{% set urllast =   url|split('/')|last %}
{{ urllast }}  // 3
1
  • Thank you for posting this! Not sure why you had a downvote. Simple and to the point without having to mess with hooks and PHP code.
    – Edward B.
    Commented Aug 14, 2020 at 5:51

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.