delaneymethod/craft-category-groups

Two field types for Craft CMS that lets you select one or more Category Groups or one or more Categories from one or more Category Groups.

Maintainers

Package info

github.com/delaneymethod/craft-category-groups

Documentation

Type:craft-plugin

pkg:composer/delaneymethod/craft-category-groups

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2025-10-30 17:38 UTC

This package is auto-updated.

Last update: 2026-02-28 19:47:03 UTC


README

Two field types for Craft CMS that:

  • lets you select one or more Category Groups.
  • lets you select one or more Categories from one or more Category Groups.

Install

  1. Add the repo to your project and install via Composer:
cd /path/to/project

composer require delaneymethod/category-groups && php craft plugin/install category-groups

Usage

CategoryGroupField

  • In the dropdown/radio buttons mode your field normalizes to a CategoryGroup|null.
  • In checkbox mode you return an array<CategoryGroup>.

Print the selected group name(s):

{% set fieldValue = entry.myCategoryGroupsFieldHandle ?? null %}
{% if fieldValue %}
	{% if fieldValue is instance of('craft\\models\\CategoryGroup') %}
		<p>Selected category group: {{ fieldValue.name }}</p>
	{% elseif fieldValue is iterable %}
		<p>Selected category groups:</p>
		<ul>
			{% for categoryGroup in fieldValue %}
				<li>{{ categoryGroup.name }}</li>
			{% endfor %}
		</ul>
	{% elseif fieldValue is string %}
		{% set categoryGroup = craft.app.categories.getGroupByUid(fieldValue) %}
		{% if categoryGroup %}
			<p>Selected category group: {{ categoryGroup.name }}</p>
		{% endif %}
	{% else %}
		<p>No category group selected.</p>
	{% endif %}
{% else %}
	<p>No category groups selected.</p>
{% endif %}

Use the selected category group to list its categories (current site)

{% if fieldValue is instance of('craft\\models\\CategoryGroup') %}
	{% set categories = craft.categories().groupId(fieldValue.id).siteId(craft.app.sites.currentSite.id).status(null).orderBy('title asc').all() %}
	{% if categories %}
		<h4>Category group: {{ fieldValue.name }}</h4>
		<ul>
			{% for category in categories %}
				<li><a href="{{ category.url }}">{{ category.title }}</a></li>
			{% endfor %}
		</ul>
	{% else %}
		<p><em>No categories found in {{ fieldValue.name }}.</em></p>
	{% endif %}
{% elseif fieldValue is iterable %}
	{% for categoryGroup in fieldValue %}
		{% set categories = craft.categories().groupId(categoryGroup.id).siteId(craft.app.sites.currentSite.id).status(null).orderBy('title asc').all() %}
		{% if categories %}
			<h4>Category group: {{ categoryGroup.uid }}</h4>
			<ul>
				{% for category in categories %}
					<li><a href="{{ category.url }}">{{ category.title }}</a></li>
				{% endfor %}
			</ul>
		{% else %}
			<p><em>No categories found in {{ categoryGroup.name }}.</em></p>
		{% endif %}
	{% endfor %}
{% elseif fieldValue is string %}
	{% set categoryGroup = craft.app.categories.getGroupByUid(fieldValue) %}
	{% if categoryGroup %}
		{% set categories = craft.categories().groupId(categoryGroup.id).siteId(craft.app.sites.currentSite.id).status(null).orderBy('title asc').all() %}
		{% if categories %}
			<h4>Category group: {{ categoryGroup.name }}</h4>
			<ul>
				{% for category in categories %}
					<li><a href="{{ category.url }}">{{ category.title }}</a></li>
				{% endfor %}
			</ul>
		{% else %}
			<p><em>No categories found for {{ categoryGroup.name }}.</em></p>
		{% endif %}
	{% else %}
		<p><em>No category group found for {{ fieldValue }}.</em></p>
	{% endif %}
{% endif %}

CategoryGroupsField

  • Normalizes to a CategoryQuery.
  • You can iterate, count, limit, etc.

List selected categories (title + group)

{% set categoryQuery = entry.myCategoryMultipleGroupsFieldHandle ?? null %}
{% if categoryQuery %}
	{% set categories = categoryQuery.status(null).all() %}
	{% if categories %}
		<ul>
			{% for category in categories %}
				<li><a href="{{ category.url }}">{{ category.title }}</a> ({{ category.group.name }})</li>
			{% endfor %}
		</ul>
	{% else %}
		<p>No categories selected.</p>
	{% endif %}
{% else %}
	<p>No categories selected.</p>
{% endif %}

Count / limit / paginate

{% set categoryQuery = entry.myCategoryMultipleGroupsFieldHandle ?? null %}
{% if categoryQuery %}
	{% set count = categoryQuery.count() }}
	{% if count > 1 %}
		<p>{{ count }} categories selected.</p>
	{% else %}
		<p>1 category selected.</p>
	{% endif %}
	
	{% set firstFiveCategories = categoryQuery.status(null).limit(5).all() %}
	{% if firstFiveCategories %}
		<p>First five categories:</p>
		<ul>
			{% for category in firstFiveCategories %}
				<li><a href="{{ category.url }}">{{ category.title }}</a></li>
			{% endfor %}
		</ul>
	{% endif %}
{% endif %}