diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index f91313f1785..e8a922873ec 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -54,6 +54,7 @@ - All element sources now have “Edit” and “Delete” actions, even if the element type’s `defineActions()` method didn’t include them. ([#11383](https://github.com/craftcms/cms/discussions/11383)) - The “Set Status” and “Edit” element actions are now only available for elements whose `canSave()` method returned `true`. - Assets fields now reject uploaded files which don’t pass their “Selectable Assets Condition” setting. ([#11433](https://github.com/craftcms/cms/issues/11433)) +- It’s now possible to save new assets without setting their `filename` or `kind` attributes, as long as `newLocation` or `newFilename` is set. ([#11439](https://github.com/craftcms/cms/issues/11439)) - The `searchindex` table is now uses the InnoDB storage engine by default for MySQL installs. ([#11374](https://github.com/craftcms/cms/discussions/11374)) - `Garnish.DELETE_KEY` now refers to the actual Delete key code, and the Backspace key code is now referenced by `Garnish.BACKSPACE_KEY`. diff --git a/src/elements/Asset.php b/src/elements/Asset.php index 594fe19d65b..956bbd1aea2 100644 --- a/src/elements/Asset.php +++ b/src/elements/Asset.php @@ -2275,10 +2275,20 @@ public function copyWithTransform(mixed $transform): Asset */ public function beforeSave(bool $isNew): bool { - // newFolderId/newFilename => newLocation. + if (!isset($this->_filename)) { + if (isset($this->newLocation)) { + [, $this->filename] = Assets::parseFileLocation($this->newLocation); + } elseif (isset($this->newFilename)) { + $this->filename = $this->newFilename; + $this->newFilename = null; + } + } + if ($this->newFilename === '' || $this->newFilename === $this->getFilename()) { $this->newFilename = null; } + + // newFolderId/newFilename => newLocation if (isset($this->newFolderId) || isset($this->newFilename)) { $folderId = $this->newFolderId ?: $this->folderId; $filename = $this->newFilename ?? $this->_filename; @@ -2304,10 +2314,8 @@ public function beforeSave(bool $isNew): bool ])); } - // Set the kind based on filename, if not set already - if (!isset($this->kind) && isset($this->_filename)) { - $this->kind = Assets::getFileKindByExtension($this->_filename); - } + // Set the kind based on filename + $this->_setKind(); // Give it a default title based on the file name, if it doesn't have a title yet if (!$this->id && !$this->title) { @@ -2324,6 +2332,16 @@ public function beforeSave(bool $isNew): bool return parent::beforeSave($isNew); } + /** + * Sets the asset’s kind based on its filename. + */ + private function _setKind(): void + { + if (isset($this->_filename)) { + $this->kind = Assets::getFileKindByExtension($this->_filename); + } + } + /** * @inheritdoc * @throws InvalidConfigException