Class craft\services\Sites
- Inheritance
- craft\services\Sites » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable
- Available since version
- 3.0
- Source Code
- https://github.com/craftcms/cms/blob/master/src/services/Sites.php
Sites service.
An instance of the Sites service is globally accessible in Craft via Craft::$app->sites
.
Method | Description | Defined By |
---|---|---|
__call() |
Calls the named method which is not a class method. | yii\base\BaseObject |
__clone() |
This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() |
Constructor. | yii\base\BaseObject |
__get() |
Returns the value of an object property. | yii\base\BaseObject |
__isset() |
Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
__set() |
Sets value of an object property. | yii\base\BaseObject |
__unset() |
Sets an object property to null. | yii\base\BaseObject |
attachBehavior() |
Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() |
Attaches a list of behaviors to the component. | yii\base\Component |
behaviors() |
Returns a list of behaviors that this component should behave as. | yii\base\Component |
canGetProperty() |
Returns a value indicating whether a property can be read. | yii\base\BaseObject |
canSetProperty() |
Returns a value indicating whether a property can be set. | yii\base\BaseObject |
className() |
Returns the fully qualified name of this class. | yii\base\BaseObject |
deleteGroup() |
Deletes a site group. | craft\services\Sites |
deleteGroupById() |
Deletes a site group by its ID. | craft\services\Sites |
deleteSite() |
Deletes a site. | craft\services\Sites |
deleteSiteById() |
Deletes a site by its ID. | craft\services\Sites |
detachBehavior() |
Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() |
Detaches all behaviors from the component. | yii\base\Component |
ensureBehaviors() |
Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
getAllGroups() |
Returns all site groups. | craft\services\Sites |
getAllSiteIds() |
Returns all of the site IDs. | craft\services\Sites |
getAllSites() |
Returns all sites. | craft\services\Sites |
getBehavior() |
Returns the named behavior object. | yii\base\Component |
getBehaviors() |
Returns all behaviors attached to this component. | yii\base\Component |
getCurrentSite() |
Returns the current site. | craft\services\Sites |
getEditableSiteIds() |
Returns all of the site IDs that are editable by the current user. | craft\services\Sites |
getEditableSites() |
Returns all editable sites. | craft\services\Sites |
getGroupById() |
Returns a site group by its ID. | craft\services\Sites |
getHasCurrentSite() |
Returns whether the current site has been set yet. | craft\services\Sites |
getPrimarySite() |
Returns the primary site. The primary site is whatever is listed first in Settings > Sites in the control panel. | craft\services\Sites |
getSiteByHandle() |
Returns a site by its handle. | craft\services\Sites |
getSiteById() |
Returns a site by its ID. | craft\services\Sites |
getSitesByGroupId() |
Returns sites by a group ID. | craft\services\Sites |
getTotalEditableSites() |
Gets the total number of sites that are editable by the current user. | craft\services\Sites |
getTotalSites() |
Gets the total number of sites. | craft\services\Sites |
hasEventHandlers() |
Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMethod() |
Returns a value indicating whether a method is defined. | yii\base\BaseObject |
hasProperty() |
Returns a value indicating whether a property is defined. | yii\base\BaseObject |
init() |
Initializes the object. | craft\services\Sites |
off() |
Detaches an existing event handler from this component. | yii\base\Component |
on() |
Attaches an event handler to an event. | yii\base\Component |
reorderSites() |
Reorders sites. | craft\services\Sites |
saveGroup() |
Saves a site group. | craft\services\Sites |
saveSite() |
Saves a site. | craft\services\Sites |
setCurrentSite() |
Sets the current site. | craft\services\Sites |
trigger() |
Triggers an event. | yii\base\Component |
Method Details
deleteGroup()
public method
#
Deletes a site group.
public boolean deleteGroup ( craft\models\SiteGroup $group )
$group |
craft\models\SiteGroup | The site group |
return | boolean | Whether the site group was deleted successfully |
---|
deleteGroupById()
public method
#
Deletes a site group by its ID.
public boolean deleteGroupById ( \craft\services\int $groupId )
$groupId |
integer | The site group’s ID |
return | boolean | Whether the site group was deleted successfully |
---|
deleteSite()
public method
#
Deletes a site.
public boolean deleteSite ( craft\models\Site $site, \craft\services\int $transferContentTo = null )
$site |
craft\models\Site | The site to be deleted |
$transferContentTo |
integer, null | The site ID that should take over the deleted site’s contents |
return | boolean | Whether the site was deleted successfully |
---|---|---|
throws | yii\base\Exception | if $site is the primary site |
throws | Throwable | if reasons |
deleteSiteById()
public method
#
Deletes a site by its ID.
public boolean deleteSiteById ( \craft\services\int $siteId, \craft\services\int $transferContentTo = null )
$siteId |
integer | The site ID to be deleted |
$transferContentTo |
integer, null | The site ID that should take over the deleted site’s contents |
return | boolean | Whether the site was deleted successfully |
---|---|---|
throws | Throwable | if reasons |
getAllGroups()
public method
#
Returns all site groups.
public craft\models\SiteGroup[] getAllGroups ( )
return | craft\models\SiteGroup[] | The site groups |
---|
getAllSiteIds()
public method
#
Returns all of the site IDs.
public integer[] getAllSiteIds ( )
return | integer[] | All the sites’ IDs |
---|
getAllSites()
public method
#
Returns all sites.
public craft\models\Site[] getAllSites ( )
return | craft\models\Site[] | All the sites |
---|
getCurrentSite()
public method
#
Returns the current site.
public craft\models\Site getCurrentSite ( )
return | craft\models\Site | The current site |
---|---|---|
throws | craft\errors\SiteNotFoundException | if no sites exist |
getEditableSiteIds()
public method
#
Returns all of the site IDs that are editable by the current user.
public array getEditableSiteIds ( )
return | array | All the editable sites’ IDs |
---|
getEditableSites()
public method
#
Returns all editable sites.
public craft\models\Site[] getEditableSites ( )
return | craft\models\Site[] | All the editable sites |
---|
getGroupById()
public method
#
Returns a site group by its ID.
public craft\models\SiteGroup, null getGroupById ( \craft\services\int $groupId )
$groupId |
integer | The site group’s ID |
return | craft\models\SiteGroup, null | The site group, or null if it doesn’t exist |
---|
getHasCurrentSite()
public method
#
Returns whether the current site has been set yet.
public boolean getHasCurrentSite ( )
getPrimarySite()
public method
#
Returns the primary site. The primary site is whatever is listed first in Settings > Sites in the control panel.
public craft\models\Site getPrimarySite ( )
return | craft\models\Site | The primary site |
---|---|---|
throws | craft\errors\SiteNotFoundException | if no sites exist |
getSiteByHandle()
public method
#
Returns a site by its handle.
public craft\models\Site, null getSiteByHandle ( \craft\services\string $siteHandle )
$siteHandle |
string |
getSiteById()
public method
#
Returns a site by its ID.
public craft\models\Site, null getSiteById ( \craft\services\int $siteId )
$siteId |
integer |
getSitesByGroupId()
public method
#
Returns sites by a group ID.
public craft\models\Site[] getSitesByGroupId ( \craft\services\int $groupId )
$groupId |
integer |
getTotalEditableSites()
public method
#
Gets the total number of sites that are editable by the current user.
public integer getTotalEditableSites ( )
getTotalSites()
public method
#
Gets the total number of sites.
public integer getTotalSites ( )
init()
public method
#
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public void init ( )
reorderSites()
public method
#
Reorders sites.
public boolean reorderSites ( array $siteIds )
$siteIds |
integer[] | The site IDs in their new order |
return | boolean | Whether the sites were reordered successfthe sites are reorderedy |
---|---|---|
throws | Throwable | if reasons |
saveGroup()
public method
#
Saves a site group.
public boolean saveGroup ( craft\models\SiteGroup $group, \craft\services\bool $runValidation = true )
$group |
craft\models\SiteGroup | The site group to be saved |
$runValidation |
boolean | Whether the group should be validated |
return | boolean | Whether the site group was saved successfully |
---|
saveSite()
public method
#
Saves a site.
public boolean saveSite ( craft\models\Site $site, \craft\services\bool $runValidation = true )
$site |
craft\models\Site | The site to be saved |
$runValidation |
boolean | Whether the site should be validated |
throws | craft\errors\SiteNotFoundException | if $site->id is invalid |
---|---|---|
throws | Throwable | if reasons |
setCurrentSite()
public method
#
Sets the current site.
public void setCurrentSite ( $site )
$site |
craft\models\Site, string, integer, null | The current site, or its handle/ID, or null |
throws | yii\base\InvalidArgumentException | if $site is invalid |
---|
Event Details
EVENT_AFTER_CHANGE_PRIMARY_SITE
event of type craft\events\SiteEvent
#
The event that is triggered after the primary site has changed
EVENT_AFTER_DELETE_SITE
event of type craft\events\DeleteSiteEvent
#
The event that is triggered after a site is deleted.
EVENT_AFTER_DELETE_SITE_GROUP
event of type craft\events\SiteGroupEvent
#
The event that is triggered after a site group is deleted.
EVENT_AFTER_REORDER_SITES
event of type craft\events\ReorderSitesEvent
#
The event that is triggered after the sites are reordered.
EVENT_AFTER_SAVE_SITE
event of type craft\events\SiteEvent
#
The event that is triggered after a site is saved.
EVENT_AFTER_SAVE_SITE_GROUP
event of type craft\events\SiteGroupEvent
#
The event that is triggered after a site group is saved.
EVENT_BEFORE_DELETE_SITE
event of 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
event of type craft\events\SiteGroupEvent
#
The event that is triggered before a site group is deleted.
EVENT_BEFORE_REORDER_SITES
event of type craft\events\ReorderSitesEvent
#
The event that is triggered before the sites are reordered.
EVENT_BEFORE_SAVE_SITE
event of type craft\events\SiteEvent
#
The event that is triggered before a site is saved.
EVENT_BEFORE_SAVE_SITE_GROUP
event of type craft\events\SiteGroupEvent
#
The event that is triggered before a site group is saved.