Closed stuartcusackie closed 2 years ago
I have run some experiments and it seems fairly simple. I think we just need a couple of modifications to the new files that I have listed above:
src/Listeners/UpdateResponsiveReferences.php A simple clone of src/Listeners/UpdateAssetReferences.php Container name needs to be prefixed to the originalPath / newPath:
public function handle(AssetSaved $event)
{
$asset = $event->asset;
$container = $asset->container()->handle();
$originalPath = $asset->getOriginal('path');
$newPath = $asset->path();
if (! $originalPath || $originalPath === $newPath) {
return;
}
$this->getItemsContainingData()->each(function ($item) use ($container, $originalPath, $newPath) {
AssetReferenceUpdater::item($item)
->filterByContainer($container)
->updateReferences($container . '::' . $originalPath, $container . '::' . $newPath);
});
}
src/ResponsiveReferenceUpdater.php A simple clone of AssetReferenceUpdater.php with the following modifications:
/**
* Recursively update fields.
*
* @param \Illuminate\Support\Collection $fields
* @param null|string $dottedPrefix
*/
protected function recursivelyUpdateFields($fields, $dottedPrefix = null)
{
$this->updateResponsiveFieldValues($fields, $dottedPrefix);
}
/**
* Update assets field values.
*
* @param \Illuminate\Support\Collection $fields
* @param null|string $dottedPrefix
* @return $this
*/
protected function updateResponsiveFieldValues($fields, $dottedPrefix)
{
$fields
->filter(function ($field) {
return $field->type() === 'responsive'
&& $this->getConfiguredAssetsFieldContainer($field) === $this->container;
})
->each(function ($field) use ($dottedPrefix) {
$this->updateArrayValue($field, $dottedPrefix);
});
return $this;
}
Might not get around to it for a while but let me know if this looks good and if you want me to put together a PR.
Feel free to send a PR! If you feel up to it a test would be great as well
I think I can manage the PR but I am a total noob when it comes to writing tests. Leave it with me.
Thanks for taking the initiative. This is now available in v2.14.0. :tada:
Thanks for fixing it all up!
When you rename an asset folder in Statamic it updates all asset fields that references files in that folder automatically. This package doesn't seem to do that and renaming an asset folder breaks all of my responsive image fields.
Multiple actions need to be handled:
These Statamic core files seem to be responsible for the above behaviour on the default asset fields:
So I guess all we would need are these new files: