Skip to content

Stop dropping media custom properties on create/edit - #2598

Open
wakqasahmed wants to merge 1 commit into
lunarphp:1.xfrom
wakqasahmed:fix/media-custom-properties-persistence
Open

Stop dropping media custom properties on create/edit#2598
wakqasahmed wants to merge 1 commit into
lunarphp:1.xfrom
wakqasahmed:fix/media-custom-properties-persistence

Conversation

@wakqasahmed

Copy link
Copy Markdown

Closes #2596

Summary

MediaRelationManager treated custom_properties as a closed set of exactly two keys (name, primary), which silently broke Spatie Media Library's open key/value contract in two ways:

  1. Create — the CreateAction handler enumerated only name/primary from the submitted form data and discarded everything else, so any field added via the documented RelationManagerExtension::extendForm() seam rendered, validated, and submitted — but its value was never persisted.
  2. EditEditAction wrote the submitted form data straight onto the model. Because custom_properties is an array-cast column, this replaced the whole JSON value instead of merging into it, so any key not rendered by the edit form (imported metadata, dedup markers, alt text, etc.) was silently dropped on save.

Fix

  • Create: pass the full submitted custom_properties array through to withCustomProperties() instead of hand-picking name/primary.
  • Edit: added mutateDataUsing() to the EditAction that merges the submitted custom_properties on top of the record's existing custom_properties, so unrendered keys survive while submitted keys still win.

Testing

  • Added a feature test asserting a custom property contributed via a RelationManagerExtension::extendForm() field is persisted when a media item is created.
  • Added a feature test asserting editing a media item preserves an existing custom property key that the edit form does not render, while still applying the change to the field the form does render.
  • Ran vendor/bin/pest tests/admin/Feature/Support (built a PHP 8.3 + intl/exif/bcmath/gd toolchain via Docker to install the monorepo's Composer dependencies): all 16 tests in that directory pass, including the 2 new tests and the pre-existing PriceRelationManagerTest/RelationManagerExtensionTest/CreatePageExtensionTest suites.
  • Verified the new "persists custom properties added by an extension when creating media" test fails against the pre-fix code (null instead of 'Jane Doe'), confirming it actually exercises bug 1.
  • php -l on both changed files (no syntax errors).

MediaRelationManager treated custom_properties as a closed set of
name/primary. The CreateAction handler enumerated only those two
keys, discarding anything an extension added via extendForm(). The
EditAction wrote submitted form data straight onto the array-cast
custom_properties column, replacing the whole JSON instead of
merging, so any key the edit form didn't render was silently lost.

Pass the submitted custom_properties array through on create, and
merge submitted values onto the record's existing custom_properties
on edit so unrendered keys survive.

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

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

[2.x] Media custom properties are silently dropped because MediaRelationManager hardcodes name/primary on create and overwrites the whole JSON on edit

1 participant