Categories

Type
Class
Namespace
craft\services
Inherits
craft\services\Categories » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
3.0.0

Categories service.

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

View source (opens new window)

# Public Properties

# allGroupIds

Type
integer (opens new window)[]
Default value
null
Access
Read-only

View source (opens new window)

# allGroups

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

View source (opens new window)

# editableGroupIds

Type
integer (opens new window)[]
Default value
null
Access
Read-only

View source (opens new window)

# editableGroups

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

View source (opens new window)

# totalGroups

Type
integer (opens new window)
Default value
null
Access
Read-only

View source (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of a component property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__serialize() Serializer
__set() (opens new window) Sets the value of a component property.
__unset() (opens new window) Sets a component property to be null.
applyBranchLimitToCategories() Filters an array of categories down to only <= X branches.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
deleteGroup() Deletes a category group.
deleteGroupById() Deletes a category group by its ID.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) 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() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) 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() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined for this component.
init() (opens new window) 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() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
pruneDeletedField()
pruneDeletedSite() Prune a deleted site from category group site settings.
saveGroup() Saves a category group.
trigger() (opens new window) Triggers an event.

# __serialize()

Serializer

View source (opens new window)

# 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 (opens new window)

Arguments

# deleteGroup()

Deletes a category group.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the category group was deleted successfully

# deleteGroupById()

Since
3.0.12

Deletes a category group by its ID.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – 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 (opens new window)

Arguments

# getAllGroupIds()

Returns all of the group IDs.

View source (opens new window)

Returns

integer (opens new window)[]

# getAllGroups()

Returns all category groups.

View source (opens new window)

Returns

craft\models\CategoryGroup[]

# getCategoryById()

Returns a category by its ID.

View source (opens new window)

Arguments

Returns

craft\elements\Category, null (opens new window)

# getEditableGroupIds()

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

View source (opens new window)

Returns

integer (opens new window)[]

# getEditableGroups()

Returns all editable groups.

View source (opens new window)

Returns

craft\models\CategoryGroup[]

# getGroupByHandle()

Returns a group by its handle.

View source (opens new window)

Arguments

Returns

craft\models\CategoryGroup, null (opens new window)

# getGroupById()

Returns a group by its ID.

View source (opens new window)

Arguments

Returns

craft\models\CategoryGroup, null (opens new window)

# getGroupByUid()

Since
3.1.0

Returns a group by its UID.

View source (opens new window)

Arguments

Returns

craft\models\CategoryGroup, null (opens new window)

# getGroupSiteSettings()

Returns a group's site settings.

View source (opens new window)

Arguments

Returns

craft\models\CategoryGroup_SiteSettings[]

# getTotalGroups()

Gets the total number of category groups.

View source (opens new window)

Returns

integer (opens new window)

# handleChangedCategoryGroup()

Handle category group change

View source (opens new window)

Arguments

# handleDeletedCategoryGroup()

Handle Category group getting deleted

View source (opens new window)

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 (opens new window)

Arguments

Returns

boolean (opens new window)

# pruneDeletedField()

DEPRECATED

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

View source (opens new window)

Arguments

  • $event

# pruneDeletedSite()

Prune a deleted site from category group site settings.

View source (opens new window)

Arguments

# saveGroup()

Saves a category group.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the category group was saved successfully

Throws

# Constants

Constant Description
CONFIG_CATEGORYROUP_KEY

# 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.