Sites

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

Sites service.

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

View source (opens new window)

# Public Properties

Property Description
allGroups craft\models\SiteGroup[] – The site groups
allSiteIds integer (opens new window)[] – all of the site IDs
allSites craft\models\Site[] – all of the sites
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component.
currentSite craft\models\Site, null (opens new window) – the current site
editableSiteIds integer (opens new window)[] – all of the site IDs that are editable by the current user
editableSites craft\models\Site[] – All the editable sites
hasCurrentSite boolean (opens new window)
primarySite craft\models\Site – the primary site
totalEditableSites integer (opens new window) – the total number of sites that are editable by the current user
totalSites integer (opens new window) – the total number of sites

# allGroups

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

The site groups

View source (opens new window)

# allSiteIds

Type
integer (opens new window)[]
Default value
null

all of the site IDs

View source (opens new window)

# allSites

Type
craft\models\Site[]
Default value
null

all of the sites

View source (opens new window)

# currentSite

Type
craft\models\Site, null (opens new window)
Default value
null

the current site

View source (opens new window)

# editableSiteIds

Type
integer (opens new window)[]
Default value
null

all of the site IDs that are editable by the current user

View source (opens new window)

# editableSites

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

All the editable sites

View source (opens new window)

# hasCurrentSite

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

View source (opens new window)

# primarySite

Type
craft\models\Site
Default value
null

the primary site

View source (opens new window)

# totalEditableSites

Type
integer (opens new window)
Default value
null

the total number of sites that are editable by the current user

View source (opens new window)

# totalSites

Type
integer (opens new window)
Default value
null

the total number of sites

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.
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 site group.
deleteGroupById() Deletes a site group by its ID.
deleteSite() Deletes a site.
deleteSiteById() Deletes a site 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.
getAllGroups() Returns all site groups.
getAllSiteIds() Returns all of the site IDs.
getAllSites() Returns all sites.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getCurrentSite() Returns the current site.
getEditableSiteIds() Returns all of the site IDs that are editable by the current user.
getEditableSites() Returns all editable sites.
getGroupById() Returns a site group by its ID.
getGroupByUid() Returns a site group by its UID.
getHasCurrentSite() Returns whether the current site has been set yet.
getPrimarySite() Returns the primary site. The primary site is whatever is listed first in Settings > Sites in the control panel.
getSiteByHandle() Returns a site by its handle.
getSiteById() Returns a site by its ID.
getSiteByUid() Returns a site by it's UID.
getSitesByGroupId() Returns sites by a group ID.
getTotalEditableSites() Gets the total number of sites that are editable by the current user.
getTotalSites() Gets the total number of sites.
handleChangedGroup() Handle site group change
handleChangedSite() Handle site changes.
handleDeletedGroup() Handle site group getting deleted.
handleDeletedSite() Handle a deleted Site.
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() Initializes the object.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
refreshSites() Refresh the status of all sites based on the DB data.
reorderSites() Reorders sites.
restoreSiteById() Restores a site by its ID.
saveGroup() Saves a site group.
saveSite() Saves a site.
setCurrentSite() Sets the current site.
trigger() (opens new window) Triggers an event.

# __serialize()

Since
3.5.14

Serializer

View source (opens new window)

# deleteGroup()

Deletes a site group.

View source (opens new window)

Arguments

Returns

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

# deleteGroupById()

Deletes a site group by its ID.

View source (opens new window)

Arguments

Returns

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

# deleteSite()

Deletes a site.

View source (opens new window)

Arguments

Returns

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

Throws

# deleteSiteById()

Deletes a site by its ID.

View source (opens new window)

Arguments

Returns

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

Throws

# getAllGroups()

Returns all site groups.

View source (opens new window)

Returns

craft\models\SiteGroup[] – The site groups

# getAllSiteIds()

Returns all of the site IDs.

View source (opens new window)

Arguments

Returns

integer (opens new window)[] – All the sites’ IDs

# getAllSites()

Returns all sites.

View source (opens new window)

Arguments

Returns

craft\models\Site[] – All the sites

# getCurrentSite()

Returns the current site.

View source (opens new window)

Returns

craft\models\Site – The current site

Throws

# getEditableSiteIds()

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

View source (opens new window)

Returns

array (opens new window) – All the editable sites’ IDs

# getEditableSites()

Returns all editable sites.

View source (opens new window)

Returns

craft\models\Site[] – All the editable sites

# getGroupById()

Returns a site group by its ID.

View source (opens new window)

Arguments

Returns

craft\models\SiteGroup, null (opens new window) – The site group, or null if it doesn’t exist

# getGroupByUid()

Since
3.5.8

Returns a site group by its UID.

View source (opens new window)

Arguments

Returns

craft\models\SiteGroup, null (opens new window) – The site group, or null if it doesn’t exist

# getHasCurrentSite()

Returns whether the current site has been set yet.

View source (opens new window)

Returns

boolean (opens new window)

# getPrimarySite()

Returns the primary site. The primary site is whatever is listed first in Settings > Sites in the control panel.

View source (opens new window)

Returns

craft\models\Site – The primary site

Throws

# getSiteByHandle()

Returns a site by its handle.

View source (opens new window)

Arguments

Returns

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

# getSiteById()

Returns a site by its ID.

View source (opens new window)

Arguments

Returns

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

# getSiteByUid()

Returns a site by it's UID.

View source (opens new window)

Arguments

Returns

craft\models\Site – The site

Throws

# getSitesByGroupId()

Returns sites by a group ID.

View source (opens new window)

Arguments

Returns

craft\models\Site[]

# getTotalEditableSites()

Gets the total number of sites that are editable by the current user.

View source (opens new window)

Returns

integer (opens new window)

# getTotalSites()

Gets the total number of sites.

View source (opens new window)

Returns

integer (opens new window)

# handleChangedGroup()

Handle site group change

View source (opens new window)

Arguments

# handleChangedSite()

Handle site changes.

View source (opens new window)

Arguments

Throws

# handleDeletedGroup()

Handle site group getting deleted.

View source (opens new window)

Arguments

# handleDeletedSite()

Handle a deleted Site.

View source (opens new window)

Arguments

Throws

# init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

View source (opens new window)

# refreshSites()

Since
3.5.13

Refresh the status of all sites based on the DB data.

View source (opens new window)

Throws

# reorderSites()

Reorders sites.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the sites were reordered successfully

Throws

# restoreSiteById()

Since
3.1.0

Restores a site by its ID.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the site was restored successfully

# saveGroup()

Saves a site group.

View source (opens new window)

Arguments

Returns

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

# saveSite()

Saves a site.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

Throws

# setCurrentSite()

Sets the current site.

View source (opens new window)

Arguments

Throws

# Constants

Constant Description
CONFIG_SITEGROUP_KEY
CONFIG_SITES_KEY

# Events

# EVENT_AFTER_CHANGE_PRIMARY_SITE

Type
craft\events\SiteEvent

The event that is triggered after the primary site has changed

# EVENT_AFTER_DELETE_SITE

Type
craft\events\DeleteSiteEvent

The event that is triggered after a site is deleted.

# EVENT_AFTER_DELETE_SITE_GROUP

Type
craft\events\SiteGroupEvent

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

# EVENT_AFTER_REORDER_SITES

Type
craft\events\ReorderSitesEvent

The event that is triggered after the sites are reordered.

# EVENT_AFTER_SAVE_SITE

Type
craft\events\SiteEvent

The event that is triggered after a site is saved.

# EVENT_AFTER_SAVE_SITE_GROUP

Type
craft\events\SiteGroupEvent

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

# EVENT_BEFORE_APPLY_GROUP_DELETE

Type
craft\events\SiteGroupEvent
Since
3.1.0

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

# EVENT_BEFORE_APPLY_SITE_DELETE

Type
craft\events\DeleteSiteEvent
Since
3.1.0

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

# EVENT_BEFORE_DELETE_SITE

Type
craft\events\DeleteSiteEvent

The event that is triggered before a site is deleted.

You may set craft\events\CancelableEvent::$isValid to false to prevent the site from getting deleted.

# EVENT_BEFORE_DELETE_SITE_GROUP

Type
craft\events\SiteGroupEvent

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

# EVENT_BEFORE_REORDER_SITES

Type
craft\events\ReorderSitesEvent

The event that is triggered before the sites are reordered.

# EVENT_BEFORE_SAVE_SITE

Type
craft\events\SiteEvent

The event that is triggered before a site is saved.

# EVENT_BEFORE_SAVE_SITE_GROUP

Type
craft\events\SiteGroupEvent

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