By paulmckibben on
Is there an equivalent to menu_get_object() in Drupal 8? I'm trying to find a way to determine whether a given URL path represents an entity, and load that entity object if it does.
How do I load an entity object given a system path (e.g. "node/123" or "some-entity-type/456").
Comments
http://www.phase2technology
http://www.phase2technology.com/blog/goodbye-menu-get-object/
Contact me to contract me for D7 -> D10/11 migrations.
Thanks! That post explains
Thanks! That post explains this question for nodes, but what about other entities? Is there a way to get an entity given a system path?
Loading a node by route parameters
After some time reading through the various D8 APIs, here's what I have currently to load a node from a system path (as of 8.2). In theory this could extend to other entities as long as the routes for those entities follow a consistent pattern, but I doubt this is the best replacement for menu_get_object():
In Drupal 8.x, given an
In Drupal 8.x, given an internal path of the form "/node/{id}", or "/taxonomy/term/{id}", you can use the following to get and load an entity (whether it's node, taxonomy, etc..):
Only use this in cases where you know the entity has an internal path.
This one worked for me (8.3)
Not sure if that's properly working with the revision route though.
Blog article about Drupal 8 menu_get_object alternative
@mxh's comment is great, I think and the best solution.
I just wrote a blog article about this topic, if someone is interested: https://julian.pustkuchen.com/node/780#comment-864
http://www.DROWL.de || Professionelle Drupal Lösungen aus Ostwestfalen-Lippe (OWL)
http://www.webks.de || webks: websolutions kept simple - Webbasierte Lösungen die einfach überzeugen!
http://www.drupal-theming.com || Individuelle Responsive Themes
Probably better
Thank you very much. Better
Thank you very much. Better use explicit
explicitly. We had an issue with using EntityInterface only... may have more than one implementation....
http://www.DROWL.de || Professionelle Drupal Lösungen aus Ostwestfalen-Lippe (OWL)
http://www.webks.de || webks: websolutions kept simple - Webbasierte Lösungen die einfach überzeugen!
http://www.drupal-theming.com || Individuelle Responsive Themes
Thanks, a shorter version below
Thanks @mxh,
I am using a shorted version based on your code in my module.
The same but with array_reduce
The same but with array_reduce:
Set up the parameters in the .route.yml
Then Drupal will fetch the entity for you (this worked in D7, too):
path: '/current/{cart}'options:
parameters:
cart:
type: 'entity:cart'
With the above, you can then simply ask Drupal for the entity without the bother of fetching the entity via its id yourself:
For any content entity type with a canonical link template:
Looks as though lots of people have already posted various approaches - I blogged about another, which should work for any type of content entity (not just nodes) with a canonical page: https://www.computerminds.co.uk/drupal-code/get-entity-route
This approach checks for whether the page is the canonical page for an entity (as opposed to other routes or view modes that might be available for an entity). It could of course be tweaked to catch other routes, but the point is that you might not want to apply to all routes that an entity might be available on.
ComputerMinds
$node = \Drupal::routeMatch()
$node = \Drupal::routeMatch()->getParameter('node');
You can get the node from the route with the above code.
Source: https://newresponsivetheme.com/drupal-get-node-route
$route = \Drupal::routeMatch(
An extensible solution, allowing custom routes
I'm going to plug my blog post about this topic: https://blog.birk-jensen.dk/drupal-get-the-current-entity
It's similar to what James Williams proposes, but it checks all link templates and allows for extensibility, though it does require a bit more code.
am trying to figure this out
am trying to figure this out too. Are you looking for a built-in function to get the entity object from a system path or more like a custom script approach?
This depends on whether you
This depends on whether you need a "current" entity - or just any entity given the path. Because if you want to take some random url from site and see what entity is behind this url if any, without it being current url - you will have to check if its a aliased url or not, get the system path from it, etc. But thats an unlikely use case.