Skip to content

Bugfix/17005 replacing assets via gql #17031

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

Merged
merged 4 commits into from
Apr 7, 2025
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Craft CMS 4

## Unreleased

- Fixed a bug where `craft\services\Assets::EVENT_BEFORE_REPLACE_ASSET` and `EVENT_BEFORE_REPLACE_ASSET` events weren’t getting triggered when replacing an asset file via GraphQL. ([#17005](https://github.com/craftcms/cms/issues/17005))
- Fixed a bug where replacing a file via GraphQL could result in two assets referring to the same file. ([#17031](https://github.com/craftcms/cms/pull/17031))

## 4.14.13 - 2025-04-04

- Fixed an error that could occur when clearing control panel resources, if the `resourceBasePath` setting was set to a nonexistent folder path. ([#17021](https://github.com/craftcms/cms/pull/17021))
Expand Down
32 changes: 31 additions & 1 deletion src/gql/resolvers/mutations/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
use craft\base\ElementInterface;
use craft\db\Table;
use craft\elements\Asset as AssetElement;
use craft\events\ReplaceAssetEvent;
use craft\gql\base\ElementMutationResolver;
use craft\helpers\Assets as AssetsHelper;
use craft\helpers\Db;
use craft\helpers\FileHelper;
use craft\helpers\UrlHelper;
use craft\models\Volume;
use craft\services\Assets;
use GraphQL\Error\Error;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
Expand All @@ -36,6 +38,8 @@ class Asset extends ElementMutationResolver
/** @inheritdoc */
protected array $immutableAttributes = ['id', 'uid', 'volumeId', 'folderId'];

private ?string $filename = null;

/**
* Save an asset using the passed arguments.
*
Expand Down Expand Up @@ -98,11 +102,32 @@ public function saveAsset(mixed $source, array $arguments, mixed $context, Resol
}
}

/** @var AssetElement $asset */
$asset->setVolumeId($volume->id);

$asset = $this->populateElementWithData($asset, $arguments, $resolveInfo);
$triggerReplaceEvents = (
$asset->getScenario() === AssetElement::SCENARIO_REPLACE &&
$assetService->hasEventHandlers(Assets::EVENT_BEFORE_REPLACE_ASSET)
);

if ($triggerReplaceEvents) {
$assetService->trigger(Assets::EVENT_BEFORE_REPLACE_ASSET, new ReplaceAssetEvent([
'asset' => $asset,
'replaceWith' => $asset->tempFilePath,
'filename' => $this->filename,
]));
}

$asset = $this->saveElement($asset);

if ($triggerReplaceEvents) {
$assetService->trigger(Assets::EVENT_AFTER_REPLACE_ASSET, new ReplaceAssetEvent([
'asset' => $asset,
'filename' => $this->filename,
]));
}

return $elementService->getElementById($asset->id, AssetElement::class);
}

Expand Down Expand Up @@ -225,7 +250,12 @@ protected function handleUpload(AssetElement $asset, array $fileInformation): bo
}

$asset->tempFilePath = $tempPath;
$asset->setFilename($filename);
$this->filename = $filename;
if ($asset->id !== null && $asset->getFilename() !== $filename) {
$asset->newFilename = $filename;
} else {
$asset->setFilename($filename);
}
$asset->avoidFilenameConflicts = true;

return true;
Expand Down