Skip to content

Proposed API Additions for ProcessWire Page Reference Fields #538

@BernhardBaumrock

Description

@BernhardBaumrock

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

  1. getPageOrFalse('field_name'): Returns a single Page object or false.
  2. getPageOrNullpage('field_name'): Returns a single Page object or a NullPage.
  3. getPageArray('field_name'): Always returns a PageArray, even for single-page references.

Benefits

  1. Consistency: Provides predictable return types, reducing the need for type checking.
  2. Simplicity: Allows for cleaner, more straightforward code in templates or modules.
  3. 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions