Skip to content

Commit

Permalink
Merge pull request #13039 from craftcms/feature/additional-list-volum…
Browse files Browse the repository at this point in the history
…e-event-checks

check list of passed volumes on start indexing

Resolves #12819
  • Loading branch information
brandonkelly committed Apr 4, 2023
2 parents f372e8b + 2adbc45 commit 97c731a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
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

0 comments on commit 97c731a

Please sign in to comment.