Skip to content

$page->get('myfield[]') method should return an empty WireArray rather than an empty plain PHP array when the field doesn't exist #580

@16th-earl

Description

@16th-earl

Currently (version 3.0.255) if you use the PHP $page->get('myfiles[]') method, where 'myfiles' is a multi-file (Pagefiles) field that happens to be empty of files, then you get an empty Pagefiles array in return. If myfiles doesn't exist in the page template at all or it's not a multi-file field, then you get an empty PHP array.

This doesn't matter so much if you're using a foreach loop to iterate through the results, however if you were relying on one of the WireArray methods then this causes a crash. For example,

// If the page has a myfiles multi-file field that is empty
$result = $page->get('myfiles[]')->implode("\n", 'description');
// $result === '';

// If the page does not have a myfiles field at all
$result = $page->get('myfiles[]')->implode("\n", 'description');
// Uncaught Error: Call to a member function implode() on array

// If the page field isn't an array type (e.g. a Textarea)
$result = $page->get('body[]')->implode("\n", 'description');
// Uncaught Error: Call to a member function implode() on array

// Work-around
$result = gettype($myFiles = $page->get('myfiles[]')) === 'object' ? $myFiles->implode("\n", 'description') : '';

I think it'd be more consistent and useful to return an empty WireArray, rather than an empty PHP array.

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