Categories

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

Categories service.

An instance of the Categories service is globally accessible in Craft via Craft::$app->categories.

View source

Public Properties

$allGroupIds

Type
integer[]
Access
Read-only

View source

Signature

public integer[] getAllGroupIds ( )

$allGroups

Signature

public craft\models\CategoryGroup[] getAllGroups ( )

$editableGroupIds

Type
integer[]
Access
Read-only

View source

Signature

public integer[] getEditableGroupIds ( )

$editableGroups

Signature

public craft\models\CategoryGroup[] getEditableGroups ( )

$totalGroups

Type
integer
Access
Read-only

View source

Signature

public integer getTotalGroups ( )

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 an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__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.
getGroupSiteSettings()Returns a group's site settings.
getTotalGroups()Gets the total number of category groups.
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.
saveGroup()Saves a category group.
trigger()Triggers an event.

applyBranchLimitToCategories()

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

View source

Arguments

Signature

public void applyBranchLimitToCategories ( array &$categories, \craft\services\int $branchLimit )

deleteGroup()

Deletes a category group.

View source

Arguments

Returns

boolean – Whether the category group was deleted successfully

Signature

public boolean deleteGroup ( craft\models\CategoryGroup $group )

deleteGroupById()

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

Signature

public boolean deleteGroupById ( \craft\services\int $groupId )

fillGapsInCategories()

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

View source

Arguments

Signature

public void fillGapsInCategories ( array &$categories )

getAllGroupIds()

Returns all of the group IDs.

View source

Returns

integer[]

Signature

public integer[] getAllGroupIds ( )

getAllGroups()

Returns all category groups.

View source

Returns

craft\models\CategoryGroup[]

Signature

public craft\models\CategoryGroup[] getAllGroups ( )

getCategoryById()

Returns a category by its ID.

View source

Arguments

Returns

craft\elements\Category, null

Signature

public craft\elements\Category, null getCategoryById ( \craft\services\int $categoryId, \craft\services\int $siteId = null )

getEditableGroupIds()

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

View source

Returns

integer[]

Signature

public integer[] getEditableGroupIds ( )

getEditableGroups()

Returns all editable groups.

View source

Returns

craft\models\CategoryGroup[]

Signature

public craft\models\CategoryGroup[] getEditableGroups ( )

getGroupByHandle()

Returns a group by its handle.

View source

Arguments

Returns

craft\models\CategoryGroup, null

Signature

public craft\models\CategoryGroup, null getGroupByHandle ( \craft\services\string $groupHandle )

getGroupById()

Returns a group by its ID.

View source

Arguments

Returns

craft\models\CategoryGroup, null

Signature

public craft\models\CategoryGroup, null getGroupById ( \craft\services\int $groupId )

getGroupSiteSettings()

Returns a group's site settings.

View source

Arguments

Returns

craft\models\CategoryGroup_SiteSettings[]

Signature

public craft\models\CategoryGroup_SiteSettings[] getGroupSiteSettings ( \craft\services\int $groupId )

getTotalGroups()

Gets the total number of category groups.

View source

Returns

integer

Signature

public integer getTotalGroups ( )

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

Signature

public boolean isGroupTemplateValid ( craft\models\CategoryGroup $group, \craft\services\int $siteId )

saveGroup()

Saves a category group.

View source

Arguments

Returns

boolean – Whether the category group was saved successfully

Throws

Signature

public boolean saveGroup ( craft\models\CategoryGroup $group, \craft\services\bool $runValidation = true )

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