Skip to content

Commit

Permalink
Enable saving assets without setting filename or kind attributes
Browse files Browse the repository at this point in the history
Resolves #11439
  • Loading branch information
brandonkelly committed Jun 17, 2022
1 parent aab29f7 commit eda9b0b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <kbd>Delete</kbd> key code, and the <kbd>Backspace</kbd> key code is now referenced by `Garnish.BACKSPACE_KEY`.

Expand Down
28 changes: 23 additions & 5 deletions src/elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit eda9b0b

Please sign in to comment.