Skip to content

Sites ​

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

Sites service.

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

View source

Public Properties ​

PropertyDescription
allGroupscraft\models\SiteGroup[] – The site groups
allSiteIdsinteger[] – all of the site IDs
allSitescraft\models\Site[] – all of the sites
behaviorsyii\base\Behavior – List of behaviors attached to this component.
currentSitecraft\models\Site, null – the current site
editableSiteIdsinteger[] – all of the site IDs that are editable by the current user
editableSitescraft\models\Site[] – All the editable sites
hasCurrentSiteboolean
primarySitecraft\models\Site – the primary site
totalEditableSitesinteger – the total number of sites that are editable by the current user
totalSitesinteger – the total number of sites

allGroups ​

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

The site groups

View source

allSiteIds ​

Type
integer[]
Default value
null

all of the site IDs

View source

allSites ​

Type
craft\models\Site[]
Default value
null

all of the sites

View source

currentSite ​

Type
craft\models\Site, null
Default value
null

the current site

View source

editableSiteIds ​

Type
integer[]
Default value
null

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

View source

editableSites ​

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

All the editable sites

View source

hasCurrentSite ​

Type
boolean
Default value
null
Access
Read-only

View source

primarySite ​

Type
craft\models\Site
Default value
null

the primary site

View source

totalEditableSites ​

Type
integer
Default value
null

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

View source

totalSites ​

Type
integer
Default value
null

the total number of sites

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.
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.
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()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.
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()Triggers an event.

__serialize() ​

Since
3.5.14

Serializer

View source

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

Arguments ​

Returns ​

integer[] – All the sites’ IDs

getAllSites() ​

Returns all sites.

View source

Arguments ​

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

getGroupByUid() ​

Since
3.5.8

Returns a site group by its UID.

View source

Arguments ​

  • $uid (string) – The site group’s UID

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 ​

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

refreshSites() ​

Since
3.5.13

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

View source

Throws ​

reorderSites() ​

Reorders sites.

View source

Arguments ​

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

Returns ​

boolean – Whether the sites were reordered successfully

Throws ​

restoreSiteById() ​

Since
3.1.0

Restores a site by its ID.

View source

Arguments ​

  • $id (integer) – The site’s ID

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 ​

  • $site (craft\models\Site) – The site to be saved
  • $runValidation (boolean) – Whether the site should be validated

Returns ​

boolean

Throws ​

setCurrentSite() ​

Sets the current site.

View source

Arguments ​

Throws ​

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.