Skip to content

Commit

Permalink
getAvailableEntryTypes() + EVENT_DEFINE_ENTRY_TYPES
Browse files Browse the repository at this point in the history
Resolves #7136
  • Loading branch information
brandonkelly committed Nov 17, 2020
1 parent 73e2cd1 commit d5ed83a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-v3.6.md
Expand Up @@ -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()`.
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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()`.
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/EntriesController.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/EntryRevisionsController.php
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
34 changes: 33 additions & 1 deletion src/elements/Entry.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 = [];
Expand Down
24 changes: 24 additions & 0 deletions src/events/DefineEntryTypesEvent.php
@@ -0,0 +1,24 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\events;

use yii\base\Event;

/**
* Define entry types event class.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.6.0
*/
class DefineEntryTypesEvent extends Event
{
/**
* @var array The available entry types
*/
public $entryTypes = [];
}

0 comments on commit d5ed83a

Please sign in to comment.