Skip to content

Commit cb276fb

Browse files
authored
Merge pull request #14141 from craftcms/bugfix/11405-temp-uploads-location-restriction
Bugfix/11405 temp uploads location restriction
2 parents 96b05f4 + d2292c4 commit cb276fb

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

CHANGELOG-WIP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
### Administration
77
- Added “Save and continue editing” actions to all core settings pages with full-page forms. ([#14168](https://github.com/craftcms/cms/discussions/14168))
8+
- 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))
89
- Added the `utils/prune-orphaned-matrix-blocks` command. ([#14154](https://github.com/craftcms/cms/pull/14154))
910

1011
### Extensibility

src/fields/Assets.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,65 @@ protected function defineRules(): array
238238
},
239239
];
240240

241+
$rules[] = [
242+
['sources', 'defaultUploadLocationSource', 'restrictedLocationSource'], 'validateNotTempVolume',
243+
];
244+
241245
$rules[] = [['previewMode'], 'in', 'range' => [self::PREVIEW_MODE_FULL, self::PREVIEW_MODE_THUMBS], 'skipOnEmpty' => false];
242246

243247
return $rules;
244248
}
245249

250+
/**
251+
* Ensure that you can't select tempUploadsLocation volume as a source or default uploads location or restricted location for an Assets field.
252+
*
253+
* @param string $attribute
254+
* @since 4.7.0
255+
*/
256+
public function validateNotTempVolume(string $attribute): void
257+
{
258+
[$tempVolume] = Craft::$app->getAssets()->getTempVolumeAndSubpath();
259+
if ($tempVolume !== null) {
260+
$tempVolumeKey = "volume:$tempVolume->uid";
261+
$inputSources = $this->getInputSources();
262+
263+
if (
264+
(in_array($attribute, ['source', 'sources']) && in_array($tempVolumeKey, $inputSources)) ||
265+
($attribute == 'defaultUploadLocationSource' && $this->defaultUploadLocationSource === $tempVolumeKey) ||
266+
($attribute == 'restrictedLocationSource' && $this->restrictedLocationSource === $tempVolumeKey)
267+
) {
268+
// intentionally not translating this since it's short-lived (>= 4.7, < 5.0) and dev-facing only.
269+
$this->addError($attribute, "Volume “{$tempVolume->name}” is used to store temporary asset uploads, so it cannot be used in an Assets field.");
270+
}
271+
}
272+
}
273+
246274
/**
247275
* @inheritdoc
248276
*/
249277
public function getSourceOptions(): array
250278
{
251279
$sourceOptions = [];
280+
/** @var Volume|null $tempVolume */
281+
[$tempVolume] = Craft::$app->getAssets()->getTempVolumeAndSubpath();
282+
if ($tempVolume) {
283+
$tempVolumeKey = 'volume:' . $tempVolume->uid;
284+
} else {
285+
$tempVolumeKey = null;
286+
}
252287

253288
foreach (Asset::sources('settings') as $volume) {
289+
if ($tempVolumeKey !== null && $volume['key'] === $tempVolumeKey) {
290+
// only allow it if already selected
291+
if (
292+
(!is_array($this->sources) || !in_array($tempVolumeKey, $this->sources)) &&
293+
$this->defaultUploadLocationSource !== $tempVolumeKey &&
294+
$this->restrictedLocationSource !== $tempVolumeKey
295+
) {
296+
continue;
297+
}
298+
}
299+
254300
if (!isset($volume['heading'])) {
255301
$sourceOptions[] = [
256302
'label' => $volume['label'],

src/templates/_components/fieldtypes/Assets/settings.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
sourceOptions: sourceOptions,
6363
sourceValue: field.restrictedLocationSource,
6464
subpathValue: field.restrictedLocationSubpath,
65-
errors: field.getErrors('restrictedLocationSubpath')
65+
errors: field.getErrors('restrictedLocationSource') + field.getErrors('restrictedLocationSubpath')
6666
}) }}
6767

6868
{{ forms.checkboxField({
@@ -103,7 +103,7 @@
103103
sourceOptions: sourceOptions,
104104
sourceValue: field.defaultUploadLocationSource,
105105
subpathValue: field.defaultUploadLocationSubpath,
106-
errors: field.getErrors('defaultUploadLocationSubpath')
106+
errors: field.getErrors('defaultUploadLocationSource') + field.getErrors('defaultUploadLocationSubpath')
107107
}) }}
108108
{% endtag %}
109109

src/templates/settings/assets/settings.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
id: 'tempUploadLocation',
5555
label: 'Temp Uploads Location'|t('app'),
5656
instructions: 'Where do you want to store temporary asset uploads?'|t('app'),
57-
warning: allVolumes is empty ? 'No volumes exist yet.'|t('app')
57+
warning: allVolumes is empty ? 'No volumes exist yet.'|t('app') : 'Don’t select a volume that’s used by any Assets fields.',
5858
}, tempVolumeInput) }}
5959

6060
<div class="buttons">

0 commit comments

Comments
 (0)