Skip to content

Commit

Permalink
Merge pull request #14654 from craftcms/feature/cms-1276-removed-entr…
Browse files Browse the repository at this point in the history
…y-type-show-validation-error-on-element-save

validate if entry type is still allowed
  • Loading branch information
brandonkelly authored Mar 23, 2024
2 parents 515b0c5 + 93a6d5e commit 1aed854
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
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

0 comments on commit 1aed854

Please sign in to comment.