Drupal’s entity model makes it possible to create highly complex data structures, these tools and tricks can help you navigate them more efficiently.
When you are building complex Drupal websites you rely on entities, and they are often entities within entities, linked to entities, and so on.
When you need to debug data deep down in the persistence layer, be it in a moderation state, language revision or somewhere else because Drupal gives you an inconsistent response, where do you begin?
A great start is setting a breakpoint instead of just trying to observe the data structure as it passes through your call with
print_r() or the like.
Xdebug is certainly a very good route to discover the state and properties of a specific entity. However, it is often insufficient to debug complex entity relationships in an efficient manner: Child entities might only be visible as their target ID or dynamic properties are empty because they have not been built yet.
Another place to look is the database, and that’s fine. It is of course the final say on the persistence layer but has significant disadvantages: writing joins by hand to discover the linkages of fields and tables is at best cumbersome and downright annoying when you are jumping from entity to entity. Also, reusing and managing such query snippets is not easy.
So your next step would likely be to write a custom script and make use of entityTypeManager. It can answer most complex queries and if you have done a little bit of Drupal 8 development you’ll likely already have come across it. Just select the desired storage, fetch a query, add your conditions and you can access the relevant revisions, their fields, and properties with ease. In some cases you might need to include some help from EntityFieldManager.
Drupal 7 hint: If you are stuck on a site you can’t migrate just yet, you can still make your life easier by using
EntityFieldQuery. You don’t have to live with stdClass objects and raw database queries.
However, you’re still writing a lot of common queries repeatedly by hand, when you just want something more efficient than digging through revisions in the UI for a particular problem.
Entity Explorer is a simple Drupal Console command which uses just a handful of entityTypeManager commands to build you an efficient output of an entity across languages and revisions including child elements, if needed:
$ drupal entity_explorer node 16287 47850 # Arguments: type, id, revision (optional)
The first example shows the translation revision of a node with the IDs of embedded entities. Use
--all-fields to recursively show their details:
Check it out:
composer require drupal/entity_explorer:~1.0