Mainstay GitHub
All docs

Tags and taxonomies

Terms that group entries, the field that holds them, and finding the entries filed under one.

A taxonomy is a type whose entries are terms. A term is an entry in every way, with its tables, its route, its view and url(); what makes it a term is that a terms field points at it and a read filters by it. Taxonomies are flat.

Declare one

#[Route('/blog/tag/{slug}')]
class Tag extends Taxonomy
{
    #[Text(required: true, localized: true)]
    public string $slug;
}

Register it with your other types. Then give entries a field holding its terms, in the order given:

#[Terms(of: Tag::class)]
public array $tags;

Where terms are kept

Not in a column: in rows of the taxonomy's pivot table, tag_entries, which is what lets a read ask the reverse question. The pivot has no locale, so an entry has the same terms in every language, and each term is translated itself. A terms field is therefore never localized, and only sits on an entry, not inside a block or on a global.

The entries holding a term

A term's page lists what is filed under it:

$articles = Mainstay::paginate(Article::class,
    where: ['tags' => $entry->id],
    sort: '-publishedOn',
    perPage: 10,
);

= finds the entries holding one term and in those holding any of several, ['tags' => ['in' => [3, 5]]]. A term in the trash gathers nothing while it is there. Terms are not sorted on.

In the admin

A terms field is a tag input: pick a term, or type a new one and it is made when the entry is saved. Terms have one capability per taxonomy, manage_tags here, and manage_terms covers every taxonomy.