{ "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": "\n

The Null Object pattern is one of my favourite ways to DRY up code and reduce duplication.<\/p>\n\n

I wrote the System User module<\/a> for a client project a number of years ago, which also included the Null User module<\/a>.<\/p>\n\n

If no system user is found, it returns a null (empty) user instead that returns null or empty values by default.<\/p>\n\n

Today, I posted<\/a> 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>\n\n

In the code, I search for a user with a given licence key.<\/p>\n\n

If a user is found, it is returned.<\/p>\n\n

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>\n\n

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

This makes the code cleaner, simpler and easier to test and maintain.<\/p>\n\n ", "format": "full_html", "processed": "\n

The Null Object pattern is one of my favourite ways to DRY up code and reduce duplication.<\/p>\n\n

I wrote the System User module<\/a> for a client project a number of years ago, which also included the Null User module<\/a>.<\/p>\n\n

If no system user is found, it returns a null (empty) user instead that returns null or empty values by default.<\/p>\n\n

Today, I posted<\/a> 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>\n\n

In the code, I search for a user with a given licence key.<\/p>\n\n

If a user is found, it is returned.<\/p>\n\n

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>\n\n

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

This makes the code cleaner, simpler and easier to test and maintain.<\/p>\n\n ", "summary": null } ] }