cms icon indicating copy to clipboard operation
cms copied to clipboard

[4.x]: Assets are moved when using `{site.handle}` in Asset Location

Open tommysvr opened this issue 3 years ago • 3 comments

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

  1. On a multisite install with Site A and Site B
  2. Add an Assets field with 'Restrict assets to a single location' checked and the Asset Location {site.handle}
  3. On a new Entry in Site A, upload an image -> save Entry
  4. Asset will be saved to the siteB folder
  5. Switch to Site B, upload another asset
  6. All assets will be moved to siteA folder

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

tommysvr avatar Mar 02 '23 04:03 tommysvr

@i-just We probably should not be moving assets if $element->propagating from Assets::afterElementSave().

brandonkelly avatar Mar 03 '23 03:03 brandonkelly

@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?

jlawrence-yellostudio avatar Aug 28 '24 15:08 jlawrence-yellostudio

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

brandonkelly avatar Aug 29 '24 22:08 brandonkelly

Craft 4.14.12 and 5.6.14 are out with a fix for this: assets are no longer relocated for propagated sites. (#16938)

brandonkelly avatar Apr 01 '25 23:04 brandonkelly