Skip to content

fix: replace array_merge for upload#1723

Open
ishetnogferre wants to merge 1 commit intocraftcms:6.xfrom
ishetnogferre:patch-1
Open

fix: replace array_merge for upload#1723
ishetnogferre wants to merge 1 commit intocraftcms:6.xfrom
ishetnogferre:patch-1

Conversation

@ishetnogferre
Copy link

Problem

When importing asset arrays where some assets already exist and others need to be uploaded, the use of array_merge() operations destroys the relationship between array positions and their corresponding sub-field data. This causes alt text, custom fields, and other asset metadata to be applied to the wrong assets.

Root Cause

The issue occurs in Assets.php:parseField() where:

  1. Line ~225: $foundElements = array_merge($foundElements, $ids) destroys array position mapping
  2. Lines ~250, ~256: Upload results are merged without preserving original positions
  3. Sub-field population: populateFields() maps field data using array keys that no longer correspond to original positions

Replace array_merge() operations with position-preserving array assignments:

Key Changes

  1. Initialize with position structure: $foundElements = array_fill_keys(array_keys($value), null)
  2. Preserve positions during asset finding: $foundElements[$key] = $id instead of array_merge()
  3. Maintain positions during uploads: $foundElements[$uploadKey] = $uploadedElements[0]
  4. Keep array keys during cleanup: Remove only null values, preserve indices

Related issues

Fixes #1722

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assets array position is skewed during reimport

1 participant