Skip to content

[4.1] Ad-hoc single asset indexing #11998

Answered by brandonkelly
rob-c-baker asked this question in Q&A
Discussion options

You must be logged in to vote

I’d recommend you just stick to the normal assets service API. Any changes made with that will automatically be indexed.

use Craft;
use craft\elements\Asset;
use craft\helpers\Assets;

$tempPath = '/path/to/new/thumb.png';
$volumeHandle = 'volumeHandle';
$folderPath = 'the/folder/path';

$filename = Assets::prepareAssetName($tempPath);
$volume = Craft::$app->volumes->getVolumeByHandle($volumeHandle);
$folder = Craft::$app->assets->ensureFolderByFullPathAndVolume($folderPath, $volume);

$asset = Asset::find()
    ->volume($volume)
    ->folderId($folder->id)
    ->one();

if ($asset) {
    // Replace the existing thumb with the new one
    Craft::$app->assets->replaceAssetFile($asset, $tem…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rob-c-baker
Comment options

Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants