Skip to content

Commit

Permalink
Better Assets field upload location errors
Browse files Browse the repository at this point in the history
resolves #2803
  • Loading branch information
brandonkelly committed Apr 25, 2018
1 parent 739fe98 commit f9fec48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Changed
- Improved the error messages displayed when an Assets field has an invalid Upload Location setting. ([#2803](https://github.com/craftcms/cms/issues/2803))

### Fixed
- Fixed an error that would occur on servers without the Phar PHP extension enabled.

Expand Down
33 changes: 17 additions & 16 deletions src/fields/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ private function _getAllowedExtensions(): array
*
* @param ElementInterface|null $element
* @param bool $createDynamicFolders whether missing folders should be created in the process
* @return int if the folder subpath is not valid
* @return int
* @throws InvalidSubpathException if the folder subpath is not valid
* @throws InvalidVolumeException if there's a problem with the field's volume configuration
*/
Expand All @@ -660,38 +660,39 @@ private function _determineUploadFolderId(ElementInterface $element = null, bool
if ($this->useSingleFolder) {
$uploadVolume = $this->singleUploadLocationSource;
$subpath = $this->singleUploadLocationSubpath;
$settingName = Craft::t('app', 'Upload Location');
} else {
$uploadVolume = $this->defaultUploadLocationSource;
$subpath = $this->defaultUploadLocationSubpath;
}

if (!$uploadVolume) {
throw new InvalidVolumeException(Craft::t('app', 'This field’s Volume configuration is invalid.'));
$settingName = Craft::t('app', 'Default Upload Location');
}

$assets = Craft::$app->getAssets();

try {
$folderId = $this->_resolveVolumePathToFolderId($uploadVolume, $subpath, $element, $createDynamicFolders);
} catch (InvalidVolumeException $exception) {
if ($this->useSingleFolder) {
$message = Craft::t('app', 'This field’s single upload location Volume is missing');
} else {
$message = Craft::t('app', 'This field’s default upload location Volume is missing');
if (!$uploadVolume) {
throw new InvalidVolumeException();
}
throw new InvalidVolumeException($message);
} catch (InvalidSubpathException $exception) {
$folderId = $this->_resolveVolumePathToFolderId($uploadVolume, $subpath, $element, $createDynamicFolders);
} catch (InvalidVolumeException $e) {
throw new InvalidVolumeException(Craft::t('app', 'The {field} field’s {setting} setting is set to an invalid volume.', [
'field' => $this->name,
'setting' => $settingName,
]), 0, $e);
} catch (InvalidSubpathException $e) {
// If this is a new/disabled element, the subpath probably just contained a token that returned null, like {id}
// so use the user's upload folder instead
if ($element === null || !$element->id || !$element->enabled || !$createDynamicFolders) {
$userModel = Craft::$app->getUser()->getIdentity();

$userFolder = $assets->getUserTemporaryUploadFolder($userModel);

$folderId = $userFolder->id;
} else {
// Existing element, so this is just a bad subpath
throw $exception;
throw new InvalidSubpathException($e->subpath, Craft::t('app', 'The {field} field’s {setting} setting has an invalid subpath (“{subpath}”).', [
'field' => $this->name,
'setting' => $settingName,
'subpath' => $e->subpath,
]), 0, $e);
}
}

Expand Down

0 comments on commit f9fec48

Please sign in to comment.