From d5ed83a63b5195078250506a9786ba164ee91922 Mon Sep 17 00:00:00 2001 From: Brandon Kelly Date: Mon, 16 Nov 2020 16:01:14 -0800 Subject: [PATCH] getAvailableEntryTypes() + EVENT_DEFINE_ENTRY_TYPES Resolves #7136 --- CHANGELOG-v3.6.md | 3 ++ CHANGELOG.md | 3 ++ src/controllers/EntriesController.php | 6 ++-- src/controllers/EntryRevisionsController.php | 6 ++-- src/elements/Entry.php | 34 +++++++++++++++++++- src/events/DefineEntryTypesEvent.php | 24 ++++++++++++++ 6 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 src/events/DefineEntryTypesEvent.php diff --git a/CHANGELOG-v3.6.md b/CHANGELOG-v3.6.md index 49086b746f9..ed47d89e009 100644 --- a/CHANGELOG-v3.6.md +++ b/CHANGELOG-v3.6.md @@ -18,6 +18,9 @@ - Added `craft\console\Controller::passwordPrompt()`. - Added `craft\elements\db\ElementQueryInterface::afterPopulate()`. - Added `craft\elements\db\ElementQueryInterface::createElement()`. +- Added `craft\elements\Entry::EVENT_DEFINE_ENTRY_TYPES`. ([#7136](https://github.com/craftcms/cms/issues/7136)) +- Added `craft\elements\Entry::getAvailableEntryTypes()`. +- Added `craft\events\DefineEntryTypesEvent`. - Added `craft\fieldlayoutelements\AssetTitleField`. - Added `craft\helpers\Gql::eagerLoadComplexity()`. - Added `craft\helpers\Gql::nPlus1Complexity()`. diff --git a/CHANGELOG.md b/CHANGELOG.md index a5ac4c85a18..a7e29aa2788 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ - Added `craft\base\VolumeTrait::$titleTranslationKeyFormat`. - Added `craft\elements\db\ElementQueryInterface::afterPopulate()`. - Added `craft\elements\db\ElementQueryInterface::createElement()`. +- Added `craft\elements\Entry::EVENT_DEFINE_ENTRY_TYPES`. ([#7136](https://github.com/craftcms/cms/issues/7136)) +- Added `craft\elements\Entry::getAvailableEntryTypes()`. +- Added `craft\events\DefineEntryTypesEvent`. - Added `craft\fieldlayoutelements\AssetTitleField`. - Added `craft\helpers\Gql::eagerLoadComplexity()`. - Added `craft\helpers\Gql::nPlus1Complexity()`. diff --git a/src/controllers/EntriesController.php b/src/controllers/EntriesController.php index 022a81f8793..9c5f0598c7c 100644 --- a/src/controllers/EntriesController.php +++ b/src/controllers/EntriesController.php @@ -220,7 +220,7 @@ public function actionEditEntry(string $section, int $entryId = null, int $draft } // Multiple entry types? - $entryTypes = $section->getEntryTypes(); + $entryTypes = $entry->getAvailableEntryTypes(); if (count($entryTypes) > 1) { $variables['showEntryTypes'] = true; @@ -595,7 +595,7 @@ private function _prepEditEntryVariables(array &$variables) if (!$typeId) { // Default to the section's first entry type - $typeId = $variables['entry']->typeId ?? $variables['section']->getEntryTypes()[0]->id; + $typeId = $variables['entry']->typeId ?? $variables['entry']->getAvailableEntryTypes()[0]->id; } $variables['entry']->typeId = $typeId; @@ -738,7 +738,7 @@ private function _populateEntryModel(Entry $entry) if (!$entry->typeId) { // Default to the section's first entry type - $entry->typeId = $entry->getSection()->getEntryTypes()[0]->id; + $entry->typeId = $entry->getAvailableEntryTypes()[0]->id; } // Prevent the last entry type's field layout from being used diff --git a/src/controllers/EntryRevisionsController.php b/src/controllers/EntryRevisionsController.php index a3c2ee2fcf2..b896ddeac34 100644 --- a/src/controllers/EntryRevisionsController.php +++ b/src/controllers/EntryRevisionsController.php @@ -95,13 +95,13 @@ public function actionCreateDraft(string $section, string $site = null): Respons // Type if (($typeHandle = $this->request->getQueryParam('type')) !== null) { - $type = ArrayHelper::firstWhere($section->getEntryTypes(), 'handle', $typeHandle); + $type = ArrayHelper::firstWhere($entry->getAvailableEntryTypes(), 'handle', $typeHandle); if ($type === null) { throw new BadRequestHttpException("Invalid entry type handle: $typeHandle"); } $entry->typeId = $type->id; } else { - $entry->typeId = $this->request->getQueryParam('typeId') ?? $section->getEntryTypes()[0]->id; + $entry->typeId = $this->request->getQueryParam('typeId') ?? $entry->getAvailableEntryTypes()[0]->id; } // Status @@ -552,7 +552,7 @@ private function _setDraftAttributesFromPost(Entry $draft) if (!$draft->typeId) { // Default to the section's first entry type - $draft->typeId = $draft->getSection()->getEntryTypes()[0]->id; + $draft->typeId = $draft->getAvailableEntryTypes()[0]->id; // Prevent the last entry type's field layout from being used $draft->fieldLayoutId = null; } diff --git a/src/elements/Entry.php b/src/elements/Entry.php index 1baff35df6e..f650f0b9b07 100644 --- a/src/elements/Entry.php +++ b/src/elements/Entry.php @@ -25,6 +25,7 @@ use craft\elements\db\ElementQueryInterface; use craft\elements\db\EntryQuery; use craft\errors\UnsupportedSiteException; +use craft\events\DefineEntryTypesEvent; use craft\helpers\ArrayHelper; use craft\helpers\Cp; use craft\helpers\DateTimeHelper; @@ -57,6 +58,14 @@ class Entry extends Element const STATUS_PENDING = 'pending'; const STATUS_EXPIRED = 'expired'; + /** + * @event DefineEntryTypesEvent The event that is triggered when defining the available entry types for the entry + * + * @see getAvailableEntryTypes() + * @since 3.6.0 + */ + const EVENT_DEFINE_ENTRY_TYPES = 'defineEntryTypes'; + /** * @inheritdoc */ @@ -1024,6 +1033,29 @@ public function getSection(): Section return $section; } + /** + * Returns the available entry types for the entry. + * + * @return EntryType[] + * @throws InvalidConfigException + * @since 3.6.0 + */ + public function getAvailableEntryTypes(): array + { + $entryTypes = $this->getSection()->getEntryTypes(); + + // Fire a 'defineEntryTypes' event + if ($this->hasEventHandlers(self::EVENT_DEFINE_ENTRY_TYPES)) { + $event = new DefineEntryTypesEvent([ + 'entryTypes' => $entryTypes, + ]); + $this->trigger(self::EVENT_DEFINE_ENTRY_TYPES, $event); + $entryTypes = $event->entryTypes; + } + + return $entryTypes; + } + /** * Returns the entry type. * @@ -1295,7 +1327,7 @@ public function getEditorHtml(): string // Show the Entry Type field? if ($this->id === null) { - $entryTypes = $this->getSection()->getEntryTypes(); + $entryTypes = $this->getAvailableEntryTypes(); if (count($entryTypes) > 1) { $entryTypeOptions = []; diff --git a/src/events/DefineEntryTypesEvent.php b/src/events/DefineEntryTypesEvent.php new file mode 100644 index 00000000000..158cdf7558d --- /dev/null +++ b/src/events/DefineEntryTypesEvent.php @@ -0,0 +1,24 @@ + + * @since 3.6.0 + */ +class DefineEntryTypesEvent extends Event +{ + /** + * @var array The available entry types + */ + public $entryTypes = []; +}