Sections
- Type
- Class
- Namespace
- craft\services
- Inherits
- craft\services\Sections » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
- Implements
- yii\base\Configurable (opens new window)
- Since
- 3.0.0
Sections service.
An instance of the service is available via Craft::$app->sections
.
View source (opens new window)
# Public Properties
Property | Description |
---|---|
allEntryTypes | craft\models\EntryType[] |
allSectionIds | integer (opens new window)[] – All the sections’ IDs. |
allSections | craft\models\Section[] – All the sections. |
autoResaveEntries | boolean (opens new window) – Whether entries should be resaved after a section or entry type has been updated. |
behaviors (opens new window) | yii\base\Behavior (opens new window) – List of behaviors attached to this component. |
editableSectionIds | integer (opens new window)[] – All the editable sections’ IDs. |
editableSections | craft\models\Section[] – All the editable sections. |
totalEditableSections | integer (opens new window) |
totalSections | integer (opens new window) |
# allEntryTypes
- Type
- craft\models\EntryType[]
- Default value
null
- Access
- Read-only
- Since
- 3.3.0
View source (opens new window)
# allSectionIds
- Type
- integer (opens new window)[]
- Default value
null
- Access
- Read-only
All the sections’ IDs.
View source (opens new window)
# allSections
- Type
- craft\models\Section[]
- Default value
null
- Access
- Read-only
All the sections.
View source (opens new window)
# autoResaveEntries
- Type
- boolean (opens new window)
- Default value
true
- Since
- 3.1.21
Whether entries should be resaved after a section or entry type has been updated.
TIP
Entries will be resaved regardless of what this is set to, when a section’s Propagation Method setting changes.
WARNING
Don’t disable this unless you know what you’re doing, as entries won’t reflect section/entry type changes until
they’ve been resaved. (You can resave entries manually by running the resave/entries
console command.)
View source (opens new window)
# editableSectionIds
- Type
- integer (opens new window)[]
- Default value
null
- Access
- Read-only
All the editable sections’ IDs.
View source (opens new window)
# editableSections
- Type
- craft\models\Section[]
- Default value
null
- Access
- Read-only
All the editable sections.
View source (opens new window)
# totalEditableSections
- Type
- integer (opens new window)
- Default value
null
- Access
- Read-only
View source (opens new window)
# totalSections
- Type
- integer (opens new window)
- Default value
null
- Access
- Read-only
View source (opens new window)
# Public Methods
Method | Description |
---|---|
__call() (opens new window) | Calls the named method which is not a class method. |
__clone() (opens new window) | This method is called after the object is created by cloning an existing one. |
__construct() (opens new window) | Constructor. |
__get() (opens new window) | Returns the value of a component property. |
__isset() (opens new window) | Checks if a property is set, i.e. defined and not null. |
__serialize() | Serializer |
__set() (opens new window) | Sets the value of a component property. |
__unset() (opens new window) | Sets a component property to be null. |
attachBehavior() (opens new window) | Attaches a behavior to this component. |
attachBehaviors() (opens new window) | Attaches a list of behaviors to the component. |
behaviors() (opens new window) | Returns a list of behaviors that this component should behave as. |
canGetProperty() (opens new window) | Returns a value indicating whether a property can be read. |
canSetProperty() (opens new window) | Returns a value indicating whether a property can be set. |
className() (opens new window) | Returns the fully qualified name of this class. |
deleteEntryType() | Deletes an entry type. |
deleteEntryTypeById() | Deletes an entry type by its ID. |
deleteSection() | Deletes a section. |
deleteSectionById() | Deletes a section by its ID. |
detachBehavior() (opens new window) | Detaches a behavior from the component. |
detachBehaviors() (opens new window) | Detaches all behaviors from the component. |
ensureBehaviors() (opens new window) | Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component. |
getAllEntryTypes() | Returns all entry types. |
getAllSectionIds() | Returns all of the section IDs. |
getAllSections() | Returns all sections. |
getBehavior() (opens new window) | Returns the named behavior object. |
getBehaviors() (opens new window) | Returns all behaviors attached to this component. |
getEditableSectionIds() | Returns all of the section IDs that are editable by the current user. |
getEditableSections() | Returns all editable sections. |
getEntryTypeById() | Returns an entry type by its ID. |
getEntryTypeByUid() | Returns an entry type by its UID. |
getEntryTypesByHandle() | Returns entry types that have a given handle. |
getEntryTypesBySectionId() | Returns a section’s entry types. |
getSectionByHandle() | Gets a section by its handle. |
getSectionById() | Returns a section by its ID. |
getSectionByUid() | Gets a section by its UID. |
getSectionSiteSettings() | Returns a section’s site-specific settings. |
getSectionsByType() | Returns all sections of a given type. |
getTotalEditableSections() | Gets the total number of sections that are editable by the current user. |
getTotalSections() | Gets the total number of sections. |
handleChangedEntryType() | Handle entry type change |
handleChangedSection() | Handle section change |
handleDeletedEntryType() | Handle an entry type getting deleted |
handleDeletedSection() | Handle a section getting deleted |
hasEventHandlers() (opens new window) | Returns a value indicating whether there is any handler attached to the named event. |
hasMethod() (opens new window) | Returns a value indicating whether a method is defined. |
hasProperty() (opens new window) | Returns a value indicating whether a property is defined for this component. |
init() (opens new window) | Initializes the object. |
off() (opens new window) | Detaches an existing event handler from this component. |
on() (opens new window) | Attaches an event handler to an event. |
pruneDeletedField() | |
pruneDeletedSite() | Prune a deleted site from section site settings. |
reorderEntryTypes() | Reorders entry types. |
saveEntryType() | Saves an entry type. |
saveSection() | Saves a section. |
trigger() (opens new window) | Triggers an event. |
# __serialize()
- Since
- 3.5.14
Serializer
View source (opens new window)
# deleteEntryType()
Deletes an entry type.
View source (opens new window)
Arguments
$entryType
(craft\models\EntryType)
Returns
boolean (opens new window) – Whether the entry type was deleted successfully
Throws
- Throwable (opens new window)
if reasons
Example
$success = Craft::$app->sections->deleteEntryType($entryType);
# deleteEntryTypeById()
Deletes an entry type by its ID.
View source (opens new window)
Arguments
$entryTypeId
(integer (opens new window))
Returns
boolean (opens new window) – Whether the entry type was deleted successfully
Throws
- Throwable (opens new window)
if reasons
Example
$success = Craft::$app->sections->deleteEntryTypeById(1);
# deleteSection()
Deletes a section.
View source (opens new window)
Arguments
$section
(craft\models\Section)
Returns
boolean (opens new window) – Whether the section was deleted successfully
Throws
- Throwable (opens new window)
if reasons
Example
$success = Craft::$app->sections->deleteSection($section);
# deleteSectionById()
Deletes a section by its ID.
View source (opens new window)
Arguments
$sectionId
(integer (opens new window))
Returns
boolean (opens new window) – Whether the section was deleted successfully
Throws
- Throwable (opens new window)
if reasons
Example
$success = Craft::$app->sections->deleteSectionById(1);
# getAllEntryTypes()
Example
$entryTypes = Craft::$app->sections->getAllEntryTypes();
# getAllSectionIds()
Returns all of the section IDs.
View source (opens new window)
Returns
integer (opens new window)[] – All the sections’ IDs.
# getAllSections()
Returns all sections.
View source (opens new window)
Returns
craft\models\Section[] – All the sections.
# getEditableSectionIds()
Returns all of the section IDs that are editable by the current user.
View source (opens new window)
Returns
integer (opens new window)[] – All the editable sections’ IDs.
# getEditableSections()
Returns all editable sections.
View source (opens new window)
Returns
craft\models\Section[] – All the editable sections.
# getEntryTypeById()
Returns an entry type by its ID.
View source (opens new window)
Arguments
$entryTypeId
(integer (opens new window))
Returns
Example
$entryType = Craft::$app->sections->getEntryTypeById(1);
# getEntryTypeByUid()
- Since
- 3.7.39
Returns an entry type by its UID.
View source (opens new window)
Arguments
$uid
(string (opens new window))
Returns
craft\models\EntryType, null (opens new window)
# getEntryTypesByHandle()
Returns entry types that have a given handle.
View source (opens new window)
Arguments
$entryTypeHandle
(string (opens new window))
Returns
Example
$entryTypes = Craft::$app->sections->getEntryTypesByHandle('article');
# getEntryTypesBySectionId()
Returns a section’s entry types.
View source (opens new window)
Arguments
$sectionId
(integer (opens new window))
Returns
Example
$entryTypes = Craft::$app->sections->getEntryTypesBySectionId(1);
# getSectionByHandle()
Gets a section by its handle.
View source (opens new window)
Arguments
$sectionHandle
(string (opens new window))
Returns
# getSectionById()
Returns a section by its ID.
View source (opens new window)
Arguments
$sectionId
(integer (opens new window))
Returns
# getSectionByUid()
- Since
- 3.1.0
Gets a section by its UID.
View source (opens new window)
Arguments
$uid
(string (opens new window))
Returns
# getSectionSiteSettings()
Returns a section’s site-specific settings.
View source (opens new window)
Arguments
$sectionId
(integer (opens new window))
Returns
craft\models\Section_SiteSettings[] – The section’s site-specific settings.
# getSectionsByType()
Returns all sections of a given type.
View source (opens new window)
Arguments
$type
(string (opens new window)) – The section type (single
,channel
, orstructure
)
Returns
craft\models\Section[] – All the sections of the given type.
# getTotalEditableSections()
Gets the total number of sections that are editable by the current user.
View source (opens new window)
Returns
# getTotalSections()
# handleChangedEntryType()
Handle entry type change
View source (opens new window)
Arguments
$event
(craft\events\ConfigEvent)
# handleChangedSection()
Handle section change
View source (opens new window)
Arguments
$event
(craft\events\ConfigEvent)
# handleDeletedEntryType()
Handle an entry type getting deleted
View source (opens new window)
Arguments
$event
(craft\events\ConfigEvent)
# handleDeletedSection()
Handle a section getting deleted
View source (opens new window)
Arguments
$event
(craft\events\ConfigEvent)
# pruneDeletedField()
DEPRECATED
Deprecated in 4.0.5. Unused fields will be pruned automatically as field layouts are resaved.
View source (opens new window)
# pruneDeletedSite()
Prune a deleted site from section site settings.
View source (opens new window)
Arguments
$event
(craft\events\DeleteSiteEvent)
# reorderEntryTypes()
Reorders entry types.
View source (opens new window)
Arguments
$entryTypeIds
(array (opens new window))
Returns
boolean (opens new window) – Whether the entry types were reordered successfully
Throws
- Throwable (opens new window)
if reasons
# saveEntryType()
Saves an entry type.
View source (opens new window)
Arguments
$entryType
(craft\models\EntryType) – The entry type to be saved$runValidation
(boolean (opens new window)) – Whether the entry type should be validated
Returns
boolean (opens new window) – Whether the entry type was saved successfully
Throws
- craft\errors\EntryTypeNotFoundException
if $entryType->id is invalid - Throwable (opens new window)
if reasons
# saveSection()
Saves a section.
View source (opens new window)
Arguments
$section
(craft\models\Section) – The section to be saved$runValidation
(boolean (opens new window)) – Whether the section should be validated
Returns
Throws
- craft\errors\SectionNotFoundException
if $section->id is invalid - Throwable (opens new window)
if reasons
Example
use craft\models\Section;
use craft\models\Section_SiteSettings;
$section = new Section([
'name' => 'News',
'handle' => 'news',
'type' => Section::TYPE_CHANNEL,
'siteSettings' => [
new Section_SiteSettings([
'siteId' => Craft::$app->sites->getPrimarySite()->id,
'enabledByDefault' => true,
'hasUrls' => true,
'uriFormat' => 'foo/{slug}',
'template' => 'foo/_entry',
]),
]
]);
$success = Craft::$app->sections->saveSection($section);
# Events
# EVENT_AFTER_DELETE_ENTRY_TYPE
The event that is triggered after an entry type is deleted.
# EVENT_AFTER_DELETE_SECTION
The event that is triggered after a section is deleted.
# EVENT_AFTER_SAVE_ENTRY_TYPE
The event that is triggered after an entry type is saved.
# EVENT_AFTER_SAVE_SECTION
The event that is triggered after a section is saved.
# EVENT_BEFORE_APPLY_ENTRY_TYPE_DELETE
- Type
- craft\events\EntryTypeEvent
- Since
- 3.1.0
The event that is triggered before an entry type delete is applied to the database.
# EVENT_BEFORE_APPLY_SECTION_DELETE
- Type
- craft\events\SectionEvent
- Since
- 3.1.0
The event that is triggered before a section delete is applied to the database.
# EVENT_BEFORE_DELETE_ENTRY_TYPE
The event that is triggered before an entry type is deleted.
# EVENT_BEFORE_DELETE_SECTION
The event that is triggered before a section is deleted.
# EVENT_BEFORE_SAVE_ENTRY_TYPE
The event that is triggered before an entry type is saved.
# EVENT_BEFORE_SAVE_SECTION
The event that is triggered before a section is saved.