Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Cast to Array to Resolve Issues with Filters Returning EntryCollection #10627

Merged
merged 2 commits into from
Aug 14, 2024

Conversation

SylvesterDamgaard
Copy link
Contributor

@SylvesterDamgaard SylvesterDamgaard commented Aug 14, 2024

Bug introduced in #10467
Cast to array to prevent issues with listings when data is returned as a Statamic\Entries\EntryCollection

The issue will happen when filters don't return an array, but a EntryCollection like this.

public function fieldItems()
    {
        $entries = \Statamic\Facades\Entry::query()->where('collection', 'locations')->get();

        return [
            'locations' => [
                'type' => 'select',
                'multiple' => true,
                'options' => $entries->mapWithKeys(function (\Statamic\Entries\Entry $item, int $key) {
                    return [$item->id() => $item->title];
                }),
            ],
        ];
    }

Cast to array to prevent issues with listings when data is returned as a Statamic\Entries\EntryCollection
@SylvesterDamgaard SylvesterDamgaard changed the title [5.x] Cast to Array to Resolve Issues with Listings Returning Statamic\Entries\EntryCollection (Bug from #10467) [5.x] Cast to Array to Resolve Issues with Filters Returning Statamic\Entries\EntryCollection (Bug from #10467) Aug 14, 2024
@duncanmcclean duncanmcclean changed the title [5.x] Cast to Array to Resolve Issues with Filters Returning Statamic\Entries\EntryCollection (Bug from #10467) [5.x] Cast to Array to Resolve Issues with Filters Returning EntryCollection Aug 14, 2024
@jasonvarga
Copy link
Member

Casting a collection to an array like that won't work either.

CleanShot 2024-08-14 at 10 50 26

I think you want to do this:

$options = $this->config('options') ?? [];
$options = $options instanceof Collection ? $options->all() : $options;

@jasonvarga
Copy link
Member

What class are you defining your public function fieldItems() in, by the way?

@jasonvarga
Copy link
Member

I'm going to assume you were using that in a filter class, and the error you encountered was array_is_list(): Argument #1 ($array) must be of type array, Illuminate\Support\Collection given.

Adding (array) does prevent the error, but would break functionality elsewhere.

I've updated the PR. Thanks!

@jasonvarga jasonvarga merged commit bd21b00 into statamic:5.x Aug 14, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants