Problem/Motivation

Entity Browser should provide a simple way to create views that dynamically limit bundles (including within exposed filters). This would be similar in functionality the default value widget recently added (#2865928: Provide method for views widget to filter based on context), but would live under the "Filters" section of views, and behave like the type filter. An another advantage over 2865928, is it wouldn't require configuration as far as what #widget_context property to use, so it would be more user friendly, IMHO.

Here's a screenshot of me testing default value widget with exposed filter, after creating nodes labeled "Article 1", "Jet 1" and "Shark 1", and set up using entity_browser_test module the widget_context_default_value view, but adding the type filter with exposed option. On the field widget I set the allowed bundles to "shark" and "jet".
You can see the default value widget is limiting the choices, as "Article 1" doesn't appear, but the exposed filter option doesn't limit the choices. I'm not sure that is possible using a default value widget anyway, and is out of scope for a default value widget.

default value plugin

Proposed resolution

Create bundle plugin that reacts to widget_context, that allows exposed filter to also adjust.

When you add the filter, the options are displayed, but disabled:

options disabled

When exposed filter option enabled, the contextual filter
1) should only show types allowed within #widget_context['target_bundles'] and "all" option
2) "all" option when applied should only show types allowed within #widget_context['target_bundles']
3) If #widget_context['target_bundles'] only has one bundle, hide exposed filter form item.

limit options in exposed select dropdown

Remaining tasks

- Review

User interface changes

Adds additional view filter plugin option, when using entity browser display.

API changes

Data model changes

Adds config schema for new views plugin.

Comments

oknate created an issue. See original summary.

oknate’s picture

Here's an initial patch.

It's dependent on changes to add #widget_context information made in #2865928: Provide method for views widget to filter based on context (built initially with patch in comment 33).

bundle filter

oknate’s picture

oknate’s picture

The initial patch I think is awesome and super helpful.

Todo
- Get "exposed" option working: done!
- Make sure only visible with entity browser view display. done!
- test coverage

oknate’s picture

Status: Active » Needs review
StatusFileSize
new10.42 KB
new470 bytes

- Fixed exposed filter option, although I haven't tested some of the options, such as multiple. Some nice features:
- If there is only one bundle available on a field, exposed filter disappears.

oknate’s picture

StatusFileSize
new11.62 KB

Oops last post included wrong file, here's the patch. Also, I removed dependency on #2865928: Provide method for views widget to filter based on context (by copying the #widget_context code).

oknate’s picture

Update adds a form alter hook that hides the filter when editing a view display that isn't an entity_browser display.

oknate’s picture

Adding test coverage. Since this is similar to the default_value view plugin in functionality, it was easy to copy these over and adjust.

Still to do: add test for exposed filter functionality.

Status: Needs review » Needs work

The last submitted patch, 8: entity-browser-contextual-bundle-3039038-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

Issue summary: View changes
StatusFileSize
new94.29 KB
oknate’s picture

Issue summary: View changes
oknate’s picture

Title: Provide views bundle filter that changes based on context » Provide type filter that limits based based on widget context
oknate’s picture

Issue summary: View changes
oknate’s picture

oknate’s picture

Issue summary: View changes
oknate’s picture

Issue summary: View changes
oknate’s picture

Issue summary: View changes
oknate’s picture

Issue summary: View changes
oknate’s picture

Issue summary: View changes
oknate’s picture

Issue summary: View changes
StatusFileSize
new95.32 KB
oknate’s picture

Issue summary: View changes
oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new36.48 KB
new68.73 KB

Adding tests coverage for exposed filters in field widget, entity embed and inline entity form contexts.

Also, merging in changes from #2865928: Provide method for views widget to filter based on context, which is now in dev.

Note, converts EntityBrowserTest and InlineEntityFormTest to use web driver instead of phantom js, copied from #3040770: Update test classes extending EntityBrowserJavascriptTestBase.

oknate’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 22: entity-browser-contextual-bundle-3039038-22.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new69.52 KB
new1.06 KB
oknate’s picture

Title: Provide type filter that limits based based on widget context » Provide views filter that limits based based on allowed bundles
oknate’s picture

reroll

kellyimagined’s picture

@oknate the patch is failing to apply, see below for the error.

git apply -v /Patches/entity_browser/entity-browser-contextual-bundle-3039038-27.patch
Checking patch config/schema/entity_browser.schema.yml...
Hunk #1 succeeded at 251 (offset 21 lines).
Checking patch entity_browser.module...
Checking patch entity_browser.views.inc...
Checking patch src/Plugin/views/filter/ContextualBundle.php...
Checking patch tests/modules/entity_browser_entity_embed_test/config/install/editor.editor.full_html.yml...
Checking patch tests/modules/entity_browser_entity_embed_test/config/install/embed.button.bundle_filter_test.yml...
Checking patch tests/modules/entity_browser_test/config/install/entity_browser.browser.bundle_filter.yml...
Checking patch tests/modules/entity_browser_test/config/install/views.view.bundle_filter.yml...
Checking patch tests/modules/entity_browser_test/config/install/views.view.bundle_filter_exposed.yml...
Checking patch tests/src/FunctionalJavascript/EntityBrowserTest.php...
Hunk #1 succeeded at 41 (offset 4 lines).
Hunk #2 succeeded at 49 (offset 4 lines).
Hunk #3 succeeded at 92 (offset 4 lines).
Hunk #4 succeeded at 131 (offset 4 lines).
Hunk #5 succeeded at 173 (offset 4 lines).
Hunk #6 succeeded at 194 (offset 4 lines).
Hunk #7 succeeded at 209 (offset 4 lines).
error: while searching for:
  }

  /**
   * Tests the EntityBrowserWidgetContext argument_default views plugin.
   */
  public function testContextualFilter() {
    $this->createNode(['type' => 'shark', 'title' => 'Luke']);
    $this->createNode(['type' => 'jet', 'title' => 'Leia']);
    $this->createNode(['type' => 'article', 'title' => 'Darth']);

error: patch failed: tests/src/FunctionalJavascript/EntityBrowserTest.php:266
error: tests/src/FunctionalJavascript/EntityBrowserTest.php: patch does not apply
Checking patch tests/src/FunctionalJavascript/EntityEmbedTest.php...
Checking patch tests/src/FunctionalJavascript/InlineEntityFormTest.php...
kellyimagined’s picture

Status: Needs review » Needs work
oknate’s picture

Status: Needs work » Needs review

Hmm, I just tested against the latest dev branches 8.x-2.x and 8.x-1.x and it applied. Can you double check you're using the latest dev branch?

I suspect human error, because it passed on the drupalci testbot 17 hours ago.

kellyimagined’s picture

Status: Needs review » Reviewed & tested by the community

The solution in #30 works great and provides a solution to the issue.

oknate’s picture

minor changes to EntityBrowserTest, in patch for #27 I had some older code changes, changing those back to the way they are in 8.x-2.x.

berdir’s picture

  1. +++ b/entity_browser.views.inc
    @@ -22,6 +22,14 @@ function entity_browser_views_data_alter(&$data) {
    +
    +      $data[$base_table]['entity_browser_bundle'] = [
    +        'title' => t('Entity Browser Target Bundles'),
    +        'filter' => [
    +          'id' => 'entity_browser_bundle',
    +          'real field' => $entity_type->getKey('bundle'),
    +        ],
    

    should this have a condition on the entity type actually having a bundle key? e.g. users or paragraph library item might get confused if you try to add this.

  2. +++ b/src/Plugin/views/filter/ContextualBundle.php
    @@ -0,0 +1,233 @@
    +      $container->get('request_stack')->getCurrentRequest(),
    

    you shouldn't inject the actual request but always the stack and get the current request when you need it. While maybe not likely in this context, subrequests result in the current request changing over time.

  3. +++ b/src/Plugin/views/filter/ContextualBundle.php
    @@ -0,0 +1,233 @@
    +    // Disable element on config form.
    +    if ($this->currentRequest->attributes->get('_route') == 'views_ui.form_handler') {
    +      $form['value']['#default_value'] = array_combine(array_keys($form['value']['#options']), array_keys($form['value']['#options']));
    +      $form['value']['#disabled'] = TRUE;
    +      $form['value']['#description'] = $this->t('@entity types will update in response to entity browser context.', ['@entity' => $this->entityType->getLabel()]);
    +    }
    

    you can use \Drupal\Core\Entity\EntityType::getBundleLabel() here, then you get for example "Vocabulary" instead of "Term types" which is not a thing. And's it better for translation, e.g. in german it is "Inhaltstyp" and not "Inhalt Typ"

    However, there is no plural of that, but that is extremely tricky to properly translate anyway, so maybe you can reword this to avoid the plural?

    Just noticed that the parent uses "@entity types" too, I guess that's where it is from. Maybe you can just use the proper label for $this->valueTitle and avoid repeating it in the description? Or just create a core issue to improve the default there.

oknate’s picture

I addressed the three pointers in #33. Thanks for the feedback, Berdir.

For number #3, I just took out the translatable label:

-      $form['value']['#description'] = $this->t('@entity types will update in response to entity browser context.', ['@entity' => $this->entityType->getLabel()]);
+      $form['value']['#description'] = $this->t('You cannot edit this list because the options update in response to entity browser context.');
berdir’s picture

Thanks, didn't test but this is certainly useful and I think ready. Only did a pretty cursory review and a few parts look a bit weird ( that route checks, checkbox disabling and dependency overriding stuff) but I doubt there's a better solution.

  • oknate committed ebe4811 on 8.x-2.x
    Issue #3039038 by oknate: Provide views filter that limits based based...

  • oknate committed 1834e6f on 8.x-1.x
    Issue #3039038 by oknate: Provide views filter that limits based based...

oknate’s picture

Sounds good to me. We can improve it later if there is a need.

We have test coverage for field widget, inline entity form and entity embed, so it I think we have good proof that it works, and it's pretty low risk since it's a stand-alone plugin that people would optionally enable.

I'm adding credit to samuel.mortenson as well, since this was built off his work for #2865928: Provide method for views widget to filter based on context.

I'm excited to get this in. On my last project, I created five or six entity browsers for different types of nodes that each had a matching view with different bundles enabled. With this in there, you could have one entity browser and one view that changes contextually. While issue 2865928 did that, it didn't provide a solution for exposed filters. So this is a very helpful tool.

I'll add documentation to the official documentation on how to use these two views plugins soon.

Status: Fixed » Closed (fixed)

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

oknate’s picture

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