1

Using \Drupal::routeMatch()->getParameter(), it's possible to fetch objects that come from the parameters of the URL.

I'm wondering if it is possible to add values to the routeMatch. For example, if using the Groups module, nodes may be part of a group, however the group is not part of the path. Is there a way to load the group from the node, and add the group to the parameters that can be retrieved with \Drupal::routeMatch()->getParameter()?

Or, more broadly, I'd like to set the $group to be part of the "context" of a page request on a node page.

2
  • No, this is not necessary. The node instance is referenced in the route match and you can get the group info anytime you want with no performance hit. Whether you need a group cache context depends, because on a node page the main page content already depends on the more specific node and a more general cache context wouldn't make a difference. Unless you have page elements like blocks which only depend on the group.
    – 4k4
    Nov 22 at 21:40
  • It's the latter - I have blocks with are dependent upon the group.
    – Jaypan
    Nov 23 at 0:16
2

To use it in a block you could create a custom route.node_group context which gets the context from the route match. Hierarchically you have to place it under the route context. When merging contexts, for example if the context bubbles up to the page level, route can replace route.node_group.

1
  • Thanks 4x4. I upvoted it.
    – Jaypan
    Nov 23 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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