If no system user is found, it returns a null (empty) user instead that returns null or empty values by default.
Today, I postedand 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.
- Oliver
Was this interesting?
About me
I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.