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.

CommentFileSizeAuthor
#6 3065677-6.patch13.07 KBmark_fullmer

Issue fork drupal-3065677

Command icon 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

nathandentzau created an issue. See original summary.

nathandentzau’s picture

Issue summary: View changes
phenaproxima’s picture

Version: 9.x-dev » 8.8.x-dev
Issue tags: -media library

+1 for this idea!

nathandentzau’s picture

Assigned: Unassigned » nathandentzau
nathandentzau’s picture

Issue summary: View changes
mark_fullmer’s picture

Status: Active » Needs work
StatusFileSize
new13.07 KB

We'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

aspilicious’s picture

We also need this, thnx for the starting patch.

phenaproxima’s picture

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.

I 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:

use \Drupal\media_library\MediaLibraryState;

// @see \Drupal\media_library\MediaLibraryState::create() for parameter info
$state = MediaLibraryState::create(...);
$element = [
  '#type' => 'media_library',
  '#state' => $state,
  '#default_value' => $selected_media_item_ids,
];
aspilicious’s picture

Thnx phenaproxima for reviewing. Does this mean this is on the roadmap?

phenaproxima’s picture

This is already in the Media Library roadmap, as a "could-have" (i.e., low priority) issue. :)

bgreco’s picture

We 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)

wim leers’s picture

It looks like #3085466: Media Library Form Element is a duplicate of this.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mark_fullmer’s picture

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

bgreco, 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.

wim leers’s picture

FYI: @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.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

rajab natshah’s picture

I 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.

phenaproxima’s picture

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

panakour’s picture

any update on this?

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

m Sami Antaki made their first commit to this issue’s fork.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.