[6.x] Fix count() TypeError when entries collections config is a string - #15177
Conversation
The relationship filters endpoint crashed when opening Link to Entry if collections was a string (common YAML shape), because the Collection filter called count() without wrapping. Co-authored-by: Cursor <cursoragent@cursor.com>
jasonvarga
left a comment
There was a problem hiding this comment.
This is an AI-generated review.
Thanks for tracking this down! The Arr::wrap() fix is correct for the two spots it touches, but the same crash is still reachable elsewhere — this needs two more fixes before merge:
-
Entries::relationshipQueryBuilder()(src/Fieldtypes/Entries.php:525) reads$this->config('collections')raw and passes it towhereIn('collection', $collections). With a string config, Stache'swhereIn()doesarray_merge($this->collections ?? [], $values), which throws the sameTypeErrorviawhereRelation/whereHasqueries on relation fields. Swap it forgetConfiguredCollections()(or wrap withArr::wrap()). -
EntryLinkType::collections()(src/Fieldtypes/Link/EntryLinkType.php:69-84) declares: arraybut returns$field->get('collections')unwrapped. When that's a string, it throwsTypeError: ...collections(): Return value must be of type array, string returned— on every render of a Link field with the Entry type visible. Since nav items use this exact class, this is likely the actual crash behind "Link to Entry in a nav" from the report, and it happens before either of the two patched call sites are ever reached.Fix:
return Arr::wrap($collections);
(plus
use Statamic\Support\Arr;)Regression test (verified it fails on this branch with the
TypeErrorabove, and passes once the fix is applied) — add totests/Fieldtypes/LinkTest.php:use Statamic\Fieldtypes\Link\EntryLinkType; #[Test] public function it_wraps_a_string_collections_config_into_an_array_for_the_entry_link_type() { // A hand-authored blueprint may set `collections: pages` (a string) rather than // `collections: [pages]`. Building the nested entries fieldtype config used to // return that string as-is, which broke the `array` return type on collections() // and crashed as soon as the link field's "entry" type was rendered (e.g. opening // "Link to Entry" in a nav item). $field = new Field('test', ['type' => 'link', 'collections' => 'pages']); $config = (new EntryLinkType)->fieldtype($field); $this->assertEquals(['pages'], $config['collections']); }
…lder Jason's review: string collections still blew up EntryLinkType's array return type (likely the actual nav Link to Entry crash) and whereIn via relationshipQueryBuilder. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Addressed the review:
|
jasonvarga
left a comment
There was a problem hiding this comment.
This is an AI-generated review.
Both follow-up fixes look correct — relationshipQueryBuilder() now goes through getConfiguredCollections(), and EntryLinkType::collections() wraps with Arr::wrap(). I checked out the branch and re-ran LinkTest, RelationshipFieldtypeTest, and EntryQueryBuilderTest (166 tests, all green), and re-ran the whereRelation scratch repro from my earlier comment against the new code — no longer throws. Thanks for the quick turnaround.
Summary
count(): Argument #1 ($value) must be of type Countable|array, string givenwhencollectionswas a stringArr::wrap()in the Collection filter andEntries::getConfiguredCollections()so string YAML configs (collections: pages) don't blow up on PHP 8From Discord: https://discord.com/channels/489818810157891584/1537065384119894146