Skip to content

Commit

Permalink
Merge pull request #14141 from craftcms/bugfix/11405-temp-uploads-loc…
Browse files Browse the repository at this point in the history
…ation-restriction

Bugfix/11405 temp uploads location restriction
  • Loading branch information
brandonkelly authored Jan 23, 2024
2 parents 96b05f4 + d2292c4 commit cb276fb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### Administration
- Added “Save and continue editing” actions to all core settings pages with full-page forms. ([#14168](https://github.com/craftcms/cms/discussions/14168))
- It’s no longer possible to select the temp asset volume within Assets fields. ([#11405](https://github.com/craftcms/cms/issues/11405), [#14141](https://github.com/craftcms/cms/pull/14141))
- Added the `utils/prune-orphaned-matrix-blocks` command. ([#14154](https://github.com/craftcms/cms/pull/14154))

### Extensibility
Expand Down
46 changes: 46 additions & 0 deletions src/fields/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,65 @@ protected function defineRules(): array
},
];

$rules[] = [
['sources', 'defaultUploadLocationSource', 'restrictedLocationSource'], 'validateNotTempVolume',
];

$rules[] = [['previewMode'], 'in', 'range' => [self::PREVIEW_MODE_FULL, self::PREVIEW_MODE_THUMBS], 'skipOnEmpty' => false];

return $rules;
}

/**
* Ensure that you can't select tempUploadsLocation volume as a source or default uploads location or restricted location for an Assets field.
*
* @param string $attribute
* @since 4.7.0
*/
public function validateNotTempVolume(string $attribute): void
{
[$tempVolume] = Craft::$app->getAssets()->getTempVolumeAndSubpath();
if ($tempVolume !== null) {
$tempVolumeKey = "volume:$tempVolume->uid";
$inputSources = $this->getInputSources();

if (
(in_array($attribute, ['source', 'sources']) && in_array($tempVolumeKey, $inputSources)) ||
($attribute == 'defaultUploadLocationSource' && $this->defaultUploadLocationSource === $tempVolumeKey) ||
($attribute == 'restrictedLocationSource' && $this->restrictedLocationSource === $tempVolumeKey)
) {
// intentionally not translating this since it's short-lived (>= 4.7, < 5.0) and dev-facing only.
$this->addError($attribute, "Volume “{$tempVolume->name}” is used to store temporary asset uploads, so it cannot be used in an Assets field.");
}
}
}

/**
* @inheritdoc
*/
public function getSourceOptions(): array
{
$sourceOptions = [];
/** @var Volume|null $tempVolume */
[$tempVolume] = Craft::$app->getAssets()->getTempVolumeAndSubpath();
if ($tempVolume) {
$tempVolumeKey = 'volume:' . $tempVolume->uid;
} else {
$tempVolumeKey = null;
}

foreach (Asset::sources('settings') as $volume) {
if ($tempVolumeKey !== null && $volume['key'] === $tempVolumeKey) {
// only allow it if already selected
if (
(!is_array($this->sources) || !in_array($tempVolumeKey, $this->sources)) &&
$this->defaultUploadLocationSource !== $tempVolumeKey &&
$this->restrictedLocationSource !== $tempVolumeKey
) {
continue;
}
}

if (!isset($volume['heading'])) {
$sourceOptions[] = [
'label' => $volume['label'],
Expand Down
4 changes: 2 additions & 2 deletions src/templates/_components/fieldtypes/Assets/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
sourceOptions: sourceOptions,
sourceValue: field.restrictedLocationSource,
subpathValue: field.restrictedLocationSubpath,
errors: field.getErrors('restrictedLocationSubpath')
errors: field.getErrors('restrictedLocationSource') + field.getErrors('restrictedLocationSubpath')
}) }}

{{ forms.checkboxField({
Expand Down Expand Up @@ -103,7 +103,7 @@
sourceOptions: sourceOptions,
sourceValue: field.defaultUploadLocationSource,
subpathValue: field.defaultUploadLocationSubpath,
errors: field.getErrors('defaultUploadLocationSubpath')
errors: field.getErrors('defaultUploadLocationSource') + field.getErrors('defaultUploadLocationSubpath')
}) }}
{% endtag %}

Expand Down
2 changes: 1 addition & 1 deletion src/templates/settings/assets/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
id: 'tempUploadLocation',
label: 'Temp Uploads Location'|t('app'),
instructions: 'Where do you want to store temporary asset uploads?'|t('app'),
warning: allVolumes is empty ? 'No volumes exist yet.'|t('app')
warning: allVolumes is empty ? 'No volumes exist yet.'|t('app') : 'Don’t select a volume that’s used by any Assets fields.',
}, tempVolumeInput) }}

<div class="buttons">
Expand Down

0 comments on commit cb276fb

Please sign in to comment.