-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
Background
Currently, ProcessWire's Page Reference fields can return different types (Page object, PageArray, or null) depending on the field's configuration and content. This can lead to inconsistent handling in templates and require type checking or conversion.
Proposed Additions
getPageOrFalse('field_name')
: Returns a single Page object or false.getPageOrNullpage('field_name')
: Returns a single Page object or a NullPage.getPageArray('field_name')
: Always returns a PageArray, even for single-page references.
Benefits
- Consistency: Provides predictable return types, reducing the need for type checking.
- Simplicity: Allows for cleaner, more straightforward code in templates or modules.
- Flexibility: Offers different methods to suit various coding styles and use cases.
Use Cases
- Simplifying conditional checks for single-page references
- Ensuring consistent iteration over page references, whether single or multiple
- Reducing boilerplate code for handling different field configurations
Examples
// Single value with simple conditional
$p = $page->getPageOrFalse('my_page_ref_field');
if($p) {
// Use $p as a Page object
}
// Single value using NullPage pattern
$p = $page->getPageOrNullpage('my_page_ref_field');
if($p->id) {
// Use $p as a non-null Page object
}
// Consistent array handling
$items = $page->getPageArray('my_page_ref_field');
foreach($items as $item) {
// Always iterates, even for single-page references
}
These additions would improve code readability, reduce errors related to unexpected types, and provide developers with more intuitive ways to work with Page Reference fields, regardless of their configuration.
Metadata
Metadata
Assignees
Labels
No labels