Skip to main content

Articles tagged with "object"

Get Hook-ed on Object-Oriented Programming

15th Sep 2021

Does writing hooks make you sad because the code is too procedural? Are you keen to use the object-oriented programming approach that Drupal and its service container enable, wherever you possibly can? Read on if you answered yes to any of those questions.

Read more

Get the entity for a route

29th Aug 2017

In Drupal 7, we had menu_get_object() to get the object in code for whichever node, term, or whatever entity makes up the current page. But in Drupal 8, that has been replaced, and entities can usually be operated on in a generic way. If you know the type of entity you want, it's pretty simple. But I keep running into different suggestions of how to get the entity object when you don't know its type. Given that D8 allows us to deal with entities in a unified way, I thought there must be a good way! The code of core itself is usually the best place to look for examples of how to do common things.

Read more