Problem/Motivation
Developers have no easy way of adding the Media Library to a custom form. A real world use case would be creating a custom layout plugin for Layout Builder that gives a content editor the power to select a background image from existing media in Drupal. The only current solution is to use the entity_autocomplete element or the entity_browser element which is not very straight forward how to use.
Proposed resolution
Abstract the Media Library from the MediaLibraryWidget to a media library form element. The widget will instead call the media library form element and the media library can be used outside of the Field API.
Remaining tasks
TBD
User interface changes
There should be no UI changes.
API changes
The media library will be available to all forms.
Data model changes
None.
Release notes snippet
TBD.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | 3065677-6.patch | 13.07 KB | mark_fullmer |
Issue fork drupal-3065677
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 #2
nathandentzauComment #3
phenaproxima+1 for this idea!
Comment #4
nathandentzauComment #5
nathandentzauComment #6
mark_fullmerWe've had a the same need for our custom compound fields that need to leverage the media library. The attached patch is a working example of a Medial Library form element.
This implementation limits the selection to only a single element, which is appropriate for our use cases, but for inclusion in Drupal core, this would need to support a parameter for allowed number of media items.
So, still needs:
- Support a parameter for allowed number of media items
- Tests
Comment #7
aspilicious commentedWe also need this, thnx for the starting patch.
Comment #8
phenaproximaI don't think this should be done as an arbitrary parameter. The media library always depends on a value object, called MediaLibraryState, which contains a lot of useful information relevant to each media library instance, including the number of allowed items and the number of remaining slots. IMHO, the form element should require one of these objects in order to work. Something like:
Comment #9
aspilicious commentedThnx phenaproxima for reviewing. Does this mean this is on the roadmap?
Comment #10
phenaproximaThis is already in the Media Library roadmap, as a "could-have" (i.e., low priority) issue. :)
Comment #11
bgreco commentedWe also need this. I tried out the patch, but it doesn't work for me in its current form, giving me an error when trying to insert the media item. @mark_fullmer, did you create a service to handle access checks/responses for the form fields you created?
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "field:media-media". in Drupal\Component\DependencyInjection\Container->get() (line 153 of core/lib/Drupal/Component/DependencyInjection/Container.php). Drupal\media_library\OpenerResolver->get(Object) (Line: 214)Comment #12
wim leersIt looks like #3085466: Media Library Form Element is a duplicate of this.
Comment #14
mark_fullmerbgreco, yes, it looks the patch in #6 is now out of date with changes to the Media Library widget introduced subsequent to Drupal 8.7.0, starting with e080ab8 (see https://github.com/drupal/drupal/commits/8.8.x/core/modules/media_librar...).
I'm working on updating the form element now, but will first review #3085466, in case that approach is more up-to-date & robust.
Comment #16
wim leersFYI: @nightlife2008 turned this in a contrib module: https://www.drupal.org/project/media_library_form_element — that's an attempt to isolate the required changes in a contrib module rather than a core patch.
Comment #18
rajab natshahI prefer to have the Form field API in Drupal core + the Media Library Edit modal
For now using Media Library Form API Element and Media Library Edit
Still they have got issues with Drupal core standard.
Comment #19
phenaproximaComment #22
panakour commentedany update on this?