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: |

The Null Object pattern is one of my favourite ways to DRY up code and reduce duplication.

I wrote the System User module for a client project a number of years ago, which also included the Null User module.

If no system user is found, it returns a null (empty) user instead that returns null or empty values by default.

Today, I posted and tooted 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.

In the code, I search for a user with a given licence key.

If a user is found, it is returned.

If not, instead of returning FALSE or NULL, I return a null user object that I can treat the same as a real user.

Now, when I create the node and assign an owner, I can just use $account->id() and it will always work if a user was found or not.

This makes the code cleaner, simpler and easier to test and maintain.

format: full_html processed: |

The Null Object pattern is one of my favourite ways to DRY up code and reduce duplication.

I wrote the System User module for a client project a number of years ago, which also included the Null User module.

If no system user is found, it returns a null (empty) user instead that returns null or empty values by default.

Today, I posted and tooted 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.

In the code, I search for a user with a given licence key.

If a user is found, it is returned.

If not, instead of returning FALSE or NULL, I return a null user object that I can treat the same as a real user.

Now, when I create the node and assign an owner, I can just use $account->id() and it will always work if a user was found or not.

This makes the code cleaner, simpler and easier to test and maintain.

summary: null field_daily_email_cta: { }