entity

Drupal 11: How To Alter Entity View Builder Configuration Before Rendering

I encountered an issue on a Drupal 11 site recently where I had a block that was rendering an entity to display on a page.

There was nothing unusual about what was going on in the rendering process, but in this case I needed to add some attributes to the entity markup and I found that this process wasn't that simple. The solution was to intercept the rendering process half way through using a pre-rendering callback method.

As it wasn't that simple I took some notes and decided to convert them into an article to show how to do the same. In this article we will look at using the view builder to generate a renderable view of an entity and then look at how to alter the attributes of the view mode without using a preprocess hook.

Drupal 9: Entity Bundle Classes

Drupal 9.3.0 came with a new feature called entity bundle classes. This allows more control of entity bundles within Drupal and provides a number of benefits over the previous mechanism of using hooks to control everything.

Drupal makes extensive use of entities to manage content and configuration. User details, pages of content and taxonomy terms just a few of the things that are represented in a Drupal site as content entities. Different types of similar configuration are called configuration entities, which would include things like text format settings or even date formats.

Content entities are normally fieldable, and in the case of content types and taxonomy terms Drupal allows users to generate their own variants. These different variants of entities are called bundles. Entities define the types of objects found in a Drupal site, bundles represent the different sub-types of these entities.

Loading A User's Groups In Drupal 7

Whilst working with Organic Groups today I had the need to load a list of the nodes that a user is connected to. After a bit of looking around in the source code I couldn't find a good solution on how to do this. So after looking around on Google for a bit I just sat down and wrote one.

Getting the group nodes that a user is a member of is quite easy as it turns out, but must be done in a number of steps. The first step is to grab a list of the group entity ID's that the user is connected through using the og_get_entity_groups() function. This can be used with no parameters (which assumes the current user).

// Load in the current user's group entity ID's
$groups = og_get_entity_groups();

Or you can load a user and pass this object to the function.