Jonathan Lahijani Posted August 30 Share Posted August 30 When creating a single page select, I use "Single page Page or boolean false when none selected" value type. It's become a habit that I haven't given it much thought about until recently. I believe this was the default option in PW at some point from the beginning (?), or at least because it shows higher up than the other option, I've tended to use it more. Then when there was an update to ProcessWire a couple years that brought in field setups, if you create a field using the "Singe page: select" setup, it defaults to using the "Single page Page or empty page NullPage when none selected" value type. When I create a field this way, I change it back to "Single page Page or boolean false when none selected" however it makes me wonder if using NullPage as opposed to a boolean is a better practice. This matters because when detecting false vs. NullPage the code must be modified to work correctly. This will bug if the there is no assigned page and the return type is boolean false: // Warning: Attempt to read property "id" on false if($page->my_single_page_reference_field->id) {} Furthermore, if you're not careful with whether you do ->id vs. the value type, your true/false could be swapped. Ack! What is your preference and why? I think mine would be to get a NullPage instead of false if there is no page. Link to comment Share on other sites More sharing options...
da² Posted August 30 Share Posted August 30 I use the false value, but I'd prefer a null value, this is more common. Globally I don't really like the NullPage usage in PW API, instead of a null value. Code is a bit less readable, and in both cases you'll end with an error when accessing a property/method on it, if you forgot to check the value. if (!$page) // Faster to write, easier to read if (!$page->id) if ($page instanceof NullPage) NullPage may be necessary in PW core code, but in my usage I don't see any advantage compared to a null value. Link to comment Share on other sites More sharing options...
bernhard Posted August 30 Share Posted August 30 I'd love to have something like this, where we can write code that doesn't break if something or someone changes an output formatting setting of a field and where we can request the value we need wherever we want: // force single value $p = $page->getPageOrFalse('my_page_ref_field'); if($p) ... // force page object $p = $page->getPageOrNullpage('my_page_ref_field'); if($p->id) ... // force array value $items = $page->getPageArray('my_page_ref_field'); foreach($items as $item) { ... } If you like that, give it a thumbs up: https://github.com/processwire/processwire-requests/issues/538 Link to comment Share on other sites More sharing options...
da² Posted August 30 Share Posted August 30 (edited) 28 minutes ago, bernhard said: If you like that, give it a thumbs up I understand the idea, this is actually a problem that application might break if this setting change. But instead of adding methods I would remove the setting, no more choice between different return types, only Page|null (and PageArray for multiple). Simplicity... 😁 Obviously it would break actual sites deployed with PW, so maybe for a ProcessWire 4.0... ^^ Edited August 30 by da² Link to comment Share on other sites More sharing options...
bernhard Posted September 5 Share Posted September 5 As @teppo mentioned in the issue this is possible to do this and I think this is the most robust way of writing code for page reference fields that work consistently and are not going to break if someone changes field formatting: foreach($page->get('my_pageref_field[]') as $p) ... if($page->get('my_pageref_field.first')) ... 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now