Skip to content

Globals ​

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

Globals service.

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

View source

Public Properties ​

PropertyDescription
allSetIdsarray
allSetscraft\elements\GlobalSet[]
behaviorsyii\base\Behavior – List of behaviors attached to this component.
editableSetIdsarray
editableSetscraft\elements\GlobalSet[]
totalEditableSetsinteger
totalSetsinteger

allSetIds ​

Type
array
Default value
null
Access
Read-only

View source

allSets ​

Type
craft\elements\GlobalSet[]
Default value
null
Access
Read-only

View source

editableSetIds ​

Type
array
Default value
null
Access
Read-only

View source

editableSets ​

Type
craft\elements\GlobalSet[]
Default value
null
Access
Read-only

View source

totalEditableSets ​

Type
integer
Default value
null
Access
Read-only

View source

totalSets ​

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.
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.
deleteGlobalSetById()Deletes a global set by its ID.
deleteSet()Deletes a global set 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.
getAllSetIds()Returns all of the global set IDs.
getAllSets()Returns all global sets.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getEditableSetIds()Returns all of the global set IDs that are editable by the current user.
getEditableSets()Returns all global sets that are editable by the current user.
getSetByHandle()Returns a global set by its handle.
getSetById()Returns a global set by its ID.
getTotalEditableSets()Returns the total number of global sets that are editable by the current user.
getTotalSets()Returns the total number of global sets.
handleChangedGlobalSet()Handle global set change
handleDeletedGlobalSet()Handle global set 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.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
pruneDeletedField()
reorderSets()Reorders global sets.
reset()Resets the memoized globals.
saveSet()Saves a global set.
trigger()Triggers an event.

__serialize() ​

Since
3.5.14

Serializer

View source

deleteGlobalSetById() ​

Deletes a global set by its ID.

View source

Arguments ​

Returns ​

boolean – Whether the global set was deleted successfully

Throws ​

deleteSet() ​

Since
3.6.0

Deletes a global set by its ID.

View source

Arguments ​

getAllSetIds() ​

Returns all of the global set IDs.

View source

Returns ​

array


Example ​

php
$globalSetIds = Craft::$app->globals->allSetIds;
twig
{% set globalSetIds = craft.app.globals.allSetIds %}

getAllSets() ​

Returns all global sets.

View source

Returns ​

craft\elements\GlobalSet[]


Example ​

php
$globalSets = Craft::$app->globals->allSets;
twig
{% set globalSets = craft.app.globals.allSets %}

getEditableSetIds() ​

Returns all of the global set IDs that are editable by the current user.

View source

Returns ​

array


Example ​

php
$globalSetIds = Craft::$app->globals->editableSetIds;
twig
{% set globalSetIds = craft.app.globals.editableSetIds %}

getEditableSets() ​

Returns all global sets that are editable by the current user.

View source

Returns ​

craft\elements\GlobalSet[]


Example ​

php
$globalSets = Craft::$app->globals->editableSets;
twig
{% set globalSets = craft.app.globals.editableSets %}

getSetByHandle() ​

Returns a global set by its handle.

View source

Arguments ​

Returns ​

craft\elements\GlobalSet, null


Example ​

php
$globalSet = Craft::$app->globals->getSetByHandle('footerInfo');
twig
{% set globalSet = craft.app.globals.getSetByHandle('footerInfo') %}

getSetById() ​

Returns a global set by its ID.

View source

Arguments ​

Returns ​

craft\elements\GlobalSet, null


Example ​

php
$globalSet = Craft::$app->globals->getSetById(1);
twig
{% set globalSet = craft.app.globals.getSetById(1) %}

getTotalEditableSets() ​

Returns the total number of global sets that are editable by the current user.

View source

Returns ​

integer


Example ​

php
$total = Craft::$app->globals->totalEditableSets;
twig
{% set total = craft.app.globals.totalEditableSets %}

getTotalSets() ​

Returns the total number of global sets.

View source

Returns ​

integer


Example ​

php
$total = Craft::$app->globals->totalSets;
twig
{% set total = craft.app.globals.totalSets %}

handleChangedGlobalSet() ​

Handle global set change

View source

Arguments ​

handleDeletedGlobalSet() ​

Handle global set getting deleted

View source

Arguments ​

pruneDeletedField() ​

DEPRECATED

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

View source

reorderSets() ​

Since
3.7.0

Reorders global sets.

View source

Arguments ​

Returns ​

boolean

Throws ​

reset() ​

Since
3.6.0

Resets the memoized globals.

View source

saveSet() ​

Saves a global set.

View source

Arguments ​

Returns ​

boolean

Throws ​

Constants ​

ConstantDescription
CONFIG_GLOBALSETS_KEY

Events ​

EVENT_AFTER_SAVE_GLOBAL_SET ​

Type
craft\events\GlobalSetEvent

The event that is triggered after a global set is saved.


EVENT_BEFORE_SAVE_GLOBAL_SET ​

Type
craft\events\GlobalSetEvent

The event that is triggered before a global set is saved.