Skip to content

Categories ​

Type
Class
Namespace
craft\services
Inherits
craft\services\Categories » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0.0

Categories service.

An instance of the service is available via Craft::$app->categories.

View source

Public Properties ​

PropertyDescription
allGroupIdsinteger[]
allGroupscraft\models\CategoryGroup[]
behaviorsyii\base\Behavior – List of behaviors attached to this component.
editableGroupIdsinteger[]
editableGroupscraft\models\CategoryGroup[]
totalGroupsinteger

allGroupIds ​

Type
integer[]
Default value
null
Access
Read-only

View source

allGroups ​

Type
craft\models\CategoryGroup[]
Default value
null
Access
Read-only

View source

editableGroupIds ​

Type
integer[]
Default value
null
Access
Read-only

View source

editableGroups ​

Type
craft\models\CategoryGroup[]
Default value
null
Access
Read-only

View source

totalGroups ​

Type
integer
Default value
null
Access
Read-only

View source

Public Methods ​

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of a component property.
__isset()Checks if a property is set, i.e. defined and not null.
__serialize()Serializer
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
applyBranchLimitToCategories()Filters an array of categories down to only <= X branches.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
deleteGroup()Deletes a category group.
deleteGroupById()Deletes a category group by its ID.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
fillGapsInCategories()Patches an array of categories, filling in any gaps in the tree.
getAllGroupIds()Returns all of the group IDs.
getAllGroups()Returns all category groups.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getCategoryById()Returns a category by its ID.
getEditableGroupIds()Returns all of the category group IDs that are editable by the current user.
getEditableGroups()Returns all editable groups.
getGroupByHandle()Returns a group by its handle.
getGroupById()Returns a group by its ID.
getGroupByUid()Returns a group by its UID.
getGroupSiteSettings()Returns a group's site settings.
getTotalGroups()Gets the total number of category groups.
handleChangedCategoryGroup()Handle category group change
handleDeletedCategoryGroup()Handle Category group getting deleted
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined for this component.
init()Initializes the object.
isGroupTemplateValid()Returns whether a group’s categories have URLs for the given site ID, and if the group’s template path is valid.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
pruneDeletedField()
pruneDeletedSite()Prune a deleted site from category group site settings.
saveGroup()Saves a category group.
trigger()Triggers an event.

__serialize() ​

Serializer

View source

applyBranchLimitToCategories() ​

DEPRECATED

Deprecated in 3.6.0. Use craft\services\Structures::applyBranchLimitToElements() instead.

Filters an array of categories down to only <= X branches.

View source

Arguments ​

deleteGroup() ​

Deletes a category group.

View source

Arguments ​

Returns ​

boolean – Whether the category group was deleted successfully

deleteGroupById() ​

Since
3.0.12

Deletes a category group by its ID.

View source

Arguments ​

  • $groupId (integer) – The category group's ID

Returns ​

boolean – Whether the category group was deleted successfully

Throws ​

fillGapsInCategories() ​

DEPRECATED

Deprecated in 3.6.0. Use craft\services\Structures::fillGapsInElements() instead.

Patches an array of categories, filling in any gaps in the tree.

View source

Arguments ​

getAllGroupIds() ​

Returns all of the group IDs.

View source

Returns ​

integer[]

getAllGroups() ​

Returns all category groups.

View source

Returns ​

craft\models\CategoryGroup[]

getCategoryById() ​

Returns a category by its ID.

View source

Arguments ​

Returns ​

craft\elements\Category, null

getEditableGroupIds() ​

Returns all of the category group IDs that are editable by the current user.

View source

Returns ​

integer[]

getEditableGroups() ​

Returns all editable groups.

View source

Returns ​

craft\models\CategoryGroup[]

getGroupByHandle() ​

Returns a group by its handle.

View source

Arguments ​

Returns ​

craft\models\CategoryGroup, null

getGroupById() ​

Returns a group by its ID.

View source

Arguments ​

Returns ​

craft\models\CategoryGroup, null

getGroupByUid() ​

Since
3.1.0

Returns a group by its UID.

View source

Arguments ​

Returns ​

craft\models\CategoryGroup, null

getGroupSiteSettings() ​

Returns a group's site settings.

View source

Arguments ​

Returns ​

craft\models\CategoryGroup_SiteSettings[]

getTotalGroups() ​

Gets the total number of category groups.

View source

Returns ​

integer

handleChangedCategoryGroup() ​

Handle category group change

View source

Arguments ​

handleDeletedCategoryGroup() ​

Handle Category group getting deleted

View source

Arguments ​

isGroupTemplateValid() ​

Returns whether a group’s categories have URLs for the given site ID, and if the group’s template path is valid.

View source

Arguments ​

Returns ​

boolean

pruneDeletedField() ​

DEPRECATED

Deprecated in 4.0.5. Unused fields will be pruned automatically as field layouts are resaved.

View source

pruneDeletedSite() ​

Prune a deleted site from category group site settings.

View source

Arguments ​

saveGroup() ​

Saves a category group.

View source

Arguments ​

Returns ​

boolean – Whether the category group was saved successfully

Throws ​

Events ​

EVENT_AFTER_DELETE_GROUP ​

Type
craft\events\CategoryGroupEvent

The event that is triggered after a category group is deleted.


EVENT_AFTER_SAVE_GROUP ​

Type
craft\events\CategoryGroupEvent

The event that is triggered after a category group is saved.


EVENT_BEFORE_APPLY_GROUP_DELETE ​

Type
craft\events\CategoryGroupEvent
Since
3.1.0

The event that is triggered before a category group delete is applied to the database.


EVENT_BEFORE_DELETE_GROUP ​

Type
craft\events\CategoryGroupEvent

The event that is triggered before a category group is deleted.


EVENT_BEFORE_SAVE_GROUP ​

Type
craft\events\CategoryGroupEvent

The event that is triggered before a category group is saved.