oliverdavies.uk/content/node.f529894e-5912-4e77-91a3-d78b9cbf2584.yml

80 lines
3.5 KiB
YAML

uuid:
- value: f529894e-5912-4e77-91a3-d78b9cbf2584
langcode:
- value: en
type:
- target_id: daily_email
target_type: node_type
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
revision_timestamp:
- value: '2025-05-11T09:00:07+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: 'Revisiting the Null Object pattern in Drupal'
created:
- value: '2024-08-30T00:00:00+00:00'
changed:
- value: '2025-05-11T09:00:07+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2024/08/30/revisiting-the-null-object-pattern-in-drupal
langcode: en
body:
- value: |
<p>The Null Object pattern is one of my favourite ways to DRY up code and reduce duplication.</p>
<p>I wrote the <a href="https://www.drupal.org/project/system_user">System User module</a> for a client project a number of years ago, which also included the <a href="https://www.drupal.org/project/null_user">Null User module</a>.</p>
<p>If no system user is found, it returns a null (empty) user instead that returns null or empty values by default.</p>
<p>Today, <a href="https://x.com/opdavies/status/1829183673725284772">I posted</a> <a href="https://mastodon.social/@opdavies/113046773292848198">and tooted</a> a screenshot of some code from a side project I'm working on, which I realised would be another good time to use the Null User module.</p>
<p>In the code, I search for a user with a given licence key.</p>
<p>If a user is found, it is returned.</p>
<p>If not, instead of returning FALSE or NULL, I return a null user object that I can treat the same as a real user.</p>
<p>Now, when I create the node and assign an owner, I can just use <code>$account-&gt;id()</code> and it will always work if a user was found or not.</p>
<p>This makes the code cleaner, simpler and easier to test and maintain.</p>
format: full_html
processed: |
<p>The Null Object pattern is one of my favourite ways to DRY up code and reduce duplication.</p>
<p>I wrote the <a href="https://www.drupal.org/project/system_user">System User module</a> for a client project a number of years ago, which also included the <a href="https://www.drupal.org/project/null_user">Null User module</a>.</p>
<p>If no system user is found, it returns a null (empty) user instead that returns null or empty values by default.</p>
<p>Today, <a href="https://x.com/opdavies/status/1829183673725284772">I posted</a> <a href="https://mastodon.social/@opdavies/113046773292848198">and tooted</a> a screenshot of some code from a side project I'm working on, which I realised would be another good time to use the Null User module.</p>
<p>In the code, I search for a user with a given licence key.</p>
<p>If a user is found, it is returned.</p>
<p>If not, instead of returning FALSE or NULL, I return a null user object that I can treat the same as a real user.</p>
<p>Now, when I create the node and assign an owner, I can just use <code>$account-&gt;id()</code> and it will always work if a user was found or not.</p>
<p>This makes the code cleaner, simpler and easier to test and maintain.</p>
summary: null
field_daily_email_cta: { }