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 Categories service is globally accessible in Craft via Craft::$app->categories
.
# Public Properties
Property | Description |
---|---|
allGroupIds | integer[] |
allGroups | craft\models\CategoryGroup[] |
behaviors | yii\base\Behavior – List of behaviors attached to this component |
editableGroupIds | integer[] |
editableGroups | craft\models\CategoryGroup[] |
totalGroups | integer |
# allGroupIds
- Type
- integer[]
- Default value
null
- Access
- Read-only
# allGroups
- Type
- craft\models\CategoryGroup[]
- Default value
null
- Access
- Read-only
# editableGroupIds
- Type
- integer[]
- Default value
null
- Access
- Read-only
# editableGroups
- Type
- craft\models\CategoryGroup[]
- Default value
null
- Access
- Read-only
# totalGroups
- Type
- integer
- Default value
null
- Access
- Read-only
# Public Methods
Method | Description |
---|---|
__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 an object property. |
__isset() | Checks if a property is set, i.e. defined and not null. |
__serialize() | Serializer |
__set() | Sets value of an object property. |
__unset() | Sets an object property to 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. |
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() | Prune a deleted field from category group layouts. |
pruneDeletedSite() | Prune a deleted site from category group site settings. |
saveGroup() | Saves a category group. |
trigger() | Triggers an event. |
# __serialize()
Serializer
# applyBranchLimitToCategories()
Filters an array of categories down to only <= X branches.
Arguments
$categories
(craft\elements\Category[])$branchLimit
(integer)
# deleteGroup()
Deletes a category group.
Arguments
$group
(craft\models\CategoryGroup) – The category group
Returns
boolean – Whether the category group was deleted successfully
# deleteGroupById()
- Since
- 3.0.12
Deletes a category group by its ID.
Arguments
$groupId
(integer) – The category group's ID
Returns
boolean – Whether the category group was deleted successfully
Throws
- Throwable
if reasons
# fillGapsInCategories()
Patches an array of categories, filling in any gaps in the tree.
Arguments
$categories
(craft\elements\Category[])
# getAllGroupIds()
Returns all of the group IDs.
Returns
integer[]
# getAllGroups()
Returns all category groups.
Returns
# getCategoryById()
Returns a category by its ID.
Arguments
Returns
# getEditableGroupIds()
Returns all of the category group IDs that are editable by the current user.
Returns
integer[]
# getEditableGroups()
Returns all editable groups.
Returns
# getGroupByHandle()
Returns a group by its handle.
Arguments
$groupHandle
(string)
Returns
craft\models\CategoryGroup, null
# getGroupById()
Returns a group by its ID.
Arguments
$groupId
(integer)
Returns
craft\models\CategoryGroup, null
# getGroupByUid()
- Since
- 3.1.0
Returns a group by its UID.
Arguments
$uid
(string)
Returns
craft\models\CategoryGroup, null
# getGroupSiteSettings()
Returns a group's site settings.
Arguments
$groupId
(integer)
Returns
craft\models\CategoryGroup_SiteSettings[]
# getTotalGroups()
Gets the total number of category groups.
Returns
# handleChangedCategoryGroup()
Handle category group change
Arguments
$event
(craft\events\ConfigEvent)
# handleDeletedCategoryGroup()
Handle Category group getting deleted
Arguments
$event
(craft\events\ConfigEvent)
# isGroupTemplateValid()
Returns whether a group’s categories have URLs for the given site ID, and if the group’s template path is valid.
Arguments
$group
(craft\models\CategoryGroup)$siteId
(integer)
Returns
# pruneDeletedField()
Prune a deleted field from category group layouts.
Arguments
$event
(craft\events\FieldEvent)
# pruneDeletedSite()
Prune a deleted site from category group site settings.
Arguments
$event
(craft\events\DeleteSiteEvent)
# saveGroup()
Saves a category group.
Arguments
$group
(craft\models\CategoryGroup) – The category group to be saved$runValidation
(boolean) – Whether the category group should be validated
Returns
boolean – Whether the category group was saved successfully
Throws
- craft\errors\CategoryGroupNotFoundException
if $group has an invalid ID - Throwable
if reasons
# Constants
Constant | Description |
---|---|
CONFIG_CATEGORYROUP_KEY |
# Events
# EVENT_AFTER_DELETE_GROUP
The event that is triggered after a category group is deleted.
# EVENT_AFTER_SAVE_GROUP
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
The event that is triggered before a category group is deleted.
# EVENT_BEFORE_SAVE_GROUP
The event that is triggered before a category group is saved.