oliverdavies.uk/content/node.75f502ee-38a8-45af-b4a4-c5abd82a4082.yml

116 lines
4.9 KiB
YAML

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: |
<p>As someone who writes a lot of custom Drupal modules, one of my favourite additions to Drupal has been Bundle Classes.</p>
<p>What do they do?</p>
<p>When writing Drupal modules, instead of relying on generic classes like <code>Node</code> or <code>Term</code>, you can create your own class for each entity type (e.g. each content type or taxonomy vocabulary).</p>
<p>This makes the code more readable and means you can add behaviour to each class by adding its own methods.</p>
<p>You can see how I've done this on my website:</p>
<pre><code>function opd_presentations_entity_bundle_info_alter(array &amp;$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;
}
}
</code></pre><p>Within my <code>opd_presentations.module</code> file, I override the classes Drupal uses for Presentation nodes and Event paragraph types.</p>
<p>My Presentation class looks like this:</p>
<pre><code>final class Presentation extends Node implements NodeInterface {
public const NODE_TYPE = 'presentation';
public function getEvents(): Events {
return Events::fromEvents($this-&gt;get('field_events')-&gt;referencedEntities());
}
}
</code></pre><p>With this, to get the events for any presentation, I can do something like <code>$presentation-&gt;getEvents()</code> and this code will be used.</p>
<p>I use the same approach in my podcast module. The <a href="https://code.oliverdavies.uk/opdavies/oliverdavies.uk/src/commit/8a480121d203ca6f51310f952b15cfa09080b034/modules">code for my website is public</a> if you want to see other examples of how I'm using this approach.</p>
<p>P.S. If you have questions about how to use this approach or other ways to improve your Drupal code, <a href="http://default/call">book a one-on-one consulting call with me</a> and I'll help you get started.</p>
summary: ''
field_daily_email_cta: { }