Problem/Motivation
When uploading images into the media library, you would normally see a lot of extraneous junk provided by the Image module's field widget -- stuff like file size, an extra remove button, a second thumbnail preview, and so forth. This is all useless, confusing clutter in the media library.
Until Drupal 8.8, Media Library hid all of this stuff using CSS. However, as of marking the module stable, that CSS had been moved into Seven, which means that any theme that is not Seven, including any theme based on Classy (read: Umami, Claro, Bartik, etc.) will show this cruft, which is a major blow to the media library's usability in non-Seven themes.
To emphasize the severity of this problem from a product standpoint: when they saw it, the only reason the product managers did not make this a Media Library stable blocker is because the timeline to release it was already so rushed.
Proposed resolution
The extraneous bits should be suppressed on the server side while building the entity form for adding an image to the media library, rather than hidden with CSS.
Remaining tasks
The patch needs review/sign-off by a front-end framework manager regarding proposed changes to core themes (see #37 and onward) in order to implement the proposed resolution. Then it can be committed by anyone.
User interface changes
None, really; the media library will behave more consistently in all themes, rather than just looking right in Seven.
API changes
TBD from front-end framework manager review; likely none.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #40 | 2987921-40.patch | 5.87 KB | oknate |
| #40 | 2987921-interdiff--38-40.txt | 586 bytes | oknate |
| #21 | description-for-files-after.png | 151.51 KB | seanb |
| #21 | description-for-files-before.png | 126.9 KB | seanb |
| #20 | 3082690-81--3090505-2--2987921-20.patch | 125.98 KB | oknate |
Comments
Comment #2
phenaproximaComment #3
phenaproximaBlocker is in!
Comment #4
quironI started working on it.
Actually is working, but still requires to restrict of the alter to media library. I would like to know if looks like the correct approach before working further on it.
thanks!
Comment #5
quironAdding a new version of the patch with the missing part of the previous one, so forget #4.
Is altering the image widget render to remove the data that was previously hidden by CSS, and removing the widget title in the form building. Also cleaned up the CSS.
Comment #8
wim leersComment #10
phenaproximaOne nice option here is not to provide a widget, but instead to implement
hook_field_widget_WIDGET_TYPE_form_alter()in Media Library to suppress these components.Comment #11
xjmPromoting to major as per #2834729: [META] Roadmap to stabilize Media Library.
Comment #12
phenaproxima@seanB and I discussed how to implement this in Slack and we agreed on an approach. The implementation, however, depends on an API change that is made by #3082690: Mark Media Library as a stable core module, so this is blocked on that.
The idea: Media Library will implement
hook_field_widget_WIDGET_TYPE_form_alter()for the image_image widget. If the base form ID ismedia_library_add_form, it will add a#processcallback to the image widget, which will suppress the components we want to suppress. We can easily test for this inMediaLibraryTest::testWidgetUpload()and::testWidgetUploadAdvancedUi().Comment #13
phenaproximaHere's a patch to get us going in the right direction after the blocker lands.
Comment #14
phenaproximaTagging this to be worked on at DrupalCon Amsterdam, assuming the blocker lands.
Comment #15
oknateHere's a patch.
It needs:
1) test coverage
2) follow-up for file-size bug. The file-size appears even if you remove filename element.
Update: here's the follow-up: #3090505: File size appears even if filename is removed in a process function
Comment #16
oknatePostponing on #3090505: File size appears even if filename is removed in a process function as well.
Comment #17
oknateThis includes a fix for #3090505: File size appears even if filename is removed in a process function in the combined patch.
Comment #18
oknateComment #19
oknateThis patch adds test coverage to MediaLibraryTest. Also, the test caught a bug I would have introduced with the previous patch.
This process function needs to only affect the source field. If there's an extra image field on the media, it shouldn't affect it!
Added this:
There could be a more efficient way to get the source field name by using $form_state->getFormObject, but it would require adding a getter for AddFormBase::mediaType. Thoughts?
We could add ::getMediaType() and then use that to find the source field name. This would be less brittle in if the form structure changes, such as someone adding a wrapper around the image_image widget.
Hmm, getMediaType is already used.
Comment #20
oknateAs I was looking at how to get the source field info, I stumbled across a @todo that references this issue, and I was able to figure out a more efficient way to test that we are only affecting the source field. To do this, I found the code that was adding the source field info to the form, and in the same place, I add the process function if applicable. When adding the process function in AddFormBase, it makes sense to move the process function there as well.
Comment #21
seanbI think this is no longer true and needs to be removed as well.
This styling also hides some fields for the document media type. We need to apply the same changes to the file_generic widget. See attached screenshots.
Before:

After:
I guess this fixes a bug in stable, since outputting the file size when the name is somehow empty doesn't make a lot of sense, but I think we need to confirm that.
Comment #22
oknateRe #21.3, see #3090505: File size appears even if filename is removed in a process function, I created a separate bug ticket for it, but I'm not sure if it needs separate test coverage because it's so self-documenting, and there's no other known use case. You'd have to create a test module that alters the output to test the fix. I'm not sure if it's worth it when this issue will provide ample test coverage.
Comment #23
oknateAddressing #21
1. ✅Removed that comment.
2. ✅Expanded the scope to include any element using FileUploadForm. In fact, I moved everything into that class.
3. I created a separate bug ticket for that originally, but I'm not so sure it needs separate test coverage.
Comment #24
seanbThis seems to work for documents as well now, so thanks!
It seems that this can also be removed. I don't see #source_field_name being used anymore?
Very clear! Love it.
Do we have a test for documents as well since we originally missed that? I think that would be good to make sure we don't break that in the future.
That being said, maybe we don't have to check this after every upload and just have a separate test method to verify this for images and documents only once.
Comment #25
oknateAddressing #23
1. I think it's useful for contrib. I suggest we leave it.
3. That's fine. I'll add a dedicated test. MediaLibraryTest is bloated as is.
Comment #26
phenaproximaThis looks great, and clean! +1 for the approach here.
This needs to be wrapped in an isset(), just in case the entity form display is in an unexpected state. Something like this:
We should rename this to something more descriptive, like hideExtraSourceFieldComponents() or something.
We should not be unsetting these, as it means extending code has less flexibility. Instead, let's set #access to FALSE. Because we're doing that, we don't need the !empty() checks. Just doing this should suffice:
Are there other values besides "none" that would be more appropriate here for accessibility (i.e., should we just make these visually hidden, rather than gone entirely)?
This also does not need the !empty() checks, and should use #access => FALSE instead of unset.
We can just merge this into assertMediaAdded(). There is no case where we would want these to show up when adding media; we can always assert that they're absent any time we add new media.
Additionally, we probably shouldn't be asserting the absence of CSS classes, since they are representational and added by Seven. Instead, we should assert the absence of particular fields/buttons by label or name, or elements identified by their
data-drupal-selectorattribute.Comment #27
rafuel92 commentedi'm working on it at drupalcon Amsterdam2019
Comment #28
oknateChanging to PP-1, due to what I've found in #3090505-10: File size appears even if filename is removed in a process function. If you use
['filename']['#access'] = FALSE;instead of removing the filename, you don't encounter the bug in that issue.Comment #29
rafuel92 commentedok, attached you can find a patch (with related interdiff) that:
- fixes points 1,2,3 and 5
- for points 4 and 6 as discussed with @seanB we have that the classy template doesn't respect the access and still renders an empty div, so we added a css rule to make sure that the image preview is completely removed and in functionalTests we check at least that the image preview is empty.
Comment #30
rafuel92 commentedComment #31
rafuel92 commentedmoving to needs review for automated tests.
Comment #33
rafuel92 commentedjust changed the "js-media-library-add-form-added-media" class with "media-library-add-form__fields" css class to target the right remove button
Comment #34
rafuel92 commentedComment #35
phenaproximaThis isn't going to fly, unfortunately :(
In tests, we cannot use CSS classes which are not prefixed with
js-, because they are normally added by themes (and this will be the case once Media Library is stable) -- and therefore, we cannot rely on them. We can use data attributes, orjs-prefixed classes, though. Thedata-drupal-selectorattribute should be sufficient.Comment #36
phenaproximaMedia Library is stable, so this is no longer needing the PP-1 designation.
Comment #37
oknate<div class="image-preview">element being added in classy:Comment #38
phenaproximaI want to try a few things here:
if data.preview|render is not emptything in favor of a new preprocess function in Classy. IMHO the current syntax is more straightforward from a theming standpoint; the preview is either there or it's not. Using therenderfilter complicates it and exposes the underlying weirdness, which is something we would ideally avoid if we can.filename's#accessis FALSE, the #suffix won't be printed either. (Let's see what the tests say about this.) Ideally we should avoid changing Stable in any way, since its purpose is to guarantee backwards compatibility.Comment #39
phenaproximaIt looks like any approach we take here is going to require some front-end framework changes, so tagging for front-end framework manager review/sign-off, especially since we're so close to the beta period.
Also removing the "needs tests" tag, since we have coverage for the changes we're making here.
Comment #40
oknateRegarding #38.3, "Removed the change to Stable", it looks like patch #38 accidentally removed the .file-size span, so fixing that. I was meaning to undo the condition as you did (see #28), but forgot about it in #37, so 👍for this change.
Comment #41
phenaproximaUpdating the IS to explain the current problem and its proposed solution.
Comment #42
phenaproximaComment #43
phenaproximaComment #44
seanbSome minor nits, but I think this is really close. Looking good!
Still not sure if we should keep this...
Minor nit, but we only process images/files. The method names suggests we might also change other types of source fields. Maybe
hideExtraFileSourceFieldComponentsorhideExtraUploadSourceFieldComponentsor something?Apparently we use data-drupal selectors in tests all the time, so I guess this is fine!
Nice one, I like it!
Comment #45
phenaproximaThis is currently used by Seven, and removing it would therefore, technically, constitute a BC-breaking API change in a stable module. Granted, I'm not sure render arrays constitute "APIs"...it's a grey area. Either way, I think this will be helpful to themes and form_alters, so IMHO we should keep it. It harms nothing by its presence.
I think we can leave it as-is. The method exists on a form called FileUploadForm, which implies that it deals with, well, file uploads. Seems reasonably self-explanatory to me.
Comment #46
seanb#45 Fair enough. If seven already uses this then it makes sense to keep it. And you are right that it lives in a
FileUploadFormclass, so I can live with that as well. Have nothing else to complain about really. Just needs frontend framework manager signoff I guess?Consider this RTBC from me.
Comment #47
lauriiiDiscussed with @alexpott and @xjm and we agreed to backport this to 8.8.x during the beta phase because it would be great to get this done as soon as possible to avoid any risk of disruption.
Comment #48
phenaproximaRTBC as per @seanB's feedback in #46.
Comment #52
webchickAwesome, yay for doing this UX fix-up for all themes.
Committed and pushed to 9.0.x; 8.9.x; 8.8.x. Thanks!