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->getSites()
.
Public Properties ​
Property | Description |
---|---|
allGroups | craft\models\SiteGroup[] – The site groups |
allSiteIds | integer[] – all of the site IDs |
allSites | craft\models\Site[] – all of the sites |
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
currentSite | craft\models\Site, null – the current site |
editableSiteIds | integer[] – all of the site IDs that are editable by the current user |
editableSites | craft\models\Site[] – All the editable sites |
hasCurrentSite | boolean |
maxSites | integer – The maximum number of sites that can be created. |
primarySite | craft\models\Site – the primary site |
remainingSites | integer |
totalEditableSites | integer – the total number of sites that are editable by the current user |
totalSites | integer – the total number of sites |
allGroups
​
- Type
- craft\models\SiteGroup[]
- Default value
null
- Access
- Read-only
The site groups
allSiteIds
​
- Type
- integer[]
- Default value
null
all of the site IDs
allSites
​
- Type
- craft\models\Site[]
- Default value
null
all of the sites
currentSite
​
- Type
- craft\models\Site, null
- Default value
null
the current site
editableSiteIds
​
- Type
- integer[]
- Default value
null
all of the site IDs that are editable by the current user
editableSites
​
- Type
- craft\models\Site[]
- Default value
null
- Access
- Read-only
All the editable sites
hasCurrentSite
​
- Type
- boolean
- Default value
null
- Access
- Read-only
maxSites
​
- Type
- integer
- Default value
100
- Since
- 5.0.0
The maximum number of sites that can be created.
primarySite
​
- Type
- craft\models\Site
- Default value
null
the primary site
remainingSites
​
- Type
- integer
- Default value
null
- Access
- Read-only
- Since
- 5.0.0
totalEditableSites
​
- Type
- integer
- Default value
null
the total number of sites that are editable by the current user
totalSites
​
- Type
- integer
- Default value
null
the total number of sites
Public Methods ​
Method | Description |
---|---|
__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. |
getEditableSitesByGroupId() | Returns editable sites by a group ID. |
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. |
getRemainingSites() | Returns the number of sites that can be created, based on maxSites. |
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. |
getSitesByLanguage() | Returns sites by their language. |
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
deleteGroup()
​
Deletes a site group.
Arguments ​
$group
(craft\models\SiteGroup) – The site group
Returns ​
boolean – Whether the site group was deleted successfully
deleteGroupById()
​
Deletes a site group by its ID.
Arguments ​
$groupId
(integer) – The site group’s ID
Returns ​
boolean – Whether the site group was deleted successfully
deleteSite()
​
Deletes a site.
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 ​
- yii\base\Exception
if $site is the primary site - Throwable
if reasons
deleteSiteById()
​
Deletes a site by its ID.
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 ​
- Throwable
if reasons
getAllGroups()
​
Returns all site groups.
Returns ​
craft\models\SiteGroup[] – The site groups
getAllSiteIds()
​
Returns all of the site IDs.
Arguments ​
Returns ​
integer[] – All the sites’ IDs
getAllSites()
​
Returns all sites.
Arguments ​
Returns ​
craft\models\Site[] – All the sites
getCurrentSite()
​
Returns the current site.
NOTE
This will always return the primary site for control panel requests. To fetch the site the control panel is currently working with, based on the site
query string param, use craft\helpers\Cp::requestedSite().
Returns ​
craft\models\Site – The current site
Throws ​
- craft\errors\SiteNotFoundException
if no sites exist
getEditableSiteIds()
​
Returns all of the site IDs that are editable by the current user.
Returns ​
array – All the editable sites’ IDs
getEditableSites()
​
Returns all editable sites.
Returns ​
craft\models\Site[] – All the editable sites
getEditableSitesByGroupId()
​
- Since
- 5.4.0
Returns editable sites by a group ID.
Arguments ​
Returns ​
getGroupById()
​
Returns a site group by its ID.
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.
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.
Returns ​
getPrimarySite()
​
Returns the primary site. The primary site is whatever is listed first in Settings > Sites in the control panel.
Returns ​
craft\models\Site – The primary site
Throws ​
- craft\errors\SiteNotFoundException
if no sites exist
getRemainingSites()
​
- Since
- 5.0.0
Returns the number of sites that can be created, based on maxSites.
Returns ​
getSiteByHandle()
​
Returns a site by its handle.
Arguments ​
Returns ​
getSiteById()
​
Returns a site by its ID.
Arguments ​
Returns ​
getSiteByUid()
​
Returns a site by it's UID.
Arguments ​
Returns ​
craft\models\Site – The site
Throws ​
- craft\errors\SiteNotFoundException
if no sites exist
getSitesByGroupId()
​
Returns sites by a group ID.
Arguments ​
Returns ​
getSitesByLanguage()
​
- Since
- 4.9.0
Returns sites by their language.
Arguments ​
Returns ​
getTotalEditableSites()
​
Gets the total number of sites that are editable by the current user.
Returns ​
getTotalSites()
​
Gets the total number of sites.
Returns ​
handleChangedGroup()
​
Handle site group change
Arguments ​
$event
(craft\events\ConfigEvent)
handleChangedSite()
​
Handle site changes.
Arguments ​
$event
(craft\events\ConfigEvent)
Throws ​
handleDeletedGroup()
​
Handle site group getting deleted.
Arguments ​
$event
(craft\events\ConfigEvent)
handleDeletedSite()
​
Handle a deleted Site.
Arguments ​
$event
(craft\events\ConfigEvent)
Throws ​
init()
​
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
refreshSites()
​
- Since
- 3.5.13
Refresh the status of all sites based on the DB data.
Throws ​
reorderSites()
​
Reorders sites.
Arguments ​
$siteIds
(integer[]) – The site IDs in their new order
Returns ​
boolean – Whether the sites were reordered successfully
Throws ​
- Throwable
if reasons
restoreSiteById()
​
- Since
- 3.1.0
Restores a site by its ID.
Arguments ​
$id
(integer) – The site’s ID
Returns ​
boolean – Whether the site was restored successfully
saveGroup()
​
Saves a site group.
Arguments ​
$group
(craft\models\SiteGroup) – The site group to be saved$runValidation
(boolean) – Whether the group should be validated
Returns ​
boolean – Whether the site group was saved successfully
saveSite()
​
Saves a site.
Arguments ​
$site
(craft\models\Site) – The site to be saved$runValidation
(boolean) – Whether the site should be validated
Returns ​
Throws ​
- craft\errors\SiteNotFoundException
if $site->id is invalid - Throwable
if reasons
setCurrentSite()
​
Sets the current site.
Arguments ​
$site
(craft\models\Site, string, integer, null) – The current site, or its handle/ID, or null
Throws ​
- yii\base\InvalidArgumentException
if $site is invalid
Events ​
EVENT_AFTER_CHANGE_PRIMARY_SITE ​
The event that is triggered after the primary site has changed
EVENT_AFTER_DELETE_SITE ​
The event that is triggered after a site is deleted.
EVENT_AFTER_DELETE_SITE_GROUP ​
The event that is triggered after a site group is deleted.
EVENT_AFTER_REORDER_SITES ​
The event that is triggered after the sites are reordered.
EVENT_AFTER_SAVE_SITE ​
The event that is triggered after a site is saved.
EVENT_AFTER_SAVE_SITE_GROUP ​
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 ​
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 ​
The event that is triggered before a site group is deleted.
EVENT_BEFORE_REORDER_SITES ​
The event that is triggered before the sites are reordered.
EVENT_BEFORE_SAVE_SITE ​
The event that is triggered before a site is saved.
EVENT_BEFORE_SAVE_SITE_GROUP ​
The event that is triggered before a site group is saved.