1

Im trying to figure out how to move asset(s) to a different volume. Also if I move it will that affect the entries that are using the asset?

Here's what im using to target the asset: $resourceImage = Asset::find()->volume('resourceImages')->filename($filename);

I need to move it to my productImages volume.

Thanks!

3

You can move them like this

$asset = Asset::find()->volume('resourceImages')->filename($filename)->one()
$folder = Craft::$app->getAssets()->findFolder(['volumeId' => X]);
Craft::$app->getAssets()->moveAsset($asset, $folder, $filename);

It won't change your relations or anything else in fact all the function does is:

/**
 * Move or rename an Asset.
 *
 * @param Asset $asset The asset whose file should be renamed
 * @param VolumeFolder $folder The Volume Folder to move the Asset to.
 * @param string $filename The new filename
 * @return bool Whether the asset was renamed successfully
 * @throws AssetLogicException if the asset’s volume is missing
 */
public function moveAsset(Asset $asset, VolumeFolder $folder, string $filename = ''): bool
{
    // Set the new combined target location, and save it
    $asset->newFilename = $filename;
    $asset->newFolderId = $folder->id;
    $asset->setScenario(Asset::SCENARIO_FILEOPS);

    return Craft::$app->getElements()->saveElement($asset);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.