Skip to content

Commit fa6fd63

Browse files
committed
Asset query folderPath param + really fixed #9619
1 parent fbf1709 commit fa6fd63

4 files changed

Lines changed: 120 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
## Unreleased
44

5+
### Added
6+
- Added the `folderPath` asset query param.
7+
- Added `craft\services\Assets::getTempVolumeAndSubpath()`.
8+
- Added `craft\services\Assets::createTempAssetQuery()`.
9+
510
### Changed
611
- Fields’ search indexes are now immediately removed when their “Use this field’s values as search keywords” settings are disabled.
712

813
### Fixed
914
- Fixed a bug where Matrix blocks could lose their Date field values, if multiple Date fields had with the same handle across different block types. ([#10884](https://github.com/craftcms/cms/issues/10884))
1015
- Fixed a bug where Matrix block menus were getting truncated if they were too large to fit within the content pane. ([#10883](https://github.com/craftcms/cms/issues/10883))
16+
- Fixed a bug where Assets fields weren’t respecting their dynamic folder path settings for unpublished drafts, if the Temp Upload Location asset setting was set to a volume. ([#9619](https://github.com/craftcms/cms/issues/9619))
1117

1218
## 3.7.38 - 2022-04-06
1319

src/elements/db/AssetQuery.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ private static function _supportsUploaderParam(): bool
206206
*/
207207
public $includeSubfolders = false;
208208

209+
/**
210+
* @var string|null The folder path that resulting assets must live within
211+
* @sused-by folderPath()
212+
* @since 3.7.39
213+
*/
214+
public $folderPath;
215+
209216
/**
210217
* @var string|array|null The asset transform indexes that should be eager-loaded, if they exist
211218
* ---
@@ -737,6 +744,46 @@ public function includeSubfolders(bool $value = true)
737744
return $this;
738745
}
739746

747+
/**
748+
* Narrows the query results based on the folders the assets belong to, per the folders’ paths.
749+
*
750+
* Possible values include:
751+
*
752+
* | Value | Fetches assets…
753+
* | - | -
754+
* | `foo/` | in a `foo/` folder (excluding nested folders).
755+
* | `foo/*` | in a `foo/` folder (including nested folders).
756+
* | `'not foo/*'` | not in a `foo/` folder (including nested folders).
757+
* | `['foo/*', 'bar/*']` | in a `foo/` or `bar/` folder (including nested folders).
758+
* | `['not', 'foo/*', 'bar/*']` | not in a `foo/` or `bar/` folder (including nested folders).
759+
*
760+
* ---
761+
*
762+
* ```twig
763+
* {# Fetch assets in the foo/ folder or its nested folders #}
764+
* {% set {elements-var} = {twig-method}
765+
* .folderPath('foo/*')
766+
* .all() %}
767+
* ```
768+
*
769+
* ```php
770+
* // Fetch assets in the foo/ folder or its nested folders
771+
* ${elements-var} = {php-method}
772+
* ->folderPath('foo/*')
773+
* ->all();
774+
* ```
775+
*
776+
* @param mixed $value The property value
777+
* @return self self reference
778+
* @uses $folderPath
779+
* @since 3.7.39
780+
*/
781+
public function folderPath($value): self
782+
{
783+
$this->folderPath = $value;
784+
return $this;
785+
}
786+
740787
/**
741788
* Causes the query to return matching assets eager-loaded with image transform indexes.
742789
*
@@ -862,6 +909,10 @@ protected function beforePrepare(): bool
862909
$this->subQuery->andWhere($folderCondition);
863910
}
864911

912+
if ($this->folderPath) {
913+
$this->subQuery->andWhere(Db::parseParam('volumeFolders.path', $this->folderPath));
914+
}
915+
865916
if (self::_supportsUploaderParam() && $this->uploaderId) {
866917
$this->subQuery->andWhere(['uploaderId' => $this->uploaderId]);
867918
}

src/fields/Assets.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,8 @@ public function afterElementSave(ElementInterface $element, bool $isNew)
568568
});
569569
} else {
570570
// Find the files with temp sources and just move those.
571-
$assetsToMove = Asset::find()
571+
$assetsToMove = $assetsService->createTempAssetQuery()
572572
->id(ArrayHelper::getColumn($assets, 'id'))
573-
->volumeId(':empty:')
574573
->all();
575574
}
576575

src/services/Assets.php

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use craft\events\GetAssetThumbUrlEvent;
3333
use craft\events\GetAssetUrlEvent;
3434
use craft\events\ReplaceAssetEvent;
35+
use craft\helpers\ArrayHelper;
3536
use craft\helpers\Assets as AssetsHelper;
3637
use craft\helpers\DateTimeHelper;
3738
use craft\helpers\Db;
@@ -50,6 +51,7 @@
5051
use craft\volumes\Temp;
5152
use yii\base\Component;
5253
use yii\base\InvalidArgumentException;
54+
use yii\base\InvalidConfigException;
5355
use yii\base\NotSupportedException;
5456

5557
/**
@@ -1001,6 +1003,54 @@ public function getCurrentUserTemporaryUploadFolder()
10011003
return $this->getUserTemporaryUploadFolder();
10021004
}
10031005

1006+
/**
1007+
* Returns the temporary volume and subpath, if set.
1008+
*
1009+
* @return array
1010+
* @throws InvalidConfigException If the temp volume is invalid
1011+
* @since 3.7.39
1012+
*/
1013+
public function getTempVolumeAndSubpath(): array
1014+
{
1015+
$assetSettings = Craft::$app->getProjectConfig()->get('assets');
1016+
if (empty($assetSettings['tempVolumeUid'])) {
1017+
return [null, null];
1018+
}
1019+
1020+
$volume = Craft::$app->getVolumes()->getVolumeByUid($assetSettings['tempVolumeUid']);
1021+
1022+
if (!$volume) {
1023+
throw new InvalidConfigException("The Temp Uploads Location is set to an invalid volume UID: {$assetSettings['tempVolumeUid']}");
1024+
}
1025+
1026+
$subpath = ($assetSettings['tempSubpath'] ?? null) ?: null;
1027+
return [$volume, $subpath];
1028+
}
1029+
1030+
/**
1031+
* Creates an asset query that is configured to return assets in the temporary upload location.
1032+
*
1033+
* @return AssetQuery
1034+
* @throws InvalidConfigException If the temp volume is invalid
1035+
* @since 3.7.39
1036+
*/
1037+
public function createTempAssetQuery(): AssetQuery
1038+
{
1039+
/** @var VolumeInterface|null $volume */
1040+
/** @var string|null $subpath */
1041+
[$volume, $subpath] = $this->getTempVolumeAndSubpath();
1042+
$query = Asset::find();
1043+
if ($volume) {
1044+
$query->volumeId($volume->id);
1045+
if ($subpath) {
1046+
$query->folderPath("$subpath/*");
1047+
}
1048+
} else {
1049+
$query->volumeId(':empty:');
1050+
}
1051+
return $query;
1052+
}
1053+
10041054
/**
10051055
* Returns the given user's temporary upload folder.
10061056
*
@@ -1029,17 +1079,19 @@ public function getUserTemporaryUploadFolder(User $user = null)
10291079
}
10301080

10311081
// Is there a designated temp uploads volume?
1032-
$assetSettings = Craft::$app->getProjectConfig()->get('assets');
1033-
if (isset($assetSettings['tempVolumeUid'])) {
1034-
$volume = Craft::$app->getVolumes()->getVolumeByUid($assetSettings['tempVolumeUid']);
1035-
if (!$volume) {
1036-
throw new VolumeException(Craft::t('app', 'The volume set for temp asset storage is not valid.'));
1037-
}
1038-
$path = (isset($assetSettings['tempSubpath']) ? $assetSettings['tempSubpath'] . '/' : '') .
1039-
$folderName;
1040-
$folderId = $this->ensureFolderByFullPathAndVolume($path, $volume, false);
1082+
try {
1083+
/** @var VolumeInterface|null $tempVolume */
1084+
/** @var string|null $tempSubpath */
1085+
[$tempVolume, $tempSubpath] = $this->getTempVolumeAndSubpath();
1086+
} catch (InvalidConfigException $e) {
1087+
throw new VolumeException($e->getMessage(), 0, $e);
1088+
}
1089+
1090+
if ($tempVolume) {
1091+
$path = ($tempSubpath ? "$tempSubpath/" : '') . $folderName;
1092+
$folderId = $this->ensureFolderByFullPathAndVolume($path, $tempVolume, false);
10411093
return $this->findFolder([
1042-
'volumeId' => $volume->id,
1094+
'volumeId' => $tempVolume->id,
10431095
'id' => $folderId,
10441096
]);
10451097
}

0 commit comments

Comments
 (0)