Problem/Motivation
The media Authored by filter should use the user_name filter plugin OOTB in order to be consistent with Content, and provide a better UX with an autocomplete field.
Proposed resolution
Updated the Media Views Data to suggest the user_name Views Filter Plugin as the default for newly created fields.
How to test
- Enable the media module.
- Create a view for media content.
- Add a media author filter to a view and make it an exposed filter.
- Notice it's a simple text field and takes the uid as a value.
- Remove the filter and apply the patch, clearing caches just to be sure.
- Re-add the "Authored by" filter, notice it's now an autocomplete field taking usernames as the values.
Remaining tasks
None
User interface changes
Adding 'Authored by' to a Media View (like the default one at /admin/content/media) results in a username autocomplete instead of a plain text filter by ID.
Before:

After:

API changes
- N/A
Data model changes
- N/A
Release notes snippet
Newly created 'Authored by' filters added to Views that list Media will autocomplete on username instead of expecting the user to enter a user ID.
| Comment | File | Size | Author |
|---|---|---|---|
| #115 | after.png | 13.16 KB | scott_euser |
| #114 | before.png | 10.71 KB | scott_euser |
Issue fork drupal-3113989
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:
- 3113989-media-author-views
changes, plain diff MR !7464
Comments
Comment #2
acbramley commentedStill need to update tests, but I'm curious what this breaks.
Comment #4
kristen polThanks for the patch.
1) Patch applies cleanly to 8.9, 9.0, and 9.1.
2) Searched the codebase for something similar to:
$data['media_field_data']['x']['filter']but didn't see anything.
3) Looked at
NodeViewsDatabased on issue summary note and see:which is the same pattern as the patch so this looks ok.
4) I'm not sure this would be considered a "Bug report" but I'll leave that as is.
5) What's the best way to manually test this?
Comment #5
kristen polAlso, this might need tests.
Comment #6
acbramley commented@Kristen Pol thanks for the review, it does need tests, I've added the label back in #2 :)
Comment #7
kristen polDoh! Sorry for the noise. :) Working on too little sleep and too many issues.
Comment #8
acbramley commentedNo worries :) This also needs an upgrade path as pointed out by larowlan in #3135342: Use the user_name filter for the linky author for a better UX
Comment #9
kristen pol@acbramley Do you have any thoughts on best way to manual test this?
Comment #10
acbramley commented@Kristen Pol Sure:
1. add the media author filter to a view and make it an exposed filter
2. notice it's a simple text field and takes the uid as a value.
3. Remove the filter and apply the patch, clearing caches just to be sure.
4. Add the filter again, notice it's now an autocomplete field taking usernames as the vaules
Comment #11
kristen polAwesome, I added this to the issue summary. I'll try to test soon.
Comment #12
kristen polTested manually and it works great!
Without patch:
With patch:
Comment #13
kristen polMoving back to "Needs work" for the tests.
Comment #14
acbramley commentedThanks so much!
Comment #15
kristen polLooked around for tests for a similar filter and had a hard time. I found
testExposedFilterin:core/modules/user/tests/src/Functional/Views/HandlerFilterUserNameTest.phpbut that's for the user entity and name property. Also found
FilterUidRevisionTestin:core/modules/node/tests/src/Kernel/Views/FilterUidRevisionTest.phpbut that's for node author / node revision author check.
Comment #16
kristen polTrying to get some guidance from the Bug Smash Initiative on where the test(s) should go.
Comment #17
dwwHow about adding another test method in core/modules/media/tests/src/Functional/MediaOverviewPageTest.php to add the media author as an exposed filter to the default media overview page? Then assert it's an autocomplete text field, not a simple integer UID field? I don't think we need to / want to actually use a JS test to verify the autocomplete works as autocomplete. That's already tested elsewhere. We just need to make sure that this filter is using the right plugin.
Comment #18
kristen pol@dww Nice. That seems like a great approach, thanks!
Comment #19
shailja179 commentedComment #20
acbramley commentedThis should work but for some reason is failing on the last assert for me locally. Checking what CI thinks.
Interdiff === test-only patch.
Comment #21
acbramley commentedOf course I figure it out just after I post the patch 🤦♂️
Comment #22
acbramley commentedAnd the missing doc block...obviously not enough coffee.
Comment #23
kristen polThanks for the patch. Not sure I follow it completely :) but I noticed a couple things.
Missing doc block.
Nitpick: Although I see a small number of tests use the
=& $viewformatting like:most of the core code uses
= &$viewlike:I'm unclear why
$this->drupalGet('/admin/content/media');is done twice here.Comment #24
kristen polAh, we crossposted. Ignore #23.1.
Comment #25
acbramley commentedFixed #23.2 and .3
Comment #26
kristen polThanks for the updates. I reviewed again and feel like I understand the test now. Not sure why the timestamp is adjusted for
$media2and$media3but it doesn't hurt anything. I see that the text being checked is the correct string so, if the tests pass, this is looking good to me.Comment #27
acbramley commentedYeah you're right this can be removed (maybe on commit?), it was a copy-paste from the other test function in the class.
Comment #29
kristen polWoot! Looks like the test is passing fine and the test-only patch fails as expected. If you want to adjust it to remove those couple timestamps, that's fine. I'll wait a bit before moving this forward. Thanks.
Comment #30
acbramley commentedThanks @Kristen Pol! I've removed the changed dates now.
Comment #31
kristen polNice! Ok, crossing fingers there is no glitch in the testbot matrix.
Comment #32
dww#30 looks great.
Actual fix is tiny and correct.
Test coverage looks reasonable.
No nits found.
No CS violations.
Therefore... RTBC!
Thanks,
-Derek
Comment #33
alexpottThis needs an update path for existing view. If I apply this patch and have a view created as suggested in the issue summary I get an error -
The operator is invalid on filter: Media: Authored by.Comment #35
mohit_aghera commentedComment #36
mohit_aghera commented- Added a post_update hook to change the filter and related attributes.
- Add test case to evaluate the post_update hook logic.
Comment #37
mohit_aghera commentedComment #38
mohit_aghera commented- Added a post_update hook to change the filter and related attributes.
- Add test case to evaluate the post_update hook logic. Update testcase is passing on local
Ignore the patch from comment #36
Accidentally, I created diff from older 9.2.x head.
I've hidden patch and interdiff.
Comment #39
lendudeNice, like @Kristen Pol not convinced it's a bug (can you not just workaround it by adding a relationship to the author?), but don't care too much.
Some things I see:
Since we are hardcoding the plugin ID in the test when we add the filter, this would pass even without the change in MediaViewsData, so that makes the test a little suspect I think. If we really want to test that setting, we would need to add the filter through the UI.
Ideally the update would use \Drupal\views\ViewsConfigUpdater so we also update any config provided by contrib and the like
Comment #40
mohit_aghera commentedComment #41
mohit_aghera commentedUpdating test cases and post_update hook as mentioned in #39
Comment #42
mohit_aghera commentedComment #43
lendudeThanks @mohit_aghera, looks great already.
One last thing I see:
We should also add a call to this in \Drupal\views\ViewsConfigUpdater::updateAll so that Views getting saved are also updated, see the other calls in updateAll
Comment #44
mohit_aghera commentedThanks, @Lendude for correcting me.
I've updated test cases accordingly.
Comment #45
kristen polThanks for the update.
1) I confirmed the interdiff addresses #43.
2) Since @Lendude already approved other than that, I only scanned the code quickly. I found one minor nitpick:
Extra empty line.
Comment #46
mohit_aghera commentedComment #47
mohit_aghera commentedRemoved the additional empty space issue mentioned in #46
Comment #48
mohit_aghera commentedComment #50
benjifisherPlease do not ask the testbot to try again until #3207086: [HEAD BROKEN] Consistent failure in MonthDatePluginTest is fixed.
Comment #51
alexpott#3207086: [HEAD BROKEN] Consistent failure in MonthDatePluginTest is fixed. I have not reviewed the code.
Comment #53
acbramley commentedBack to RTBC, looks like another random fail.
Comment #55
kristen polWhile it should have been moved back to needs review in #51 rather than RTBC, I've confirmed that the interdiff in #45 does fix the nitpick in #45 so RTBC is good :)
Comment #57
catchRestoring status after HEAD was broken.
Comment #58
larowlanLooking great, couple of questions
if we already have a test view with the media author exposed, shouldn't we just use that in test instead of going to the trouble of editing the default media view?
We already have a d9 filled standard in core/modules/system/tests/fixtures/update/drupal-9.0.0.filled.standard.php.gz
Any reason to add a new one? Can we just use the existing one?
is the 18 in the function name significant here, this is the first instance of it I can see for the views post update file.
Comment #59
acbramley commented@larowlan re 1: This is explained in #39
Have fixed 2 and 3
Comment #61
acbramley commentedAh, that fixture had the
test_media_view_exposed_filterview in it...Comment #62
larowlanCan we do a fixture that adds to the existing dump, like e.g. core/modules/system/tests/fixtures/update/drupal-8.update-test-semver-update-n-enabled.php
Comment #63
acbramley commentedSure, so far have downloaded the old fixture, loaded into a db manually, and extracted the config XD good lesson in how this stuff works!
Comment #64
acbramley commentedThis should work but something's a bit screwy with my local environment. Maybe someone else can have more luck.
Comment #65
acbramley commentedComment #66
daffie commentedAll points of @larowlan have been addressed.
I have just one question:
What happens when somebody has a custom project that uses the view with the min/max placeholder values?
Comment #67
rahul b commentedThe patch works fine for me. after applying the same it is generating prediction auto-completion successfully.
Before Patch

After Patch
Comment #68
rahul b commentedComment #70
acbramley commentedRandom fail.
Comment #71
kim.pepperComment #72
larowlanSaving issue credit
Can we get an answer for #66 and a change notice here.
With both of those, I think this is good to go.
Comment #73
larowlanFor #72
Comment #74
acbramley commentedDrafted a Changed record using the screenshots from #67
https://www.drupal.org/node/3220467
Comment #75
lendudeRe: #66, since we are updating the plugin, those values wouldn't do much anymore anyway, so I don't see any harm in removing them with the current fix, but it does raise a very good point because the same goes for projects that have a filter and they are using it with a 'between' operator (or anything not '='), we are destroying that too ('between' will become 'in', so does something completely different after the update). I think it is fairly unlikely that anybody would have that, but we do have to ask the question if we are ok with that.
Found some other things that probably need addressing.
Calling this without the media_field_data check in that method will have the potential to update WAY more then the field we are trying to target (see point 4 below)
we are checking $handler['id'] in the isset() but checking $handler['field'] after that, should they both check $handler['field']?
This comment needs an update
This should probably be checked in needsMediaAuthorFilterPluginId() or probably processMediaAuthorFilterPlugins()
Comment #76
acbramley commentedNice catches @Lendude
Have rerolled + addressed #75
Comment #78
lendudeHmm the check on base table is still circumvented in the updateAll call like this. We probably need to pass the View along to the process method and check it there.
The update still feels potentially destructive though. An alternative would be to do this without an update and provide the user name filter as a separate filter so the original can stay as it is. Too extreme BC? ¯\_(ツ)_/¯
Comment #79
lendudeSomething like this would also work, without the BC implications. We should probably add the test coverage, but this is just an idea.
Comment #80
acbramley commented@Lendude I've tested the upgrade path while using the filter as non-exposed. It's currently using the EntityReference plugin which only has the following operators: Is one of, Is all of, Is none of, Is empty, Is not empty.
Tested using "Is one of" and referencing a user and running the update hook. The operator and value were correctly migrated and the filter continued to work exactly as it had before.
Is all of does not make sense for this field, Is none of doesn't even work in the current state. The other 2 are 1 to 1 mappings.
I think given all of this, it's safe to update the existing filter.
Comment #81
lendudeWhen I add the 'Authored by' filter without the patch, I see all the options in the screenshot. In the update we are mapping all of these to the 'in' operator. Again, I doubt anybody is using those operators, but we can't be sure.
Or are your seeing something else?
Comment #82
acbramley commentedI was definitely seeing something else! I will spin up a vanilla instance and re-test.
EDIT: It's most likely because I was testing a site that had #2429699: Add Views EntityReference filter to be available for all entity reference fields applied :(
Comment #83
quietone commentedi manually tested the patch in #79 on 9.4.x, standard install. Both before and after the patch the uid was used for the filter not the name. I did not experience any difference before and after the patch.
Comment #84
acbramley commented@quietone #79 does not contain an upgrade path so you would have to remove and re-add the filter to see the changes, or test #76 and run database updates :)
Comment #85
quietone commentedOh, I see the problem. I don't think the steps in the IS are correct. The last step is to apply the filter again. To me that means the same filter used in the previous step when instead it is to use the new filter with the title 'Author name'. When I use that filter it works as expected.
Comment #87
kristen polTagging for issue summary update for proposed resolution and possibly testing steps (per #85).
Comment #88
ranjith_kumar_k_u commentedRe-rolled #79 for 9.4
Comment #89
kristen pol@ranjith_kumar_k_u Thanks for the reroll. Tagging for tests.
Compared patch in #88 with previous one in #79 and the changes are the same.
Patch in #88 applies cleanly to versions 9.4 and 10 and with offsets for 9.3.
Comment #90
acbramley commentedTests are present in #76 - not sure why they weren't brought forward.
Comment #91
kristen polMoving back to needs work.
As @acbramley points out in #90, tests were in patch #76. These need to be added back.
Comment #92
yogeshmpawarComment #93
yogeshmpawarAdding tests again from #76 as suggested in #91.
Comment #95
yogeshmpawarUpdated patch with interdiff.
Comment #97
acbramley commented@yogeshmpawar sorry I should've been more clear - we don't need the upgrade path test or fixture because there is no longer an upgrade path, we are simply adding a new filter.
Comment #99
ameymudras commentedRemoving tests related to upgrade path and fixtures. Also updated the testMediaOverviewAuthorFilter inline comment.
Comment #105
maskedjellybeanThank you for the patch in #99. It still works in Drupal core 10.2.3.
I opened a MR (in case that helps move this along) which contains the changes from #99, plus hopefully fixes the tests, although I can already see they've failed...
Comment #106
maskedjellybeanMade the requested changed to the MR. Unfortunately I can't do anything more to make the tests pass because the error appears to have nothing to do with the test being added here.
Comment #107
mrshowermanJust came across this and fell into the same trap as #85.
Updating IS for clarity.
Comment #108
mohit_aghera commented- All three points mentioned by @acbramley is fixed by @maskedjellybean
- Issue summary is updated and PR is rebased with latest 11.x.
- Merge conflict is resolved.
- Hiding older patches in favour of PR based approach.
- I think it is ready for moving to needs review.
Comment #109
mohit_aghera commentedComment #110
smustgrave commentedRan test-only feature
Manually testing editing the view I'm seeing the new filters.
Believe this is good
Comment #111
catchComment #112
quietone commentedThere are no unanswered questions here.
After reading the IS I expected to see a new plugin of some type, but there isn't one. The IS says there are no UI changes but there are so there should be screenshots available from the Issue summary.
I then read the CR and saw that the screenshots are out of date. For that I applied the diff to make new ones. I then saw that the description for the new filter, 'Author name' is that it is the user ID. That is not correct, it is the user name. Should this be doing the same as the Content view and use a title of "Authored by" and a description of "The username of the content author."?
At the lest the description should change. Setting to NW.
Comment #114
scott_euser commentedComment #115
scott_euser commentedUpdated 'After' screenshot to reflect change to default label
Comment #116
scott_euser commentedComment #117
smustgrave commentedAppears to have a relevant test failure
Have not reviewed yet.
Comment #118
scott_euser commentedTest coverage passing now; applies a filter on username (rather than uid) and returns expected media entities as results.
Comment #119
smustgrave commentedRan test-only feature
Resolved the open threads as they appears to be addressed
Applied the MR before doing an install
Followed the steps in the summary and am seeing the new filter "The username of the content author."
Applied it to the Media Library view I can filter by usernames
LGTM
Comment #120
alexpottCommitted and pushed 68013115a83 to 11.x and c3a9311a660 to 11.1.x. Thanks!