Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check list of passed volumes on start indexing #13039

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- Selectize menus now expand upwards when there’s not ample space below them. ([#12976](https://github.com/craftcms/cms/issues/12976))
- Element index bulk action spinners are now centered on the viewport. ([#12972](https://github.com/craftcms/cms/issues/12972))
- All control panel errors are new presented via error notifications rather than browser alerts. ([#13024](https://github.com/craftcms/cms/issues/13024))
- Added `craft\utilities\AssetIndexes::volumes()`.
- `craft\controllers\AssetIndexesController::actionStartIndexing()` now cross-references the selected volumes with those allowed by `craft\utilities\AssetIndexes::EVENT_LIST_VOLUMES` event handlers. ([#13039](https://github.com/craftcms/cms/pull/13039), [#12819](https://github.com/craftcms/cms/pull/12819))
- Fixed a bug where Assets fields weren’t respecting their View Mode setting when viewing entry revisions. ([#12948](https://github.com/craftcms/cms/issues/12948))
- Fixed a bug where asset pagination was broken when there was more than 100 subfolders. ([#12969](https://github.com/craftcms/cms/issues/12969))
- Fixed a bug where entry index pages’ “Revision Notes” and “Last Edited By” columns weren’t getting populated for disabled entries. ([#12981](https://github.com/craftcms/cms/issues/12981))
Expand Down
13 changes: 10 additions & 3 deletions src/controllers/AssetIndexesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use craft\helpers\Json;
use craft\i18n\Locale;
use craft\models\AssetIndexingSession;
use craft\models\Volume;
use craft\utilities\AssetIndexes;
use craft\web\Controller;
use Throwable;
use yii\web\BadRequestHttpException;
Expand Down Expand Up @@ -50,15 +52,20 @@ public function beforeAction($action): bool
public function actionStartIndexing(): Response
{
$request = Craft::$app->getRequest();
$volumes = (array)$request->getRequiredBodyParam('volumes');
$volumeIds = (array)$request->getRequiredBodyParam('volumes');
$cacheRemoteImages = (bool)$request->getBodyParam('cacheImages', false);
$listEmptyFolders = (bool)$request->getBodyParam('listEmptyFolders', false);

if (empty($volumes)) {
// Typecast volume IDs and filter out any disallowed volumes
$volumeIds = array_map(fn($volumeId) => (int)$volumeId, $volumeIds);
$allowedVolumeIds = array_map(fn(Volume $volume) => $volume->id, AssetIndexes::volumes());
$volumeIds = array_intersect($volumeIds, $allowedVolumeIds);

if (empty($volumeIds)) {
return $this->asFailure(Craft::t('app', 'No volumes specified.'));
}

$indexingSession = Craft::$app->getAssetIndexer()->startIndexingSession($volumes, $cacheRemoteImages, $listEmptyFolders);
$indexingSession = Craft::$app->getAssetIndexer()->startIndexingSession($volumeIds, $cacheRemoteImages, $listEmptyFolders);
$sessionData = $this->prepareSessionData($indexingSession);

$data = ['session' => $sessionData];
Expand Down
19 changes: 15 additions & 4 deletions src/utilities/AssetIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use craft\events\ListVolumesEvent;
use craft\helpers\Html;
use craft\i18n\Locale;
use craft\models\Volume;
use craft\web\assets\assetindexes\AssetIndexesAsset;
use yii\base\Event;

Expand Down Expand Up @@ -54,19 +55,29 @@ public static function iconPath(): ?string
}

/**
* @inheritdoc
* Returns all of the available volumes for indexing.
*
* @return Volume[]
* @since 4.4.6
*/
public static function contentHtml(): string
public static function volumes(): array
{
// Fire a 'listVolumes' event
$event = new ListVolumesEvent([
'volumes' => Craft::$app->getVolumes()->getAllVolumes(),
]);
Event::trigger(self::class, self::EVENT_LIST_VOLUMES, $event,);
Event::trigger(self::class, self::EVENT_LIST_VOLUMES, $event);
return $event->volumes;
}

/**
* @inheritdoc
*/
public static function contentHtml(): string
{
$volumeOptions = [];

foreach ($event->volumes as $volume) {
foreach (static::volumes() as $volume) {
$volumeOptions[] = [
'label' => Html::encode($volume->name),
'value' => $volume->id,
Expand Down