[4.x]: Assets are moved when using `{site.handle}` in Asset Location
What happened?
Description
Assets are being saved to the wrong folder when uploading to a field with 'Restrict assets to a single location' and {site.handle} inside the Asset Location. Able to reproduce on 3.7.67 and 4.3.10.
Steps to reproduce
- On a multisite install with Site A and Site B
- Add an Assets field with 'Restrict assets to a single location' checked and the Asset Location
{site.handle} - On a new Entry in Site A, upload an image -> save Entry
- Asset will be saved to the
siteBfolder - Switch to Site B, upload another asset
- All assets will be moved to
siteAfolder
Expected behavior
Assets will be saved to the folder corresponding to the site being edited.
Actual behavior
All Assets are moved after saving an Entry.
Here are Composer files and a database dump from a fresh install with the above setup to hopefully help quickly reproduce: composer-db.zip
Craft CMS version
4.3.10 and 3.7.67
PHP version
8.0
Operating system and version
No response
Database type and version
MariaDB 10.4
Image driver and version
No response
Installed plugins and versions
@i-just We probably should not be moving assets if $element->propagating from Assets::afterElementSave().
@brandonkelly We've been experiencing this on a project we upgraded to craft 4, the majority of our asset fields have {site.handle} as an asset location. The behaviour we are seeing is that for site shared elements often the asset gets 'moved' to the last site in the sites query, which typically is the last site when grouped by the site group (in alphabetical order) same as it is displayed in the sites area of the control panel.
The original motivation behind using {site.handle} in the asset location was to try to prevent people using different sites from seeing the assets from the other sites.
When I say shared elements i mean those that retain the same element ID across multiple sites, e.g. categories, singles, or any entry that is propagated to all sites.
Am i right in assuming that in this scenario (where the same element ID is retained across multiple sites), that the use of {site.handle} in the upload / folder location of the field settings is incompatible because the asset volume folders are not multi site, and the asset itself can only have one folderId?
Am i right in assuming that in this scenario (where the same element ID is retained across multiple sites), that the use of {site.handle} in the upload / folder location of the field settings is incompatible because the asset volume folders are not multi site, and the asset itself can only have one folderId?
@jlawrence-yellostudio Correct
Craft 4.14.12 and 5.6.14 are out with a fix for this: assets are no longer relocated for propagated sites. (#16938)