Entries ​
- Type
- Class
- Namespace
- craft\services
- Inherits
- craft\services\Entries » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable
- Since
- 3.0.0
The Entries service provides APIs for managing entries in Craft.
An instance of the service is available via Craft::$app->entries
.
Public Properties ​
Property | Description |
---|---|
allEntryTypes | craft\models\EntryType[] |
allSectionIds | integer[] – All the sections’ IDs. |
allSections | craft\models\Section[] – All the sections. |
autoResaveEntries | boolean – Whether entries should be resaved after a section or entry type has been updated. |
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
editableSectionIds | integer[] – All the editable sections’ IDs. |
editableSections | craft\models\Section[] – All the editable sections. |
totalEditableSections | integer |
totalSections | integer |
allEntryTypes
​
- Type
- craft\models\EntryType[]
- Default value
null
- Access
- Read-only
- Since
- 5.0.0
allSectionIds
​
- Type
- integer[]
- Default value
null
- Access
- Read-only
- Since
- 5.0.0
All the sections’ IDs.
allSections
​
- Type
- craft\models\Section[]
- Default value
null
- Access
- Read-only
- Since
- 5.0.0
All the sections.
autoResaveEntries
​
- Type
- boolean
- Default value
true
- Since
- 5.0.0
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.)
editableSectionIds
​
- Type
- integer[]
- Default value
null
- Access
- Read-only
- Since
- 5.0.0
All the editable sections’ IDs.
editableSections
​
- Type
- craft\models\Section[]
- Default value
null
- Access
- Read-only
- Since
- 5.0.0
All the editable sections.
totalEditableSections
​
- Type
- integer
- Default value
null
- Access
- Read-only
- Since
- 5.0.0
totalSections
​
- Type
- integer
- Default value
null
- Access
- Read-only
- Since
- 5.0.0
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. |
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() | 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. |
getAllEntryTypes() | Returns all entry types. |
getAllSectionIds() | Returns all of the section IDs. |
getAllSections() | Returns all sections. |
getBehavior() | Returns the named behavior object. |
getBehaviors() | 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. |
getEntryById() | Returns an entry by its ID. |
getEntryTypeByHandle() | Returns an entry type by its handle. |
getEntryTypeById() | Returns an entry type by its ID. |
getEntryTypeByUid() | Returns an entry type by its UID. |
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. |
getSectionTableData() | Returns data for the Sections index page in the control panel. |
getSectionsByType() | Returns all sections of a given type. |
getSingleEntriesByHandle() | Returns an array of Single section entries which match a given list of section handles. |
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() | 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. |
moveEntryToSection() | Move entry to a different section. |
off() | Detaches an existing event handler from this component. |
on() | Attaches an event handler to an event. |
pruneDeletedField() | |
pruneDeletedSite() | Prune a deleted site from section site settings. |
refreshEntryTypes() | Refreshes the internal entry type cache. |
saveEntryType() | Saves an entry type. |
saveSection() | Saves a section. |
trigger() | Triggers an event. |
__serialize()
​
Serializer
deleteEntryType()
​
- Since
- 5.0.0
Deletes an entry type.
Arguments ​
$entryType
(craft\models\EntryType)
Returns ​
boolean – Whether the entry type was deleted successfully
Throws ​
- Throwable
if reasons
Example ​
$success = Craft::$app->entries->deleteEntryType($entryType);
deleteEntryTypeById()
​
- Since
- 5.0.0
Deletes an entry type by its ID.
Arguments ​
$entryTypeId
(integer)
Returns ​
boolean – Whether the entry type was deleted successfully
Throws ​
- Throwable
if reasons
Example ​
$success = Craft::$app->entries->deleteEntryTypeById(1);
deleteSection()
​
- Since
- 5.0.0
Deletes a section.
Arguments ​
$section
(craft\models\Section)
Returns ​
boolean – Whether the section was deleted successfully
Throws ​
- Throwable
if reasons
Example ​
$success = Craft::$app->entries->deleteSection($section);
deleteSectionById()
​
- Since
- 5.0.0
Deletes a section by its ID.
Arguments ​
$sectionId
(integer)
Returns ​
boolean – Whether the section was deleted successfully
Throws ​
- Throwable
if reasons
Example ​
$success = Craft::$app->entries->deleteSectionById(1);
getAllEntryTypes()
​
- Since
- 5.0.0
Returns all entry types.
Returns ​
Example ​
$entryTypes = Craft::$app->entries->getAllEntryTypes();
getAllSectionIds()
​
- Since
- 5.0.0
Returns all of the section IDs.
Returns ​
integer[] – All the sections’ IDs.
Example ​
$sectionIds = Craft::$app->entries->allSectionIds;
{% set sectionIds = craft.app.entries.allSectionIds %}
getAllSections()
​
- Since
- 5.0.0
Returns all sections.
Returns ​
craft\models\Section[] – All the sections.
Example ​
$sections = Craft::$app->entries->allSections;
{% set sections = craft.app.entries.allSections %}
getEditableSectionIds()
​
- Since
- 5.0.0
Returns all of the section IDs that are editable by the current user.
Returns ​
integer[] – All the editable sections’ IDs.
Example ​
$sectionIds = Craft::$app->entries->editableSectionIds;
{% set sectionIds = craft.app.entries.editableSectionIds %}
getEditableSections()
​
- Since
- 5.0.0
Returns all editable sections.
Returns ​
craft\models\Section[] – All the editable sections.
Example ​
$sections = Craft::$app->entries->editableSections;
{% set sections = craft.app.entries.editableSections %}
getEntryById()
​
Returns an entry by its ID.
$entry = Craft::$app->entries->getEntryById($entryId);
Arguments ​
$entryId
(integer) – The entry’s ID.$siteId
(integer, string, integer[], null) – The site(s) to fetch the entry in. Defaults to the current site.$criteria
(array)
Returns ​
craft\elements\Entry, null – The entry with the given ID, or null
if an entry could not be found.
getEntryTypeByHandle()
​
- Since
- 5.0.0
Returns an entry type by its handle.
Arguments ​
$entryTypeHandle
(string)
Returns ​
Example ​
$entryType = Craft::$app->entries->getEntryTypeByHandle('article');
getEntryTypeById()
​
- Since
- 5.0.0
Returns an entry type by its ID.
Arguments ​
$entryTypeId
(integer)
Returns ​
Example ​
$entryType = Craft::$app->entries->getEntryTypeById(1);
getEntryTypeByUid()
​
- Since
- 5.0.0
Returns an entry type by its UID.
Arguments ​
$uid
(string)
Returns ​
getEntryTypesBySectionId()
​
- Since
- 5.0.0
Returns a section’s entry types.
Arguments ​
$sectionId
(integer)
Returns ​
Example ​
$entryTypes = Craft::$app->entries->getEntryTypesBySectionId(1);
getSectionByHandle()
​
- Since
- 5.0.0
Gets a section by its handle.
Arguments ​
$sectionHandle
(string)
Returns ​
Example ​
$section = Craft::$app->entries->getSectionByHandle('news');
{% set section = craft.app.entries.getSectionByHandle('news') %}
getSectionById()
​
- Since
- 5.0.0
Returns a section by its ID.
Arguments ​
$sectionId
(integer)
Returns ​
Example ​
$section = Craft::$app->entries->getSectionById(1);
{% set section = craft.app.entries.getSectionById(1) %}
getSectionByUid()
​
- Since
- 5.0.0
Gets a section by its UID.
Arguments ​
$uid
(string)
Returns ​
Example ​
$section = Craft::$app->entries->getSectionByUid('b3a9eef3-9444-4995-84e2-6dc6b60aebd2');
{% set section = craft.app.entries.getSectionByUid('b3a9eef3-9444-4995-84e2-6dc6b60aebd2') %}
getSectionSiteSettings()
​
- Since
- 5.0.0
Returns a section’s site-specific settings.
Arguments ​
$sectionId
(integer)
Returns ​
craft\models\Section_SiteSettings[] – The section’s site-specific settings.
getSectionTableData()
​
- Since
- 5.5.0
Returns data for the Sections index page in the control panel.
Arguments ​
Returns ​
getSectionsByType()
​
- Since
- 5.0.0
Returns all sections of a given type.
Arguments ​
$type
(string) – The section type (single
,channel
, orstructure
)
Returns ​
craft\models\Section[] – All the sections of the given type.
Example ​
use craft\models\Section;
$singles = Craft::$app->entries->getSectionsByType(Section::TYPE_SINGLE);
{% set singles = craft.app.entries.getSectionsByType('single') %}
getSingleEntriesByHandle()
​
- Since
- 4.4.0
Returns an array of Single section entries which match a given list of section handles.
Arguments ​
$handles
(string[])
Returns ​
array<string,\craft\elements\Entry>
getTotalEditableSections()
​
- Since
- 5.0.0
Gets the total number of sections that are editable by the current user.
Returns ​
Example ​
$total = Craft::$app->entries->totalEditableSections;
{% set total = craft.app.entries.totalEditableSections %}
getTotalSections()
​
- Since
- 5.0.0
Gets the total number of sections.
Returns ​
Example ​
$total = Craft::$app->entries->totalSections;
{% set total = craft.app.entries.totalSections %}
handleChangedEntryType()
​
- Since
- 5.0.0
Handle entry type change
Arguments ​
$event
(craft\events\ConfigEvent)
handleChangedSection()
​
- Since
- 5.0.0
Handle section change
Arguments ​
$event
(craft\events\ConfigEvent)
handleDeletedEntryType()
​
- Since
- 5.0.0
Handle an entry type getting deleted
Arguments ​
$event
(craft\events\ConfigEvent)
handleDeletedSection()
​
- Since
- 5.0.0
Handle a section getting deleted
Arguments ​
$event
(craft\events\ConfigEvent)
moveEntryToSection()
​
- Since
- 5.3.0
Move entry to a different section.
Arguments ​
$entry
(craft\elements\Entry)$section
(craft\models\Section)
Returns ​
Throws ​
- yii\base\Exception
- craft\errors\InvalidElementException
- Throwable
- craft\errors\UnsupportedSiteException
pruneDeletedField()
​
DEPRECATED
Deprecated in 4.0.5. Unused fields will be pruned automatically as field layouts are resaved.
pruneDeletedSite()
​
- Since
- 5.0.0
Prune a deleted site from section site settings.
Arguments ​
$event
(craft\events\DeleteSiteEvent)
refreshEntryTypes()
​
- Since
- 5.0.0
Refreshes the internal entry type cache.
saveEntryType()
​
- Since
- 5.0.0
Saves an entry type.
Arguments ​
$entryType
(craft\models\EntryType) – The entry type to be saved$runValidation
(boolean) – Whether the entry type should be validated
Returns ​
boolean – Whether the entry type was saved successfully
Throws ​
- craft\errors\EntryTypeNotFoundException
if $entryType->id is invalid - Throwable
if reasons
saveSection()
​
- Since
- 5.0.0
Saves a section.
Arguments ​
$section
(craft\models\Section) – The section to be saved$runValidation
(boolean) – Whether the section should be validated
Returns ​
Throws ​
- craft\errors\SectionNotFoundException
if $section->id is invalid - Throwable
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->entries->saveSection($section);
Events ​
EVENT_AFTER_DELETE_ENTRY_TYPE ​
- Type
- craft\events\EntryTypeEvent
- Since
- 5.0.0
The event that is triggered after an entry type is deleted.
EVENT_AFTER_DELETE_SECTION ​
- Type
- craft\events\SectionEvent
- Since
- 5.0.0
The event that is triggered after a section is deleted.
EVENT_AFTER_MOVE_TO_SECTION ​
- Type
- craft\events\MoveEntryEvent
- Since
- 5.3.0
The event that is triggered before an entry is move to a different section.
EVENT_AFTER_SAVE_ENTRY_TYPE ​
- Type
- craft\events\EntryTypeEvent
- Since
- 5.0.0
The event that is triggered after an entry type is saved.
EVENT_AFTER_SAVE_SECTION ​
- Type
- craft\events\SectionEvent
- Since
- 5.0.0
The event that is triggered after a section is saved.
EVENT_BEFORE_APPLY_ENTRY_TYPE_DELETE ​
- Type
- craft\events\EntryTypeEvent
- Since
- 5.0.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
- 5.0.0
The event that is triggered before a section delete is applied to the database.
EVENT_BEFORE_DELETE_ENTRY_TYPE ​
- Type
- craft\events\EntryTypeEvent
- Since
- 5.0.0
The event that is triggered before an entry type is deleted.
EVENT_BEFORE_DELETE_SECTION ​
- Type
- craft\events\SectionEvent
- Since
- 5.0.0
The event that is triggered before a section is deleted.
EVENT_BEFORE_MOVE_TO_SECTION ​
- Type
- craft\events\MoveEntryEvent
- Since
- 5.3.0
The event that is triggered before an entry is move to a different section.
EVENT_BEFORE_SAVE_ENTRY_TYPE ​
- Type
- craft\events\EntryTypeEvent
- Since
- 5.0.0
The event that is triggered before an entry type is saved.
EVENT_BEFORE_SAVE_SECTION ​
- Type
- craft\events\SectionEvent
- Since
- 5.0.0
The event that is triggered before a section is saved.