1

Task: Create a view with link to it displayed among node tabs (View, Edit, etc.).

Solution: Create a view with page display and set its path to "node/%node/foo".

Issue: When the page "node/%node/foo" is visited, \Drupal::routeMatch()->getParameter('node') returns node ID instead of node object.

Is there a simple way to make it return the node object? (Currently I have to alter the view route and set the parameter type.)

1 Answer 1

2

Views doesn't use typed arguments as it doesn't understand them anyway. Why do you need that to work? You can just load it where you need it? And if you have code that relies on that being an object then that code is wrong and will also fail on node/N/revisions for example.

What might work is defining such a page with page_manager, that will allow to define the right type.. but on the other hand, page manager does not yet support defining local tasks.

3
  • Will \Drupal::routeMatch()->getParameter('node') return a string on /node/{node}/revisions? Commented Feb 20, 2017 at 23:36
  • 1
    I think it is actually on viewing a specific revision, not the overview. But yes, that will just return the ID then. You should always check with if ($node instanceof NodeInterface)
    – Berdir
    Commented Feb 21, 2017 at 7:10
  • Berdir is correct that your code should check and work with this, however, the $node parameter not getting up-casted on revision pages is a core bug in the Drupal issue queue. Commented Oct 8, 2019 at 16:01

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.