Skip to content

Commit

Permalink
Entries service cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Dec 29, 2023
1 parent bf8353f commit 061b863
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 37 deletions.
22 changes: 20 additions & 2 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,26 @@
- Added `craft\queue\BaseBatchedJob::before()`.
- Added `craft\queue\BaseBatchedJob::beforeBatch()`.
- Added `craft\services\Auth`.
- Added `craft\services\Entries::deleteEntryType()`.
- Added `craft\services\Entries::deleteEntryTypeById()`.
- Added `craft\services\Entries::deleteSection()`.
- Added `craft\services\Entries::deleteSectionById()`.
- Added `craft\services\Entries::getAllEntryTypes()`.
- Added `craft\services\Entries::getAllSectionIds()`.
- Added `craft\services\Entries::getAllSections()`.
- Added `craft\services\Entries::getEditableSectionIds()`.
- Added `craft\services\Entries::getEditableSections()`.
- Added `craft\services\Entries::getEntryTypeByHandle()`.
- Added `craft\services\Entries::getEntryTypeById()`.
- Added `craft\services\Entries::getEntryTypesBySectionId()`.
- Added `craft\services\Entries::getSectionByHandle()`.
- Added `craft\services\Entries::getSectionById()`.
- Added `craft\services\Entries::getSectionByUid()`.
- Added `craft\services\Entries::getSectionsByType()`.
- Added `craft\services\Entries::getTotalEditableSections()`.
- Added `craft\services\Entries::getTotalSections()`.
- Added `craft\services\Entries::refreshEntryTypes()`.
- Added `craft\services\Entries::saveSection()`.
- Added `craft\services\Fields::$fieldContext`, which replaces `craft\services\Content::$fieldContext`.
- Added `craft\services\Fields::getAllLayouts()`.
- Added `craft\services\Gql::defineContentArgumentsForFieldLayouts()`.
Expand All @@ -265,7 +284,6 @@
- Added `craft\web\CpScreenResponseBehavior::site()`.
- Added `craft\web\Request::getQueryParamsWithoutPath()`.
- Added `craft\web\twig\variables\Cp::getEntryTypeOptions()`.
- All of the `craft\services\Sections` members have been moved into `craft\services\Entries`.
- Renamed `craft\base\BlockElementInterface` to `NestedElementInterface`, and added the `getField()`, `getSortOrder()`, and `setOwner()` methods to it.
- Renamed `craft\base\Element::EVENT_SET_TABLE_ATTRIBUTE_HTML` to `EVENT_DEFINE_ATTRIBUTE_HTML`.
- Renamed `craft\base\Element::getHasCheckeredThumb()` to `hasCheckeredThumb()` and made it protected.
Expand Down Expand Up @@ -450,7 +468,7 @@
- Removed `craft\services\ProjectConfig::PATH_MATRIX_BLOCK_TYPES`.
- Removed `craft\services\ProjectConfig::PATH_MATRIX_BLOCK_TYPES`.
- Removed `craft\services\ProjectConfig::updateStoredConfigAfterRequest()`.
- Removed `craft\services\Sections::reorderEntryTypes()`.
- Removed `craft\services\Sections`. Most of its methods have been moved to `craft\services\Entries`.
- Removed `craft\web\CpScreenResponseBehavior::$contextMenuHtml`. `$contextMenuItems` should be used instead.
- Removed `craft\web\CpScreenResponseBehavior::contextMenuHtml()`. `contextMenuItems()` should be used instead.
- Removed `craft\web\CpScreenResponseBehavior::contextMenuTemplate()`. `contextMenuItems()` should be used instead.
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
- Entry types now have a “Show the Slug field” setting. ([#13799](https://github.com/craftcms/cms/discussions/13799))
- Fixed a bug where section preview targets weren’t being respected in Craft Pro. ([#14050](https://github.com/craftcms/cms/issues/14050))
- Added `craft\events\SetEagerLoadedElementsEvent::$plan`.
- Added `craft\services\Entries::getEntryTypeByHandle()`.
- `craft\base\ElementInterface::setEagerLoadedElements()` now has a `$plan` argument, which will be set to the eager-loading plan.
- Removed `craft\services\Entries::getEntryTypesByHandle()`.
- Fixed an error that could occur if eager-loading aliases conflicted with native eager-loading handles, such as `author`. ([#14057](https://github.com/craftcms/cms/issues/14057))
- Fixed an error that occurred when indexing search keywords for an element with multiple instances of the same custom field. ([#13987](https://github.com/craftcms/cms/issues/13987))

Expand Down
2 changes: 1 addition & 1 deletion src/console/controllers/SectionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public function actionCreate(): int

if (isset($this->entryTypes)) {
foreach (explode(',', $this->entryTypes) as $entryTypeHandle) {
$entryType = $entriesService->getEntryTypesByHandle($entryTypeHandle)[0] ?? null;
$entryType = $entriesService->getEntryTypeByHandle($entryTypeHandle);
if (!$entryType) {
throw new InvalidArgumentException("Invalid entry type handle: $entryTypeHandle");
}
Expand Down
2 changes: 1 addition & 1 deletion src/elements/db/EntryQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ public function type(mixed $value): static
{
if (Db::normalizeParam($value, function($item) {
if (is_string($item)) {
$item = Craft::$app->getEntries()->getEntryTypesByHandle($item);
$item = Craft::$app->getEntries()->getEntryTypeByHandle($item);
}
return $item instanceof EntryType ? $item->id : null;
})) {
Expand Down
66 changes: 33 additions & 33 deletions src/services/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ public function __serialize()
* ---
*
* ```php
* $sectionIds = Craft::$app->sections->allSectionIds;
* $sectionIds = Craft::$app->entries->allSectionIds;
* ```
* ```twig
* {% set sectionIds = craft.app.sections.allSectionIds %}
* {% set sectionIds = craft.app.entries.allSectionIds %}
* ```
*
* @return int[] All the sections’ IDs.
Expand All @@ -187,10 +187,10 @@ public function getAllSectionIds(): array
* ---
*
* ```php
* $sectionIds = Craft::$app->sections->editableSectionIds;
* $sectionIds = Craft::$app->entries->editableSectionIds;
* ```
* ```twig
* {% set sectionIds = craft.app.sections.editableSectionIds %}
* {% set sectionIds = craft.app.entries.editableSectionIds %}
* ```
*
* @return int[] All the editable sections’ IDs.
Expand Down Expand Up @@ -279,10 +279,10 @@ private function _createSectionQuery(): Query
* ---
*
* ```php
* $sections = Craft::$app->sections->allSections;
* $sections = Craft::$app->entries->allSections;
* ```
* ```twig
* {% set sections = craft.app.sections.allSections %}
* {% set sections = craft.app.entries.allSections %}
* ```
*
* @return Section[] All the sections.
Expand All @@ -299,10 +299,10 @@ public function getAllSections(): array
* ---
*
* ```php
* $sections = Craft::$app->sections->editableSections;
* $sections = Craft::$app->entries->editableSections;
* ```
* ```twig
* {% set sections = craft.app.sections.editableSections %}
* {% set sections = craft.app.entries.editableSections %}
* ```
*
* @return Section[] All the editable sections.
Expand Down Expand Up @@ -333,10 +333,10 @@ public function getEditableSections(): array
* ```php
* use craft\models\Section;
*
* $singles = Craft::$app->sections->getSectionsByType(Section::TYPE_SINGLE);
* $singles = Craft::$app->entries->getSectionsByType(Section::TYPE_SINGLE);
* ```
* ```twig
* {% set singles = craft.app.sections.getSectionsByType('single') %}
* {% set singles = craft.app.entries.getSectionsByType('single') %}
* ```
*
* @param string $type The section type (`single`, `channel`, or `structure`)
Expand All @@ -354,10 +354,10 @@ public function getSectionsByType(string $type): array
* ---
*
* ```php
* $total = Craft::$app->sections->totalSections;
* $total = Craft::$app->entries->totalSections;
* ```
* ```twig
* {% set total = craft.app.sections.totalSections %}
* {% set total = craft.app.entries.totalSections %}
* ```
*
* @return int
Expand All @@ -374,10 +374,10 @@ public function getTotalSections(): int
* ---
*
* ```php
* $total = Craft::$app->sections->totalEditableSections;
* $total = Craft::$app->entries->totalEditableSections;
* ```
* ```twig
* {% set total = craft.app.sections.totalEditableSections %}
* {% set total = craft.app.entries.totalEditableSections %}
* ```
*
* @return int
Expand All @@ -394,10 +394,10 @@ public function getTotalEditableSections(): int
* ---
*
* ```php
* $section = Craft::$app->sections->getSectionById(1);
* $section = Craft::$app->entries->getSectionById(1);
* ```
* ```twig
* {% set section = craft.app.sections.getSectionById(1) %}
* {% set section = craft.app.entries.getSectionById(1) %}
* ```
*
* @param int $sectionId
Expand All @@ -415,10 +415,10 @@ public function getSectionById(int $sectionId): ?Section
* ---
*
* ```php
* $section = Craft::$app->sections->getSectionByUid('b3a9eef3-9444-4995-84e2-6dc6b60aebd2');
* $section = Craft::$app->entries->getSectionByUid('b3a9eef3-9444-4995-84e2-6dc6b60aebd2');
* ```
* ```twig
* {% set section = craft.app.sections.getSectionByUid('b3a9eef3-9444-4995-84e2-6dc6b60aebd2') %}
* {% set section = craft.app.entries.getSectionByUid('b3a9eef3-9444-4995-84e2-6dc6b60aebd2') %}
* ```
*
* @param string $uid
Expand All @@ -436,10 +436,10 @@ public function getSectionByUid(string $uid): ?Section
* ---
*
* ```php
* $section = Craft::$app->sections->getSectionByHandle('news');
* $section = Craft::$app->entries->getSectionByHandle('news');
* ```
* ```twig
* {% set section = craft.app.sections.getSectionByHandle('news') %}
* {% set section = craft.app.entries.getSectionByHandle('news') %}
* ```
*
* @param string $sectionHandle
Expand Down Expand Up @@ -517,7 +517,7 @@ private function _createSectionSiteSettingsQuery(): Query
* ]
* ]);
*
* $success = Craft::$app->sections->saveSection($section);
* $success = Craft::$app->entries->saveSection($section);
* ```
*
* @param Section $section The section to be saved
Expand Down Expand Up @@ -960,7 +960,7 @@ private function _ensureSingleEntry(Section $section, ?array $siteSettings = nul
* ---
*
* ```php
* $success = Craft::$app->sections->deleteSectionById(1);
* $success = Craft::$app->entries->deleteSectionById(1);
* ```
*
* @param int $sectionId
Expand All @@ -985,7 +985,7 @@ public function deleteSectionById(int $sectionId): bool
* ---
*
* ```php
* $success = Craft::$app->sections->deleteSection($section);
* $success = Craft::$app->entries->deleteSection($section);
* ```
*
* @param Section $section
Expand Down Expand Up @@ -1148,7 +1148,7 @@ public function pruneDeletedField(): void
* ---
*
* ```php
* $entryTypes = Craft::$app->sections->getEntryTypesBySectionId(1);
* $entryTypes = Craft::$app->entries->getEntryTypesBySectionId(1);
* ```
*
* @param int $sectionId
Expand Down Expand Up @@ -1252,7 +1252,7 @@ private function _createEntryTypeQuery(): Query
* ---
*
* ```php
* $entryTypes = Craft::$app->sections->getAllEntryTypes();
* $entryTypes = Craft::$app->entries->getAllEntryTypes();
* ```
*
* @return EntryType[]
Expand All @@ -1269,7 +1269,7 @@ public function getAllEntryTypes(): array
* ---
*
* ```php
* $entryType = Craft::$app->sections->getEntryTypeById(1);
* $entryType = Craft::$app->entries->getEntryTypeById(1);
* ```
*
* @param int $entryTypeId
Expand All @@ -1294,21 +1294,21 @@ public function getEntryTypeByUid(string $uid): ?EntryType
}

/**
* Returns entry types that have a given handle.
* Returns an entry type by its handle.
*
* ---
*
* ```php
* $entryTypes = Craft::$app->sections->getEntryTypesByHandle('article');
* $entryType = Craft::$app->entries->getEntryTypeByHandle('article');
* ```
*
* @param string $entryTypeHandle
* @return EntryType[]
* @return EntryType|null
* @since 5.0.0
*/
public function getEntryTypesByHandle(string $entryTypeHandle): array
public function getEntryTypeByHandle(string $entryTypeHandle): ?EntryType
{
return $this->_entryTypes()->where('handle', $entryTypeHandle, true)->all();
return $this->_entryTypes()->firstWhere('handle', $entryTypeHandle, true);
}

/**
Expand Down Expand Up @@ -1478,7 +1478,7 @@ public function handleChangedEntryType(ConfigEvent $event): void
* ---
*
* ```php
* $success = Craft::$app->sections->deleteEntryTypeById(1);
* $success = Craft::$app->entries->deleteEntryTypeById(1);
* ```
*
* @param int $entryTypeId
Expand All @@ -1503,7 +1503,7 @@ public function deleteEntryTypeById(int $entryTypeId): bool
* ---
*
* ```php
* $success = Craft::$app->sections->deleteEntryType($entryType);
* $success = Craft::$app->entries->deleteEntryType($entryType);
* ```
*
* @param EntryType $entryType
Expand Down

0 comments on commit 061b863

Please sign in to comment.