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:

  1. What entity bundles should be shown in the View
  2. 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.

CommentFileSizeAuthor
#92 default_argument.patch9.48 KBa.dmitriiev
#75 entity-browser-view-context-2865928-75.patch65.37 KBoknate
#75 2865928--intediff-73-75.txt1.43 KBoknate
#73 entity-browser-view-context-2865928-73.patch65.5 KBoknate
#70 entity-browser-view-context-2865928-70.patch65.51 KBoknate
#70 interdiff-54-70.txt28.63 KBoknate
#68 entity-browser-view-context-2865928-68.patch64.96 KBoknate
#68 interdiff-65-68.txt1.47 KBoknate
#67 entity-browser-view-context-2865928-67.patch65.08 KBoknate
#67 interdiff-65-67.txt1.57 KBoknate
#65 entity-browser-view-context-2865928-65.patch64.66 KBoknate
#65 interdiff-63-65.txt748 bytesoknate
#63 entity-browser-view-context-2865928-63.patch64.64 KBoknate
#63 interdiff-58-63.txt378 bytesoknate
#60 entity-browser-view-context-2865928-58.patch64.27 KBoknate
#60 interdiff-56-58.txt1.1 KBoknate
#58 entity-browser-view-context-2865928-56.patch64.23 KBoknate
#56 entity-browser-view-context-2865928-56.patch64.23 KBoknate
#56 interdiff-55-56.txt18.5 KBoknate
#54 entity-browser-view-context-2865928-54.patch71.72 KBoknate
#54 interdiff-51-54.txt18.89 KBoknate
#53 entity-browser-view-context-2865928-53.patch57.42 KBoknate
#51 entity-browser-view-context-2865928-51.patch57.42 KBoknate
#51 interdiff-50-51.txt776 bytesoknate
#50 entity-browser-view-context-2865928-50.patch56.66 KBoknate
#50 interdiff-48-50.txt1.29 KBoknate
#48 entity-browser-view-context-2865928-48.patch56.57 KBoknate
#48 interdiff-46-48.txt745 bytesoknate
#46 entity-browser-view-context-2865928-46.patch56.12 KBoknate
#46 interdiff-43-46.txt3.03 KBoknate
#43 entity-browser-view-context-2865928-43.patch55.5 KBoknate
#43 interdiff-39-43.txt8.53 KBoknate
#40 entity-browser-view-context-2865928-39.patch50.21 KBoknate
#40 interdiff-37-39.txt631 bytesoknate
#38 interdiff-33-37.txt23.06 KBoknate
#37 entity-browser-view-context-2865928-37.patch49.92 KBoknate
#3 2761833-2865928-3-combined.patch22.66 KBsamuel.mortenson
#3 entity-browser-view-context-2865928-3.patch8.72 KBsamuel.mortenson
#5 2761833-2865928-5-combined.patch37.79 KBsamuel.mortenson
#5 entity-browser-view-context-2865928-5.patch23.21 KBsamuel.mortenson
#8 2761833-2865928-8-combined.patch38.21 KBsamuel.mortenson
#8 entity-browser-view-context-2865928-8.patch23.51 KBsamuel.mortenson
#9 entity-browser-view-context-2865928-9.patch23.51 KBsamuel.mortenson
#15 entity-browser-view-context-2865928-14.patch24.45 KBrecrit
#29 interdiff-14-29.txt2.8 KBoknate
#29 entity-browser-view-context-2865928-29.patch25.3 KBoknate
#29 notes update.png134.52 KBoknate
#30 entity-browser-view-context-2865928-30.patch26.48 KBoknate
#30 interdiff-29-30.patch1.18 KBoknate
#32 entity-browser-view-context-2865928-32.patch26.37 KBoknate
#33 interdiff-32-33.txt3.51 KBoknate
#33 entity-browser-view-context-2865928-33.patch26.86 KBoknate

Comments

samuel.mortenson created an issue. See original summary.

samuel.mortenson’s picture

Title: The View widget should be aware filter based on field settings » The View widget should filter based on field settings
samuel.mortenson’s picture

Here'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.

samuel.mortenson’s picture

samuel.mortenson’s picture

Status: Active » Needs review
Issue tags: -Needs tests
StatusFileSize
new37.79 KB
new23.21 KB

Added test coverage!

The last submitted patch, 5: 2761833-2865928-5-combined.patch, failed testing.

The last submitted patch, 5: 2761833-2865928-5-combined.patch, failed testing.

samuel.mortenson’s picture

StatusFileSize
new38.21 KB
new23.51 KB

This should fix tests.

samuel.mortenson’s picture

StatusFileSize
new23.51 KB

Just 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.

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Lightning is using this patch. It works for us, and we like it too. I think that's RTBC.

slashrsm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +D8Media
+++ b/src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php
@@ -560,11 +560,15 @@
+    $handler = $settings['handler_settings'];
...
+      'widget_context' => [
+        'target_bundles' => !empty($handler['target_bundles']) ? $handler['target_bundles'] : [],

This 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.

samuel.mortenson’s picture

@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?

samuel.mortenson’s picture

Status: Needs work » Needs review

@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.

legolasbo’s picture

Status: Needs review » Reviewed & tested by the community

After 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)

recrit’s picture

Attached 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 .

recrit’s picture

Status: Reviewed & tested by the community » Needs review

re-queuing tests.

Narretz’s picture

Hi 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?

samuel.mortenson’s picture

Hi @narretz, thanks for testing out the patch!

The filtering works, but I assumed that this patch would also restrict the values in the filter dropdown to the allowed media bundles.

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:

      /** @var \Drupal\views\ViewExecutable $view */
      $view = $form_state->get('view');
      if ($view instanceof \Drupal\views\ViewExecutable && isset($view->argument['type'])) {
        /** @var \Drupal\node\Plugin\views\argument\Type $type */
        $type = $view->argument['type'];
        $value = $type->getValue();
        if (!is_null($value) && !$type->isException($value)) {
          $types = explode('+', $value);
          $types[] = 'All';
          foreach ($form['type']['#options'] as $name => $option) {
            if (!in_array($name, $types, TRUE)) {
              unset($form['type']['#options'][$name]);
            }
          }
        }
      }

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.

I assume this means the "Bundle" entry of the media module in the list of contextual filters, right? -> correct

Yes, and for the Node module you would add the "Content Type" contextual filter.

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?

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.

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?

You would need to add the contextual filter for displays that use the Entity Browser display.

martijn de wit’s picture

Status: Needs review » Needs work

Tested 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.

martijn de wit’s picture

szeidler’s picture

I'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.

szeidler’s picture

I 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.

kerasai’s picture

I'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?

martijn de wit’s picture

We 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

iampuma’s picture

Used drupal/entity_browser (1.5.0) with the settings mentioned in #15 and works perfect.

samuel.mortenson’s picture

Assigned: samuel.mortenson » Unassigned
prineshazar’s picture

#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!

oknate’s picture

Tested 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?

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new2.8 KB
new25.3 KB
new134.52 KB

Here'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.

defaults and help text.

oknate’s picture

This patch expands on #14 and #29.

This patch adds integration with inline entity form so that the default value views plugin works.

oknate’s picture

Title: The View widget should filter based on field settings » Provide method for views widget to filter based on context
Issue summary: View changes
oknate’s picture

StatusFileSize
new26.37 KB

Reroll of #30 (arguments for FileBrowserWidget have changed)

oknate’s picture

StatusFileSize
new3.51 KB
new26.86 KB

Adding 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.

Status: Needs review » Needs work

The last submitted patch, 33: entity-browser-view-context-2865928-33.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

oknate’s picture

Status: Needs work » Needs review

Last failure was random. Marking back to "Needs Review".

oknate’s picture

Adding test coverage for inline entity form

oknate’s picture

StatusFileSize
new23.06 KB

Status: Needs review » Needs work

The last submitted patch, 37: entity-browser-view-context-2865928-37.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

StatusFileSize
new631 bytes
new50.21 KB

working on fixing tests

oknate’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 40: entity-browser-view-context-2865928-39.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

StatusFileSize
new8.53 KB
new55.5 KB

Working on fixing tests

oknate’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 43: entity-browser-view-context-2865928-43.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new3.03 KB
new56.12 KB

Working on fixing tests

Status: Needs review » Needs work

The last submitted patch, 46: entity-browser-view-context-2865928-46.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
Status: Needs work » Needs review
StatusFileSize
new745 bytes
new56.57 KB

working on fixing tests

Status: Needs review » Needs work

The last submitted patch, 48: entity-browser-view-context-2865928-48.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new1.29 KB
new56.66 KB

working on fixing tests

oknate’s picture

StatusFileSize
new776 bytes
new57.42 KB

There are random failures on line 77 of PluginsTest. I have noticed this on other issues too. Seeing if this fixes it.

oknate’s picture

Version: 8.x-2.x-dev » 8.x-1.x-dev

For 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.

oknate’s picture

Same 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.

oknate’s picture

StatusFileSize
new18.89 KB
new71.72 KB

Adding test coverage for entity embed! That's the last @todo item.

Status: Needs review » Needs work

The last submitted patch, 54: entity-browser-view-context-2865928-54.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

StatusFileSize
new18.5 KB
new64.23 KB

- 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.

oknate’s picture

Status: Needs work » Needs review
oknate’s picture

testing 56 against 8.x-2.x

Status: Needs review » Needs work

The last submitted patch, 58: entity-browser-view-context-2865928-56.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

Working on fixing tests

oknate’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 60: entity-browser-view-context-2865928-58.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new378 bytes
new64.64 KB

Working on fixing tests

oknate’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
oknate’s picture

StatusFileSize
new748 bytes
new64.66 KB

Working on fixing tests

Status: Needs review » Needs work

The last submitted patch, 65: entity-browser-view-context-2865928-65.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

StatusFileSize
new1.57 KB
new65.08 KB

Working on fixing tests, sorry for the noise.

oknate’s picture

Working on fixing tests

oknate’s picture

I 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.

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new28.63 KB
new65.51 KB

Updated composer require dev to fix test dependencies on entity_embed and embed. Thanks to Berdir for the info.

Status: Needs review » Needs work

The last submitted patch, 70: entity-browser-view-context-2865928-70.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

Status: Needs work » Reviewed & tested by the community

I 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.

oknate’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new65.5 KB

Reroll

Status: Needs review » Needs work

The last submitted patch, 73: entity-browser-view-context-2865928-73.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new1.43 KB
new65.37 KB
oknate’s picture

Status: Needs review » Reviewed & tested by the community

Marking back as RTBC.

oknate’s picture

oknate’s picture

  • oknate committed a3987ee on 8.x-2.x
    git commit -m 'Issue #2865928 by samuel.mortenson, oknate: Provide...

  • oknate committed 3c65efe on 8.x-1.x
    git commit -m 'Issue #2865928 by samuel.mortenson, oknate: Provide...
oknate’s picture

Status: Reviewed & tested by the community » Fixed

Committed! Thanks to Samuel Mortenson, and to all who reviewed.

oknate’s picture

Issue summary: View changes
oknate’s picture

martijn de wit’s picture

Great work @oknate!

One side note, I see the whole git command in your git message(s).
https://cgit.drupalcode.org/entity_browser

"git commit -m 'Issue #2865928 by samuel.mortenson, oknate: Provide method for views widget to filter based on context' --author="samuel.mortenson <samuel.mortenson@2582268.no-reply.drupal.org>" "
oknate’s picture

Oh, how embarrassing. I use tower git. I'll fix that going forward, thanks. I'm not sure I can fix the ones already added.

samuel.mortenson’s picture

Really cool to see this go in!

oknate’s picture

Yes, I considered waiting until the 31st, which would make it two years in the making! Thanks again for the new feature.

oknate’s picture

I'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.

vpeltot’s picture

I 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?

oknate’s picture

If 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'

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

a.dmitriiev’s picture

StatusFileSize
new9.48 KB

Here 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

dunebl’s picture

++for this patch!
Great work

oknate’s picture

I added documentation on drupal.org. Feel free to review/update.