Skip to content

[6.x] Don't filter Terms fieldtype values by collection usage - #15870

Merged
jasonvarga merged 1 commit into
statamic:6.xfrom
ryanmitchell:fix/terms-fieldtype-collection-context
Nov 12, 2026
Merged

jasonvarga merged 1 commit into
statamic:6.xfrom
ryanmitchell:fix/terms-fieldtype-collection-context

Conversation

@ryanmitchell

@ryanmitchell ryanmitchell commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Related: statamic/eloquent-driver#561. Paired driver PR: statamic/eloquent-driver#649, which adds withCollectionContext() to the Eloquent term query builder, along with other speedups that work on any cms version.

The problem

When a Terms field is augmented on an entry, Terms::queryBuilder() adds where('collection', $parent->collectionHandle()). The reason is so the terms get collection-scoped URLs (/blog/tags/foo). But on the term query builder, where('collection') does two things:

  1. Attaches the collection to the resulting terms. This is what the fieldtype wants.
  2. Limits the results to terms used by at least one entry in that collection.

The second part can't change the result here: the ids come from the parent entry, which is in that collection. The only difference shows up in Live Preview: a term that no saved entry uses yet is hidden until you save. That's arguably a bug in itself.

For the Stache, the usage filter reads an index, so it's cheap. For database-backed term repositories it means scanning the entries table. In the eloquent driver that's a JSON EXISTS per term in the taxonomy, and it runs for every Terms column on every row of the CP entries listing, and on every front-end augmentation. People with large sites report CP listings timing out after 60 s.

The change

  • Add withCollectionContext($collection) to the Stache TermQueryBuilder. It attaches the collection to the resulting terms without filtering by usage.
  • The Terms fieldtype calls it when the term query builder supports it (method_exists), and otherwise falls back to where('collection') as before.

Why this isn't breaking

  • No contract changes. where('collection') keeps its meaning for everyone else.
  • Term query builders that don't have the method (third-party repositories, older eloquent-driver releases) keep exactly today's behaviour.
  • first() (used for max_items: 1) goes through get(), so the collection is attached there too.
  • For saved entries the results are identical. In Live Preview, newly selected terms now show up straight away.
cms eloquent-driver Result
before before Today's behaviour
before after The method is never called, so no change
after before method_exists is false, so it falls back to where('collection')
after after No usage scan; URLs are still collection-scoped

Benchmarks

These use the real Terms fieldtype through the eloquent driver: 100 rows of the CP entries listing, and the Terms fields on an entry page. Each figure is the median of 3 fresh requests. The full matrix, the methodology and the benchmark script are in statamic/eloquent-driver#649.

Dataset DB Listing: today → both PRs Entry page: today → both PRs
10,000 terms, 3,000 entries MySQL 8.0 44.78 s → 314 ms 38.25 s → 7 ms
10,000 terms, 3,000 entries Postgres 18 10.24 s → 297 ms 6.43 s → 5 ms
10,000 terms, 3,000 entries SQLite 21.36 s → 237 ms 18.63 s → 4 ms
84 terms, 17,000 × 9 KB entries MySQL 8.0 1.92 s → 129 ms 6 ms → 5 ms

🤖 Generated with Claude Code

The Terms fieldtype adds where('collection', ...) so terms get collection
scoped URLs, but that also limits results to terms used in the collection.
The ids come from the parent entry, so that filter can't change the result
for saved entries, and it's expensive for database driven term repositories.

Add withCollectionContext() to the term query builder, which supplies the
collection to the terms without filtering, and use it from the fieldtype when
the query builder supports it. Other builders keep the previous behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jasonvarga
jasonvarga merged commit c05cbad into statamic:6.x Nov 12, 2026
64 checks passed
@ryanmitchell
ryanmitchell deleted the fix/terms-fieldtype-collection-context branch November 12, 2026 21:38
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