Problem/Motivation
A generated name for media entities is not alway a good idea, especially for document media types, where a descriptive and searchable title would be better.
Between the media entity form and the media modal upload widget , there is an inconsistency between how the media name basefield is applied, when the field is not hidden from the form.
In #2882473: Hide the media name basefield from the entity form by default it was stated that "Probably 80% of the use cases will want this populated automatically".
For images, I agree this is probably true. But in the case of documents, some installations will want to provide a descriptive and searchable media name. Both for use on the frontend and also in the media library.
The inconsistency arrises because the media modal upload widget pre-populates the name field with a generated value and this can result in media being named inappropriately.
Steps to reproduce
Upload some media.
1. Directly via the path /media/add/document
2. Via a media enabled content type using the media library modal.
1. In the case of the default form mode from the media module.
- On creating a new document, an empty (and required) input is provided.
- The content creator has to provide a name before the form can be saved.
2. The media modal upload widget behaves differently.
- On creating a new document, the name field is pre-populated with a generated name, as would happen if the field was hidden.
- The content creator does not have to provide an alternative before saving the form.
---
In the original feature request it was suggested that "if the name basefield is visible on the entity form, it should be entered by a human and should not be pre-populated.
However, during the Drupal Usability Meeting - 2020-12-04 it was concluded that such an approach would be unsuitable and may cause problems for existing sites. See comment 15.
Proposed resolution
Only set a default name for the media item if mapped in the media source.
Remaining tasks
1. Agree that a worthwhile UX improvement could be implemented.
2. Develop a patch.
User interface changes
When the Media type does not provide a mapping for the media's name, the Name field has to be filled out manually.
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #30 | 3185321-30.patch | 5.98 KB | lendude |
| #30 | 3185321-30-TEST_ONLY.patch | 2.82 KB | lendude |
| #27 | 3185321-27.patch | 2.96 KB | idebr |
Issue fork drupal-3185321
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
mrpauldriver commentedComment #3
mrpauldriver commentedComment #4
phenaproximaThis definitely seems like a usability concern; tagging appropriately.
Comment #5
phenaproximaA possible workaround here might be to implement hook_form_alter() so that it makes the Name field required, and empty by default, for non-images.
Comment #6
mrpauldriver commentedComment #7
mrpauldriver commentedComment #8
mrpauldriver commentedComment #9
antoniya commented@MrPaulDriver that's a good point about documents having a required name field, which is not hidden in Form Display. It would be great if this was the default.
However, this is tagged as a 'Bug report' and I wasn't able to reproduce the bug. Moving the name field to the enabled fields in Media library form display worked exactly as I would have expected it to work: after uploading a new file named Test.pdf via the Media library widget, I was presented with a form containing a thumbnail preview, a language select and a required name field, pre-populated with 'Test.pdf'. I was very well able to adjust the name to my liking and save the form.
Comment #10
mrpauldriver commented@Antoniya The reason I am describing this as a bug is because these two core modules; media and media library ought to behave in the same way when uploading a file. In this respect, the media modal upload widget does not work as expected.
Yes you are able to change the name manually, but does mean to content creators will and I my experience, they usually will not. This defeats the point of having a searchable media library.
If the modal upload widget worked in a consistent way to the default media upload, there would be nothing to change and a content creator would be forced (quite rightly) to provide a suitable name.
It is clear that when testing, you did so via the Media library form. Did you test the default media upload path at /media/add/document ?
Comment #11
phenaproximaI respectfully completely disagree with this statement. :) These two modules have very different purposes. Media is primarily utilitarian; it's a way to create and manage media entities, but depending on your needs as a user, not necessarily the "best" or "easiest" way. Media Library, on the other hand, is meant to be the UI that lets you interact with media entities quickly and easily, as a non-technical user.
So to me, it doesn't follow that they should necessarily behave the same way. IMHO, Media Library needs to optimize for the best possible user experience, whereas Media needs to optimize for power and flexibility.
That's why I consider this a usability issue, but not a bug (although I'm leaving this issue in that category). It sounds like the media library is working as designed; it's just that it might not be doing things as well as it could or should be for files that aren't images.
Comment #12
mrpauldriver commentedI stand corrected about bug status :)
To distill this down. I don't think it should be necessary to make any determination about what media type is in play. Just that if the name field is visible on the form, it should be empty and required. If hidden, the name should be generated.
Comment #13
mrpauldriver commentedIssue needs a more suitable title now it's a feature request.
The basis of the feature request is that if the media name field is displayed on a form, the site owner does not want a generated name.
Comment #14
antoniya commented@MrPaulDriver yes, I certainly did.
This makes total sense now as a feature request. Indeed content editors can be just as lazy as us devs (hehe), so I know what you mean when you say 'well, yes they could, but they probably won't change the auto generated name'.
Comment #15
benjifisher@Antoniya: For developers, laziness is a feature, not a bug. ;)
We discussed this issue at the #3183735: Drupal Usability Meeting 2020-12-04. We agreed that the proposed resolution (do not provide a default value for the Name field when it is shown on the form) is not a good idea for a few reasons:
We have no objection to adding an option to provide a default value or not. There is a small cost (yet another option on yet another form), and we should consider whether enough sites would use this option to be worth that cost, but that is outside the scope of the usability review. We should also consider alternatives and work-arounds: adding another field or (see #5) implementing a hook in a contrib or custom module.
If you want to move forward with adding an option, the next step is to rewrite the issue summary:
Comment #16
phenaproximaFor what it's worth, I have no objection to adding something like this to the Media Library Extras module, which is intended to handle exactly these sorts of useful-but-not-quite-useful-enough-for-core scenarios.
Comment #17
antoniya commentedtrudat 🙈
So if the option 'Autogenerate media name based on file name' is added either in core or in the Media Library Extras module, it would be enabled by default for all media bundles, but site builders will be able to disable it for a specific bundle if they wish (assuming the option exists on bundle level)? @MrPaulDriver, I believe this should cover the use case you described?
Comment #18
mrpauldriver commented@Antoniya Yes, this is the suggestion that has been made, and yes this would cover my use case.
As recommend in #15, I plan to update the main issue to reflect this.
Comment #19
mrpauldriver commentedUpdated the issue description to reflect changes to the proposed resolution.
Comment #20
mrpauldriver commentedRevised issue description.
Also adding a link a corresponding feature request for Media Library Extras module.
#3190327: Provide an option to not provide a default value for the media name basefield
Comment #21
jonraedeke commentedI might be mistaken, but don't we already have a setting for this per media bundle? At /admin/structure/media/manage/image, I can un-map the name field. That means, I do not want the name field to be populated with the file name. Drupal should check for that before prefilling. I believe the name field is mapped by default, so this wouldn't change the default behavior.
I want my media to have SEO friendly titles. Prefilling the file name discourages that.
Comment #23
chrisck@jonraedeke you're right in that the name field can be unmapped when using media/add but the media library widget modal in ckeditor has the Name field default set to prepopulate with the file name.
Comment #25
idebr commented#21 Applying the media type field mapping to the Media library default value is a good idea:
Attached patch only set a default name for the media item if mapped in the media type source configuration.
No tests yet, let's see what breaks first.
Comment #27
idebr commentedThank you existing test coverage.
Attached patch fixes existing tests and implements correct logic for finding the name attribute in the media source field mapping.
Automated tests will keep failing until #3255749: Composer v2.2 prompts to authorize plugins is resolved.
Comment #29
spokjeComment #30
lendudeYeah using this setting makes sense to me, it's also what I expected it to do.
Here is a test for this, test-only isn't really needed for features but since it was the same as the interdiff might as well add it like a test-only.
Not sure about "The behaviour is unchanged for new and existing sites.", I think we would need an update to set the mapping for all name fields, since that way, if somebody had cleared this setting, it would still behave the same way after updating (ie the name gets pre-filled)
Comment #33
chrisck#30 was applied to 9.4.7 and it is working as expected. When the field mapping is skipped, the default name does not prepopulate with the filename. When the field mapping is set to Name, the default name is prepopulated with the filename.
Comment #34
anchal_gupta commentedRerolled patch Against 9.5.x
Comment #36
mansi agarwal commentedAdded patch against #34 in drupal 10.1.x
Comment #37
gaurav-mathur commentedThanks @mansi your patch ,your patch #36 is successfully applied on drupal 9.5.x and 10.1.x and its working fine.
Comment #38
phenaproximaAdjusting credit because the patches in #34 and #36 are identical. (You should see an "Add test/retest" link underneath patch #34, which could have been used to run it against Drupal 10.)
Comment #39
phenaproximaAdjusting credit again because the same thing is true of #34, which is identical to #30.
Comment #40
phenaproximaI think this change makes sense, but the test is a bit messy (there appears to be some copypasta) and could be streamlined. But generally I don't think this is far off.
This could be more descriptive. How about something like "Tests that the name field is automatically mapped as expected"?
This should have the
voidreturn type.I'm not seeing where $driver is used in this method.
It doesn't look like $png_image is used elsewhere in this method, so I think this could be streamlined. We just need to have an image of any type.
This comment seems divorced from what's actually going on in the test.
Is this necessary?
Can't we just use
$assert_session->fieldValueEquals()here?IMHO we could just do this by loading the media type and modifying it directly, rather than logging in as an admin and doing it in the UI. That would remove some complexity from this test.
We probably don't want to call
$page->fieldField('Name')->getValue()here, because if the field isn't found, the chained call togetValue()will cause a fatal error. Couldn't we use$assert_session->fieldValueEquals('Name', '')instead?