Problem/Motivation
Currently the MediaLibraryWidget uses a static call to \Drupal\media_library\MediaLibraryUiBuilder::dialogOptions which means that altering these dimensions is really hard.
Proposed resolution
By just using the media_library.ui_builder instead of the static call a developer can influence the dimensions by overriding the service.
I know the service says:
* This service is an internal part of the modal media library dialog and
* does not provide any extension points.
But sheesh how un-Drupal can you get and there doesn't seem to be another way to alter these options either.
Remaining tasks
Decide if this is the best way to do this
User interface changes
None
API changes
Constructor for Drupal\media_library\Plugin\CKEditorPlugin\DrupalMediaLibrary updated to accept and eventually require the media_library.ui_builder service as an argument.
Data model changes
None
Release notes snippet
TBD
| Comment | File | Size | Author |
|---|---|---|---|
| #43 | media_library_ui_builder_service-9.5.x-3127867-42.patch | 1.42 KB | mighty_webber |
| #40 | interdiff_38-40.txt | 1.93 KB | akshay_d |
| #40 | media_library_ui_builder_service-9.5.x-3127867-40.patch | 2.12 KB | akshay_d |
Issue fork drupal-3127867
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
lendudeWe are using the service in the line above it, might as well use it....
Comment #3
idebr commentedThe class was added as a service in #3020716: Add vertical tabs style menu to media library, but it was contested whether this code should be overrideable, see #19:
If the class is declared as a service, it should be called as a service, so this change makes sense.
The MediaLibraryUiBuilder is now an unused use statement.
Comment #4
lendude@idebr thanks for the review.
Fixed both. We can't use dependency injection here because the service has no interface :(
Comment #5
idebr commentedPatch looks good now, thanks!
Comment #6
catchTagging for subsystem maintainer review. I think we should probably just take internal off the service if we're going this direction.
Comment #7
seanbI've had more questions about changing the width and height of the modal. We have discussed things like an alter hook before but since the class is also used in a lot of javascript we should not make it easy for people to change this.
As a first step I can see making this method non-static and fetch the options via the service is a good step to help with this. Calling the static method on the class is definitely not developer friendly. I guess if we make the width and height configurable this would even help other people from not needing to override the service as well, but that can be a followup.
For the patch, I like the change. Can we add a comment to dialogOptions() that the class name is used in the javascript to warn people that removing/changing the class breaks the library?
Comment #8
phenaproximaI don't really agree with this; IMHO, we should remove the internal designation when and if we add an interface for the service. That would put it, in my view, firmly into the API realm.
However, the proposed change is pretty harmless and increases the flexibility available to mad scientists, while keeping the media library "just working" out of the box. So, +1 for this change. And I agree with this:
Setting "needs work" for that.
Comment #9
lendudeAdded the comment.
Should this maybe get a follow up to not use a class for that but a data-attribute or at least as js- prefixed class that makes it clearer that this is a non-styling class?
Comment #10
phenaproximaYes, this should be a js- prefixed class if it's required for functionality. Tagging for a follow-up.
Comment #11
lendudeAdded the follow up #3131564: \Drupal\media_library\MediaLibraryUiBuilder::dialogOptions dialogClass should be js- prefixed since the media modal JavaScript depends on this
Comment #12
phenaproximaThank you! This is ready, IMHO.
Comment #14
xjmThis seems like a sound change.
Is there a particular reason we're using
\Drupal::service()here rather than injecting the dependency?Also, it sounds like we need a followup to discuss #7 (in addition to the js-prefixing stuff).
Thanks!
Comment #15
phenaproximaEDIT: Never mind, misinterpreted the diff @xjm posted.
Comment #16
phenaproximaFiled #3167992: Make the media library modal's dimensions configurable to address making the modal dimensions configurable.
Comment #17
phenaproximaOK, this should address @xjm's feedback (injected the dependency into the non-static places that it's used). No interdiff because for some reason it's misbehaving (showing the same diff twice).
Comment #19
vsujeetkumar commentedFixing test.
Comment #20
phenaproximaThanks, @vsujeetkumar, for cleaning that up! We do need to keep the default value of the $media_library_ui_builder parameter as NULL for now, so that we don't break classes which might be extending that one. :) Otherwise, looks good.
Comment #21
vsujeetkumar commented@phenaproxima As per your comment #20, I have set the NULL as default value of $media_library_ui_builder.
Comment #25
phenaproximaReplacing patches with a merge request.
Comment #26
phenaproxima@xjm's feedback seems to have been addressed, and follow-ups have been filed. IMHO this is ready, but since I've worked on it I don't think I can RTBC. :(
Comment #27
seanbThe dialog options are pretty well tested since the JavaScript relies on the
.media-library-widget-modalclass. We also assert the dialog titleAdd or select media, which is set by the options, A LOT! For that reason I'm convinced this little helpful change is good to go. Feedback seems to be all addressed.Comment #28
larowlanLeft a minor comment regarding the change notice link on the MR
I realise this is all to facilitate easier overriding of the defaults - but don't we have other ways to alter the output - e.g alter hooks, widget plugins etc etc - I.e. I'm not totally convinced on this change. I don't feel strongly about it though
Comment #29
phenaproximaI'm not aware of any alter hook which can change these options. They are set in an internal AJAX callback and stored in an AJAX command, which has an API to modify the options after the fact, but it's not an API that can be accessed at a useful time (i.e., when the AJAX response is being built). Overriding the defaults can be done without this change, as you can see in this merge request against Media Library Extras: https://www.drupal.org/project/media_library_extras/issues/3228196
But that way is hacky. It requires the module to root around in the guts of an AJAX command, which is probably 95% reliable, but still not an API. Making the UI builder into a service will at least help mitigate this, but ideally we'll open a follow-up that sets the dialog options in the widget form (maybe attached to the "open media library" button as a property), which would allow the existing
hook_field_widget_single_element_form_alter()to influence them. I'm open to do doing that in this issue, though, if you think that's a preferable approach.Nonetheless, I don't think we should be getting the dialog options statically. The UI builder class is explicitly marked as internal anyway, so IMHO we should minimize the need for other classes to call it directly.
Comment #32
danflanagan8Here's a patch version of the MR. Not running tests; I just want to use this on a project.
I'm also happy to help get this over the finish line. Seems like the deprecation message is the only thing holding this up. The deprecation message is out of date at this point, but I'm not sure what versions the deprecation would happen in now. The MR target also needs to be updated and I'm don't think I have the proper permissions for doing that.
I've updated the IS just a bit so that none of the sections are blank.
Comment #33
idebr commented#28.1 Updated the target Drupal version and link to point to the change record
#28.2 This feedback is addressed in #29
Comment #34
xjmDiscussed with @danflanagan8 and @catch. We agreed that since this issue is doing some refactoring, it should be deferred to 10.1.x, and the deprecations adjusted accordingly. Thanks!
Comment #35
akshay_dHi Guys
I have rerolled the patch #32 since it does not apply for the latest drupal 9.5.x
Since the ckeditor plugin updates moved from
"core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php" to "core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php"
Please review
Thanks
Comment #36
phenaproximaAs per #34, changing this issue to target 10.1.x.
Comment #37
danflanagan8Hey, @akshay_d!
The re-roll looks fine for the ckeditor plugin, but can you add the changes to the FieldWidget plugin as well?
Could you also label the patch with
9.5.xin the file name? Sinceckeditoris removed in D10, we'll want to communicate that the patch is specifically for 9.5.x.Thanks!
Comment #38
akshay_dThanks @danflanagan8 for reviewing the patch
I have updated the patch with missing FieldWidget updates
Please review
Comment #39
danflanagan8That looks like a good re-roll, @akshay_d. Thanks!
Just for clarity in the community, I wanted to reiterate that the patches in #32 and #38 are so this work-in-progress can be used on a project by including the patch through composer.
The real work should still happen on the MR. I'll set this back to NW because the MR needs to be updated to account for the removal of the DrupalMediaLibrary ckeditor(4) plugin. I'm not sure if the analogous ckeditor5 plugin would benefit from any related changes, but maybe?
Comment #40
akshay_dHi
I am updating the patch #38 for using ckeditor5 on drupal 9.5.x.
As part of this i have removed the ckeditor4 updates from #38. But the status will be still needs work from the comment #39.
Thanks
Comment #42
mighty_webberPatch for 10.4.9
Comment #43
mighty_webberFix Patch for 10.4.9