Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate if entry type is still allowed #14654

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Moved the “Forgot password?” link below the Password input on the control panel login page. ([#14643](https://github.com/craftcms/cms/pull/14643))
- Selectize inputs no longer automatically select the hovered option on <kbd>Tab</kbd> press. ([selectize/selectize.js#2085](https://github.com/selectize/selectize.js/issues/2085))
- Entries now validate that their selected type is allowed by the section. ([#14654](https://github.com/craftcms/cms/pull/14654))
- Fixed a bug where filesystems’ `afterSave()` and `afterDelete()` methods weren’t getting called. ([#14634](https://github.com/craftcms/cms/pull/14634))
- Fixed an error that could occur on `elements/recent-activity` Ajax requests when editing an element. ([#14635](https://github.com/craftcms/cms/issues/14635))
- Fixed a bug where asset queries’ `hasAlt` param wasn’t working. ([#14642](https://github.com/craftcms/cms/pull/14642))
Expand Down
34 changes: 33 additions & 1 deletion src/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,18 @@ protected function defineRules(): array
'required',
'when' => fn() => !isset($this->fieldId),
];
$rules[] = [
['typeId'],
function(string $attribute) {
if (!$this->isEntryTypeAllowed()) {
$this->addError($attribute, Craft::t('app', '{type} entries are no longer allowed in this section. Please choose a different entry type.', [
'type' => $this->getType()->getUiLabel(),
]));
}
},
'skipOnEmpty' => false,
'when' => fn() => $this->getIsCanonical(),
];
$rules[] = [['fieldId'], function(string $attribute) {
if (isset($this->sectionId)) {
$this->addError($attribute, Craft::t('app', '`sectionId` and `fieldId` cannot both be set on an entry.'));
Expand Down Expand Up @@ -1934,7 +1946,10 @@ public function metaFieldsHtml(bool $static): string
// Type
$fields[] = (function() use ($static) {
$entryTypes = $this->getAvailableEntryTypes();
if (count($entryTypes) <= 1) {
if (!ArrayHelper::contains($entryTypes, fn(EntryType $entryType) => $entryType->id === $this->typeId)) {
$entryTypes[] = $this->getType();
}
if (count($entryTypes) <= 1 && $this->isEntryTypeAllowed($entryTypes)) {
return null;
}

Expand Down Expand Up @@ -2570,4 +2585,21 @@ private function _shouldSaveRevision(): bool
$this->getSection()?->enableVersioning
);
}

/**
* Check if current typeId is in the array of passed in entry types.
* If no entry types are passed, check get all the available ones.
*
* @param array|null $entryTypes
* @return bool
* @throws InvalidConfigException
*/
private function isEntryTypeAllowed(array|null $entryTypes = null): bool
{
if ($entryTypes === null) {
$entryTypes = $this->getAvailableEntryTypes();
}

return in_array($this->typeId, array_map(fn($entryType) => $entryType->id, $entryTypes));
}
}
1 change: 1 addition & 0 deletions src/translations/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,7 @@
'{type} deleted for site.' => '{type} deleted for site.',
'{type} deleted.' => '{type} deleted.',
'{type} duplicated.' => '{type} duplicated.',
'{type} entries are no longer allowed in this section. Please choose a different entry type.' => '{type} entries are no longer allowed in this section. Please choose a different entry type.',
'{type} not restored.' => '{type} not restored.',
'{type} restored.' => '{type} restored.',
'{type} reverted to past revision.' => '{type} reverted to past revision.',
Expand Down
Loading