Problem/Motivation
In a multilingual site, when creating media items via the media library widget, the created media items do not always have the same langcode as the parent entity through which they are created. The media library widget simply creates a media item without specifying a langcode, which makes the langcode default to the sites default langcode.
Since users add an alt text in the language of the parent content, this can be problematic.
Steps to reproduce
- Setup a multilingual site.
- Add a media field to a content type with the media library widget.
- Create a translation for existing content, or new content in a non-default language.
- Add a media item to the content using the media library widget.
- Observe that the created media item has the default langcode, instead of the langcode of the content through which it was created.
Proposed resolution
Pass the langcode of the parent entity to the media library, and use it to create new media items.
Remaining tasks
Write patch
Add tests
Review
Commit
User interface changes
None
API changes
A new optional langcode parameter is added to AddFormBase::createMediaFromValue. It is a protected method, but media library add forms extending the class might need to be updated.
Data model changes
None
Release notes snippet
Issue fork drupal-3450772
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
seanbCreated a MR. Let's see if this breaks anything.
Comment #4
smustgrave commentedMakes sense to me.
Can we add a test case to show that behavior?
Thanks!
Comment #6
vidorado commentedFirst of all, congratulations to @seanb! The media library code was not trivial, and their fix was both well-executed and clean.
After manually testing, I noticed that the
langcodewas not being properly set in the translation add form. To fix this, I modified the code to retrieve thelangcodefrom the form_state storage, which turned out to contain the target translation’s langcode.Since the documentation states that "no specific support is provided for it in the Form API" regarding the
FormState::storagevariable, I added a fallback to the original fix $entity's language, which is for sure always available:Additionally, I’ve included an FJ test. :)
Comment #7
smustgrave commentedLeft some comments on the MR
thanks.
Comment #9
vidorado commentedAddressed! :)
Comment #10
smustgrave commentedleft some comments on the MR.