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

Signature

public craft\models\SiteGroup[] getAllGroups ( )

$allSiteIds

Type
integer[]
Access
Read-only

All the sites’ IDs

View source

Signature

public integer[] getAllSiteIds ( )

$allSites

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

All the sites

View source

Signature

public craft\models\Site[] getAllSites ( )

$currentSite

Type
craft\models\Site

The current site

View source

Signature

public craft\models\Site getCurrentSite ( )
public void setCurrentSite ( $site )

$editableSiteIds

Type
array
Access
Read-only

All the editable sites’ IDs

View source

Signature

public array getEditableSiteIds ( )

$editableSites

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

All the editable sites

View source

Signature

public craft\models\Site[] getEditableSites ( )

$hasCurrentSite

Type
boolean
Access
Read-only

View source

Signature

public boolean getHasCurrentSite ( )

$primarySite

Type
craft\models\Site
Access
Read-only

The primary site

View source

Signature

public craft\models\Site getPrimarySite ( )

$totalEditableSites

Type
integer
Access
Read-only

View source

Signature

public integer getTotalEditableSites ( )

$totalSites

Type
integer
Access
Read-only

View source

Signature

public integer getTotalSites ( )

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.
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.
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.
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

Signature

public boolean deleteGroup ( craft\models\SiteGroup $group )

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

Signature

public boolean deleteGroupById ( \craft\services\int $groupId )

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

Signature

public boolean deleteSite ( craft\models\Site $site, \craft\services\int $transferContentTo = null )

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

Signature

public boolean deleteSiteById ( \craft\services\int $siteId, \craft\services\int $transferContentTo = null )

getAllGroups()

Returns all site groups.

View source

Returns

craft\models\SiteGroup[] – The site groups

Signature

public craft\models\SiteGroup[] getAllGroups ( )

getAllSiteIds()

Returns all of the site IDs.

View source

Returns

integer[] – All the sites’ IDs

Signature

public integer[] getAllSiteIds ( )

getAllSites()

Returns all sites.

View source

Returns

craft\models\Site[] – All the sites

Signature

public craft\models\Site[] getAllSites ( )

getCurrentSite()

Returns the current site.

View source

Returns

craft\models\Site – The current site

Throws

Signature

public craft\models\Site getCurrentSite ( )

getEditableSiteIds()

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

View source

Returns

array – All the editable sites’ IDs

Signature

public array getEditableSiteIds ( )

getEditableSites()

Returns all editable sites.

View source

Returns

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

Signature

public craft\models\Site[] getEditableSites ( )

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

Signature

public craft\models\SiteGroup, null getGroupById ( \craft\services\int $groupId )

getHasCurrentSite()

Returns whether the current site has been set yet.

View source

Returns

boolean

Signature

public boolean getHasCurrentSite ( )

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

Signature

public craft\models\Site getPrimarySite ( )

getSiteByHandle()

Returns a site by its handle.

View source

Arguments

Returns

craft\models\Site, null

Signature

public craft\models\Site, null getSiteByHandle ( \craft\services\string $siteHandle )

getSiteById()

Returns a site by its ID.

View source

Arguments

Returns

craft\models\Site, null

Signature

public craft\models\Site, null getSiteById ( \craft\services\int $siteId )

getSitesByGroupId()

Returns sites by a group ID.

View source

Arguments

Returns

craft\models\Site[]

Signature

public craft\models\Site[] getSitesByGroupId ( \craft\services\int $groupId )

getTotalEditableSites()

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

View source

Returns

integer

Signature

public integer getTotalEditableSites ( )

getTotalSites()

Gets the total number of sites.

View source

Returns

integer

Signature

public integer getTotalSites ( )

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

Signature

public void init ( )

reorderSites()

Reorders sites.

View source

Arguments

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

Returns

boolean – Whether the sites were reordered successfthe sites are reorderedy

Throws

Signature

public boolean reorderSites ( array $siteIds )

saveGroup()

Saves a site group.

View source

Arguments

Returns

boolean – Whether the site group was saved successfully

Signature

public boolean saveGroup ( craft\models\SiteGroup $group, \craft\services\bool $runValidation = true )

saveSite()

Saves a site.

View source

Arguments

Returns

boolean

Throws

Signature

public boolean saveSite ( craft\models\Site $site, \craft\services\bool $runValidation = true )

setCurrentSite()

Sets the current site.

View source

Arguments

Throws

Signature

public void setCurrentSite ( $site )

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_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.