uuid: - value: 75f502ee-38a8-45af-b4a4-c5abd82a4082 langcode: - value: en type: - target_id: daily_email target_type: node_type target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7 revision_timestamp: - value: '2025-06-20T23:06:28+00:00' revision_uid: - target_type: user target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849 revision_log: { } status: - value: true uid: - target_type: user target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849 title: - value: 'Drupal Bundle Classes' created: - value: '2025-06-17T22:49:40+00:00' changed: - value: '2025-06-20T23:06:28+00:00' promote: - value: false sticky: - value: false default_langcode: - value: true revision_translation_affected: - value: true path: - alias: /daily/2025/06/17/drupal-bundle-classes langcode: en body: - value: |- As someone who writes a lot of custom Drupal modules, one of my favourite additions to Drupal has been Bundle Classes. What do they do? When writing Drupal modules, instead of relying on generic classes like `Node` or `Term`, you can create your own class for each entity type (e.g. each content type or taxonomy vocabulary). This makes the code more readable and means you can add behaviour to each class by adding its own methods. You can see how I've done this on my website: ```php function opd_presentations_entity_bundle_info_alter(array &$bundles): void { if (isset($bundles['node'])) { $bundles['node'][Presentation::NODE_TYPE]['class'] = Presentation::class; } if (isset($bundles['paragraph'])) { $bundles['paragraph'][Event::PARAGRAPH_TYPE]['class'] = Event::class; } } ``` Within my `opd_presentations.module` file, I override the classes Drupal uses for Presentation nodes and Event paragraph types. My Presentation class looks like this: ```php final class Presentation extends Node implements NodeInterface { public const NODE_TYPE = 'presentation'; public function getEvents(): Events { return Events::fromEvents($this->get('field_events')->referencedEntities()); } } ``` With this, to get the events for any presentation, I can do something like `$presentation->getEvents()` and this code will be used. I use the same approach in my podcast module. The [code for my website is public][0] if you want to see other examples of how I'm using this approach. P.S. If you have questions about how to use this approach or other ways to improve your Drupal code, [book a one-on-one consulting call with me][1] and I'll help you get started. [0]: https://code.oliverdavies.uk/opdavies/oliverdavies.uk/src/commit/8a480121d203ca6f51310f952b15cfa09080b034/modules [1]: /call format: markdown processed: |
As someone who writes a lot of custom Drupal modules, one of my favourite additions to Drupal has been Bundle Classes.
What do they do?
When writing Drupal modules, instead of relying on generic classes like Node
or Term
, you can create your own class for each entity type (e.g. each content type or taxonomy vocabulary).
This makes the code more readable and means you can add behaviour to each class by adding its own methods.
You can see how I've done this on my website:
function opd_presentations_entity_bundle_info_alter(array &$bundles): void {
if (isset($bundles['node'])) {
$bundles['node'][Presentation::NODE_TYPE]['class'] = Presentation::class;
}
if (isset($bundles['paragraph'])) {
$bundles['paragraph'][Event::PARAGRAPH_TYPE]['class'] = Event::class;
}
}
Within my opd_presentations.module
file, I override the classes Drupal uses for Presentation nodes and Event paragraph types.
My Presentation class looks like this:
final class Presentation extends Node implements NodeInterface {
public const NODE_TYPE = 'presentation';
public function getEvents(): Events {
return Events::fromEvents($this->get('field_events')->referencedEntities());
}
}
With this, to get the events for any presentation, I can do something like $presentation->getEvents()
and this code will be used.
I use the same approach in my podcast module. The code for my website is public if you want to see other examples of how I'm using this approach.
P.S. If you have questions about how to use this approach or other ways to improve your Drupal code, book a one-on-one consulting call with me and I'll help you get started.
summary: '' field_daily_email_cta: { }