diff --git a/content/meta/index.json b/content/meta/index.json index 30383f3b9..faa70d17c 100644 --- a/content/meta/index.json +++ b/content/meta/index.json @@ -6484,5 +6484,14 @@ ], "path_alias.baec19d7-cb5e-4ba8-a181-15aa7cd85aee": [ "node.0c66d8de-6dbd-4ab1-b40f-29276d7af5f1" + ], + "node.75f502ee-38a8-45af-b4a4-c5abd82a4082": [ + "user.b8966985-d4b2-42a7-a319-2e94ccfbb849" + ], + "path_alias.93e6c499-c547-4942-a6d1-e93520789f49": [ + "node.75f502ee-38a8-45af-b4a4-c5abd82a4082" + ], + "redirect.714cbf00-2159-46b5-893e-67dd5da6013a": [ + "user.b8966985-d4b2-42a7-a319-2e94ccfbb849" ] } \ No newline at end of file diff --git a/content/node.75f502ee-38a8-45af-b4a4-c5abd82a4082.json b/content/node.75f502ee-38a8-45af-b4a4-c5abd82a4082.json new file mode 100644 index 000000000..ddb6a376c --- /dev/null +++ b/content/node.75f502ee-38a8-45af-b4a4-c5abd82a4082.json @@ -0,0 +1,92 @@ +{ + "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\/20\/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.\r\n\r\nWhat do they do?\r\n\r\nWhen 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).\r\n\r\nThis makes the code more readable and means you can add behaviour to each class by adding its own methods.\r\n\r\nYou can see how I've done this on my website:\r\n\r\n```php\r\nfunction opd_presentations_entity_bundle_info_alter(array &$bundles): void {\r\n if (isset($bundles['node'])) {\r\n $bundles['node'][Presentation::NODE_TYPE]['class'] = Presentation::class;\r\n }\r\n\r\n if (isset($bundles['paragraph'])) {\r\n $bundles['paragraph'][Event::PARAGRAPH_TYPE]['class'] = Event::class;\r\n }\r\n}\r\n```\r\n\r\nWithin my `opd_presentations.module` file, I override the classes Drupal uses for Presentation nodes and Event paragraph types.\r\n\r\nMy Presentation class looks like this:\r\n\r\n```php\r\nfinal class Presentation extends Node implements NodeInterface {\r\n\r\n public const NODE_TYPE = 'presentation';\r\n\r\n public function getEvents(): Events {\r\n return Events::fromEvents($this->get('field_events')->referencedEntities());\r\n }\r\n\r\n}\r\n```\r\n\r\nWith this, to get the events for any presentation, I can do something like `$presentation->getEvents()` and this code will be used.\r\n\r\nI 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.\r\n\r\nP.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.\r\n\r\n[0]: https:\/\/code.oliverdavies.uk\/opdavies\/oliverdavies.uk\/src\/commit\/8a480121d203ca6f51310f952b15cfa09080b034\/modules\r\n[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.<\/p>\n
What do they do?<\/p>\n
When writing Drupal modules, instead of relying on generic classes like This makes the code more readable and means you can add behaviour to each class by adding its own methods.<\/p>\n You can see how I've done this on my website:<\/p>\n Within my My Presentation class looks like this:<\/p>\n With this, to get the events for any presentation, I can do something like I use the same approach in my podcast module. The code for my website is public<\/a> if you want to see other examples of how I'm using this approach.<\/p>\nNode<\/code> or
Term<\/code>, you can create your own class for each entity type (e.g. each content type or taxonomy vocabulary).<\/p>\n
function opd_presentations_entity_bundle_info_alter(array &$bundles): void {\n if (isset($bundles['node'])) {\n $bundles['node'][Presentation::NODE_TYPE]['class'] = Presentation::class;\n }\n\n if (isset($bundles['paragraph'])) {\n $bundles['paragraph'][Event::PARAGRAPH_TYPE]['class'] = Event::class;\n }\n}\n<\/code><\/pre>
opd_presentations.module<\/code> file, I override the classes Drupal uses for Presentation nodes and Event paragraph types.<\/p>\n
final class Presentation extends Node implements NodeInterface {\n\n public const NODE_TYPE = 'presentation';\n\n public function getEvents(): Events {\n return Events::fromEvents($this->get('field_events')->referencedEntities());\n }\n\n}\n<\/code><\/pre>
$presentation->getEvents()<\/code> and this code will be used.<\/p>\n