Sites

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

Sites service.

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

View source

Public Properties

PropertyDescription
allGroupscraft\models\SiteGroup[] – The site groups
allSiteIdsinteger[] – All the sites’ IDs
allSitescraft\models\Site[] – All the sites
behaviorsyii\base\Behavior – List of behaviors attached to this component
currentSitecraft\models\Site – The current site
editableSiteIdsarray – All the editable sites’ IDs
editableSitescraft\models\Site[] – All the editable sites
hasCurrentSiteboolean
primarySitecraft\models\Site – The primary site
totalEditableSitesinteger
totalSitesinteger

allGroups

Type
craft\models\SiteGroup[]
Access
Read-only

The site groups

View source

allSiteIds

Type
integer[]
Access
Read-only

All the sites’ IDs

View source

allSites

Type
craft\models\Site[]
Access
Read-only

All the sites

View source

currentSite

Type
craft\models\Site

The current site

View source

editableSiteIds

Type
array
Access
Read-only

All the editable sites’ IDs

View source

editableSites

Type
craft\models\Site[]
Access
Read-only

All the editable sites

View source

hasCurrentSite

Type
boolean
Access
Read-only

View source

primarySite

Type
craft\models\Site
Access
Read-only

The primary site

View source

totalEditableSites

Type
integer
Access
Read-only

View source

totalSites

Type
integer
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 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.
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 site group.
deleteGroupById()Deletes a site group by its ID.
deleteSite()Deletes a site.
deleteSiteById()Deletes a site 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.
getAllGroups()Returns all site groups.
getAllSiteIds()Returns all of the site IDs.
getAllSites()Returns all sites.
getBehavior()Returns the named behavior object.
getBehaviors()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.
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()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.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
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()Triggers an event.

deleteGroup()

Deletes a site group.

View source

Arguments

Returns

boolean – Whether the site group was deleted successfully

deleteGroupById()

Deletes a site group by its ID.

View source

Arguments

  • $groupId (integer) – The site group’s ID

Returns

boolean – Whether the site group was deleted successfully

deleteSite()

Deletes a site.

View source

Arguments

  • $site (craft\models\Site) – The site to be deleted
  • $transferContentTo (integer, null) – The site ID that should take over the deleted site’s contents

Returns

boolean – Whether the site was deleted successfully

Throws

deleteSiteById()

Deletes a site by its ID.

View source

Arguments

  • $siteId (integer) – The site ID to be deleted
  • $transferContentTo (integer, null) – The site ID that should take over the deleted site’s contents

Returns

boolean – Whether the site was deleted successfully

Throws

getAllGroups()

Returns all site groups.

View source

Returns

craft\models\SiteGroup[] – The site groups

getAllSiteIds()

Returns all of the site IDs.

View source

Returns

integer[] – All the sites’ IDs

getAllSites()

Returns all sites.

View source

Returns

craft\models\Site[] – All the sites

getCurrentSite()

Returns the current site.

View source

Returns

craft\models\Site – The current site

Throws

getEditableSiteIds()

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

View source

Returns

array – All the editable sites’ IDs

getEditableSites()

Returns all editable sites.

View source

Returns

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

getGroupById()

Returns a site group by its ID.

View source

Arguments

  • $groupId (integer) – The site group’s ID

Returns

craft\models\SiteGroup, null – The site group, or null if it doesn’t exist

getHasCurrentSite()

Returns whether the current site has been set yet.

View source

Returns

boolean

getPrimarySite()

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

View source

Returns

craft\models\Site – The primary site

Throws

getSiteByHandle()

Returns a site by its handle.

View source

Arguments

Returns

craft\models\Site, null

getSiteById()

Returns a site by its ID.

View source

Arguments

Returns

craft\models\Site, null

getSiteByUid()

Returns a site by it's UID.

View source

Arguments

  • $uid

Returns

craft\models\Site – The site

Throws

getSitesByGroupId()

Returns sites by a group ID.

View source

Arguments

Returns

craft\models\Site[]

getTotalEditableSites()

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

View source

Returns

integer

getTotalSites()

Gets the total number of sites.

View source

Returns

integer

handleChangedGroup()

Handle site group change

View source

Arguments

handleChangedSite()

Handle site changes.

View source

Arguments

Throws

handleDeletedGroup()

Handle site group getting deleted.

View source

Arguments

handleDeletedSite()

Handle a deleted Site.

View source

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

reorderSites()

Reorders sites.

View source

Arguments

  • $siteIds (string[]) – The site IDs in their new order

Returns

boolean – Whether the sites were reordered successfully

Throws

restoreSiteById()

Restores a site by its ID.

View source

Arguments

Returns

boolean – Whether the site was restored successfully

saveGroup()

Saves a site group.

View source

Arguments

Returns

boolean – Whether the site group was saved successfully

saveSite()

Saves a site.

View source

Arguments

Returns

boolean

Throws

setCurrentSite()

Sets the current site.

View source

Arguments

Throws

Constants

ConstantDescription
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

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

EVENT_BEFORE_APPLY_SITE_DELETE

Type
craft\events\DeleteSiteEvent

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\SiteEvent::$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.