Problem/Motivation
Jumping off of the work being done in #2761833: Widget context and validators not passed to Entity Browser element, we should look at dynamically filtering the View widget using widget_context, or a similar system. Supporting cardinality (i.e. preventing how many items can be selected with JS) is out of scope for this issue.
Proposed resolution
I think a starter patch should make the following available:
- What entity bundles should be shown in the View
- Acceptable mime types for Files/Images
I want to keep the scope small as the issue is complex.
Remaining tasks
None.
User interface changes
- Provides views default argument plugin "Entity Browser context"
API changes
None.
Data model changes
Adds schema for new views default argument plugin.
Comments
Comment #2
samuel.mortensonComment #3
samuel.mortensonHere's a start to this - I created a new Views argument_default plugin which allows users to set default contextual filter values based on the widget context.
You can test this out on any existing Entity Browser view by doing the following:
1. Add a new contextual filter for the entity type bundle.
2. Check "Provide default value", then select "Entity Browser Context'
3. In "Context key" field, enter "target_bundles"
4. Under "Multiple values", select "OR"
5. At the bottom of the form expand the "More" fieldset and and check "Allow multiple values"
6. That's it! The next time you open a Entity Browser using the field widget, your View should contextually filter out bundles that can't be selected
For file MIME types, things are a little more tricky. #672606: Hyphens and forward slashes (-/) break Views contextual filters will need to be closed to support filtering out un-selectable extensions, so that will not be covered by the test coverage.
Still need to write test coverage but don't have time today, keeping it assigned to myself until that's done. We'll also need to add schema for the argument_default plugin.
Comment #4
samuel.mortensonComment #5
samuel.mortensonAdded test coverage!
Comment #8
samuel.mortensonThis should fix tests.
Comment #9
samuel.mortensonJust re-uploading entity-browser-view-context-2865928-8.patch, which should apply cleanly now that #2761833: Widget context and validators not passed to Entity Browser element is closed.
Comment #10
phenaproximaLightning is using this patch. It works for us, and we like it too. I think that's RTBC.
Comment #11
slashrsm commentedThis is assuming a specific entity reference selection display plugin (which probably covers a good portion of the use cases).
It would be very hand to do this generally but at least we should to the empty check or something along those lines to prevent notices from happening.
The fact that the tests didn't detect that make me think that we should improve those too.
Comment #12
samuel.mortenson@slashrsm With widget context I think the goal is to provide as much information as possible, and let Widgets implement handleWidgetContext if what's passed to them doesn't match up 1:1 with their configuration. The Views argument_default plugin introduced by this patch is generic, but it does require that the end-user knows the names of the widget context provided to them. Maybe it would feel better if all the out of the box widget contexts were documented somewhere?
Edit: Also, where are you seeing notices?
Comment #13
samuel.mortenson@slashrsm I'm reviewing your comment again and think I'm missing something - you mention a "specific entity reference selection display", but I don't know what this is referring to. We already assume that the "target_type" handler setting exists, all I'm doing is also using the "target_bundles" setting. If you look at the default widget (\Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget) it also references "target_bundles" directly - I think it's safe to use.
I'm putting this back into review, if there's something else you would like addressed here let me know, I'd like to see this get in.
Comment #14
legolasboAfter reviewing the patch I agree with phenaproxima. The patch does what it says on the tin and the code looks good. Sure there might be some improvements that could be made, but I'd rather have this in and improve on it than spend another couple of months perfecting it (and rerolling it a dozen times in the process)
Comment #15
recrit commentedAttached is a re-roll against the latest dev #0f7df4a since this commit causes a conflict with the patch: http://drupalcode.org/entity_browser/commit/?id=0f7df4a .
Comment #16
recrit commentedre-queuing tests.
Comment #17
Narretz commentedHi samuel.mortensen,
thanks for working on this, I hope it can make it into the module soon.
Edit: as happens so often, I found the answer 15 minutes after sending the issue.
The filtering works, but I assumed that this patch would also restrict the values in the filter dropdown to the allowed media bundles.
The only thing I'm still wondering is why the "Entity Browser Context" default value is not available when I add the filter in the Master display.
------
Regarding the instructions, can you clarify something for me?
1. Add a new contextual filter for the entity type bundle.
I assume this means the "Bundle" entry of the media module in the list of contextual filters, right? -> correct
I've noticed that the "Entity Browser Context" default value is not available when I add the filter in the Master display, is that normal?
And since there are many different displays in the "Media Library" view, I wonder where I need to add the filter to have it show up on the "Gallery media library" entity browser?
Comment #18
samuel.mortensonHi @narretz, thanks for testing out the patch!
This is a hard problem to solve, but I agree that the UX would be improved by also removing items from any select/radio/checkbox lists that filter on the same values as the contextual filter. Here's a snippet of form alter code from a Content Browser issue (#2851687: Does not respect content type limitations) that I wrote to do this:
This could probably be abstracted to work for all Entity Browsers, but I think this is a hard enough problem that we should address it in a follow up. All contextual filters in Views ultimately have this problem, but Entity Browser can likely do some work to make this better.
Yes, and for the Node module you would add the "Content Type" contextual filter.
Yes, I didn't want the Entity Browser contextual filter showing up in every View, as it can only really function if used with an Entity Browser display.
You would need to add the contextual filter for displays that use the Entity Browser display.
Comment #19
martijn de witTested the patch using field widget "entity browser" & "inline entity form".
entity browse
Added an "entity browser" view display on the default content view from Drupal. Than added a contextual filter: Content type with the options described as in comment #3.
Using a field with form display "entity browser". Clicking on button launches the entity browser in my model with the filtered view.
inline entity form
Tested this with inline entity form. Using a field widget "Inline entity form - Complex".
When enabling the entity browser here. The views doesn't seems to get the context from the field. It seems that the widget_context is empty using this setup.
I'm not a developer so I can't help you with an extension to the patch. I sure hope this also can be fixed for the inline entity form widget.
As a side note, totally agree this solution needs some UX improvements. Without the help of comment #3. A normal site builder would not be able to get this working.
Comment #20
martijn de witComment #21
szeidlerI'm using the patch for entity browser and an entity form widget. I'm using it in production since a while and it simply works.
Comment #22
szeidlerI just tested the patch with a combination of entity_embed and entity_browser within CKEditor. Here, the library is shown empty, because it's missing the context.
I see, that it's the result of not being an entity reference field, but I just wanted to notice it here for further reference.
Comment #23
kerasai commentedI'm running into this as well, but using the core media functionality with Entity Browser 8.x-2.0-alpha2.
Any feedback on if this patch will (almost?) apply or if the implementation will be somewhat similar for the 8.x-2.x branch?
Comment #24
martijn de witWe are using the patch from #15 for version 8.x-2.0-aplha2. It works fine for the entity reference field. As early mentioned it doesn't work for the IEF module.
Added a test at patch #15 for 8.x-2.0-aplha2
Comment #25
iampumaUsed drupal/entity_browser (1.5.0) with the settings mentioned in #15 and works perfect.
Comment #26
samuel.mortensonComment #27
prineshazar commented#15 Patch working nicely with entity_browser 1.6.
I tried to apply it to 2.3 and it failed.
So using 1.6 for now, thank you!
Comment #28
oknateTested with 8.x-2.1, works well with field widget with nodes. It took a bit to figure out.
1) I think an update to the README.md with the instructions in #3 would be really helpful.
2) This text seems out of date: "The key within the widget context. If the corresponding value is an array its values will be joined with an AND."
Since it gives you the option to use AND or OR. I think the second sentence should be dropped. Right?
Comment #29
oknateHere's an updated patch
1) Adds widget context target bundles for entity embed dialog. This means you can use the same entity browser for both field widgets and entity embeds. Eventually the code could be moved to the entity_embed module. But we can leave it here for now.
2) Adds sensible defaults in the contextual plugin that follow the instructions in #3. I don't think we need to update the README.md with these defaults and updated description text in place in place. Most importantly, I add a note about checking "Allow multiple values" at the bottom of the form. I got stuck on this when testing this morning, so I think it will be helpful to have this note there.
Comment #30
oknateThis patch expands on #14 and #29.
This patch adds integration with inline entity form so that the default value views plugin works.
Comment #31
oknateComment #32
oknateReroll of #30 (arguments for FileBrowserWidget have changed)
Comment #33
oknateAdding target_entity_type to widget context. Not sure how it might be used, but I'm working on an alter hook in a patch for #2790951: Provide for contextual filter argument option on field widget, and I think having the information there could be useful. The entity browser doesn't store a target type, so it will be convenient to have it stored in the widget context.
Comment #35
oknateComment #36
oknateLast failure was random. Marking back to "Needs Review".
Comment #37
oknateAdding test coverage for inline entity form
Comment #38
oknateComment #40
oknateworking on fixing tests
Comment #41
oknateComment #43
oknateWorking on fixing tests
Comment #44
oknateComment #46
oknateWorking on fixing tests
Comment #48
oknateworking on fixing tests
Comment #50
oknateworking on fixing tests
Comment #51
oknateThere are random failures on line 77 of PluginsTest. I have noticed this on other issues too. Seeing if this fixes it.
Comment #52
oknateFor some reason on the Add test /retest form, even though I check 8.x-1.x, it keeps adding tests for 8.x-2.x, so changing back to 8.x-1.x to see if that helps.
Update: No, it keeps ignoring what I check.
Comment #53
oknateSame as #51, just trying to test 8.x-1.x on 8.6.
So the random failures are annoying. But back to what is left to do:
- We still need a test that shows that this works now with entity embed. I'll work on this ASAP.
Comment #54
oknateAdding test coverage for entity embed! That's the last @todo item.
Comment #56
oknate- Working on fixing tests, I moved some configs from entity_browser_ief_test to entity_browser_test, so that I could share them with entity_browser_test_entity_embed
- I changed EntityBrowserTest::testContextualFilter to match the other two, apologies to @samuel.mortenson for renaming content types. I also added some additional test coverage by switching the bundle and reopening the entity browser.
Comment #57
oknateComment #58
oknatetesting 56 against 8.x-2.x
Comment #60
oknateWorking on fixing tests
Comment #61
oknateComment #63
oknateWorking on fixing tests
Comment #64
oknateComment #65
oknateWorking on fixing tests
Comment #67
oknateWorking on fixing tests, sorry for the noise.
Comment #68
oknateWorking on fixing tests
Comment #69
oknateI figured out why the dependencies are not working, see #3040322: Add entity_embed and embed to test dependencies.
Also, I created an issue for the broken PluginsTest: #3040286: PluginsTest failing on 8.6.11
Update, I have a fix for PluginsTest.
Comment #70
oknateUpdated composer require dev to fix test dependencies on entity_embed and embed. Thanks to Berdir for the info.
Comment #72
oknateI believe this is ready to go now!
It has been repeatedly verified and marked RTBC. And now it has test coverage for field_widget, inline_entity_form and entity_embed.
Comment #73
oknateReroll
Comment #75
oknateComment #76
oknateMarking back as RTBC.
Comment #77
oknateComment #78
oknateComment #81
oknateCommitted! Thanks to Samuel Mortenson, and to all who reviewed.
Comment #82
oknateComment #83
oknateComment #84
martijn de witGreat work @oknate!
One side note, I see the whole git command in your git message(s).
https://cgit.drupalcode.org/entity_browser
Comment #85
oknateOh, how embarrassing. I use tower git. I'll fix that going forward, thanks. I'm not sure I can fix the ones already added.
Comment #86
samuel.mortensonReally cool to see this go in!
Comment #87
oknateYes, I considered waiting until the 31st, which would make it two years in the making! Thanks again for the new feature.
Comment #88
oknateI'm working on adding another views plugin building on this feature that will allows exposed filter options to update based on target_bundles:
#3039038: Provide views filter that limits based based on allowed bundles
It would be great if someone could review it.
Comment #89
vpeltot commentedI use the last patch in some composer extra patches definitions, but it cannot be longer applied to any version (2.0, 2.1 or 2.x).
Is it possible tu create a new release?
Comment #90
oknateIf you add the entity_browser project like this in composer.json:
"drupal/entity_browser": "dev-2.x",
the patch is included, so you don't need to apply the patch.
With require command:
composer require 'drupal/entity_browser:2.x-dev'Comment #92
a.dmitriiev commentedHere is the patch for the views argument only, without tests, untill the new release is there :)
Forgot the most important part: it applies to current stable 2.1 version.
BTW, thank you for the awesome new feature, looking forward to seeing it in 2.2
Comment #93
dunebl++for this patch!
Great work
Comment #94
oknateI added documentation on drupal.org. Feel free to review/update.