Once I've selected an entity type, it would be nice if I could select a bundle via select field that helps narrow the list of autocomplete options so that I can know for sure that I've selected the correct entity. Several sites I've worked on have nodes with the same title, but different types and would find the UX of this widget hard to use and confusing to pick the correct result.

Issue fork der_extra-2381991

Command icon 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

jibran’s picture

Thanks for creating a feature request I am working on this same feature in #2346273: Allow user to select bundles per entity type on field settings page. I think it is really close now. After fixing #2346273 I am planning to work on autocomplete route to use the same settings selected in field settings form. So #2346273 will fix this issue but let's keep it open for autocomplete route update. We can postpone this on a #2346273 and re-title this.

dave reid’s picture

Yeah, just to clarify, the problem is I don't want to limit selection to certain bundles, but just in the widget only, not the field settings.

jibran’s picture

As I suggested here #2423093-5: Allow multiple target entity types in the 'entity_autocomplete' Form API element we can implement hierarchical select type widget to select bundles. We can add a widget option to turn this setting on. We'll only show the bundles selected in field settings. Let me complete #2411981: Fixes after generic 'entity_autocomplete' Form API element first then I'll have a look at it.

jibran’s picture

This is totally possible if we change traget_bundles in handler_settings but selection handler settings are stored in \Drupal::keyValue('entity_autocomplete') before the creation of autocomplete link. One way to fix it is to add a target bundle select and trigger AJAX onchange to change the selection settings. We should do it in a new widget.

rp7’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
StatusFileSize
new10.21 KB

I was working on something similar for a project of mine, only found this issue afterwards. Patch attached adds a new widget Autocomplete (per bundle) for Dynamic Entity Reference fields.

Seems to work pretty well, improvements/reviews always welcome.

rp7’s picture

StatusFileSize
new10.2 KB

Removed a tiny project-specific left-over.

jibran’s picture

Nice one. Can you please share some screenshots? Should we add it to https://www.drupal.org/sandbox/jibran/2932751?

+++ b/src/Plugin/Field/FieldWidget/DynamicEntityReferenceBundleWidget.php
@@ -0,0 +1,269 @@
+          $available[$target_type . ':' . $bundle_id] = $target_type . ':' . $bundle_id;

Instead of doing this can we create target type dependent bundle select(s) using states?

rp7’s picture

StatusFileSize
new10.97 KB

A few more bugs I found in my previous patch:

  • Default value of the select value was not always correctly set
  • No aphabetical ordering of select options
  • When no specific bundles configured, the behavior reverted to the default one
rp7’s picture

StatusFileSize
new11.05 KB

Bundle default value still not always correct, hopefully finally fixed now. Sorry for the spam!

rp7’s picture

StatusFileSize
new79.94 KB
new67.86 KB

@jibran
For the screenshots:

The default autocomplete widget:
Default widget

The per bundle autocomplete wiget:
Bundle widget

The above screenshots were taken from an installation with the following entity types & bundles:

  • The Node entity type with following bundles: Country, Landing page, Map, News article, Page, Press release, Product, Publication, Story, Webform
  • The Publication entity type with no bundles
  • The Press release entity type with no bundles
rp7’s picture

StatusFileSize
new75.55 KB
rp7’s picture

@jibran

Instead of doing this can we create target type dependent bundle select(s) using states?

Honestly no idea what you mean with this. Can you elaborate a little bit more? Perhaps give a raw code example? Not sure if I can make more time available for this the next few days/weeks, but I can try.

wim leers’s picture

I think @jibran means:

[ENTITY TYPE] [BUNDLE] [… autocomplete …]

instead of HEAD's

[ENTITY TYPE] [… autocomplete …]

or your patch's

[ENTITY TYPE if bundleless or BUNDLE otherwise] [… autocomplete …]

(That's also what he was alluding to in #3.)

rp7’s picture

StatusFileSize
new11.39 KB

I have a (new) use case where I need to have an AJAX callback triggered by a change event on the target_id element. I noticed an issue that - when replacing the completely rendered field through AJAX - the autocomplete gave suggestions for the wrong bundle. Fixed it in the patch attached.

FYI, we have been using this widget for about 2 years now on a decently sized production site. It's used in 10+ content types, by 30+ editors. No issues so far. Wondering how we should proceed with this (knowing this is - obviously - not the finished product yet):

  1. Make it part of this project as a separate widget
  2. Make it part of this project and provide an extra option to the default DER widget that allow selecting per bundle (instead of per entity type)
  3. Make it part of https://www.drupal.org/sandbox/jibran/2932751 (if so, a start would be to move this issue to that project)
jibran’s picture

Project: Dynamic Entity Reference » DER Extra
Version: 8.x-2.x-dev »

Let's do this. We can promote this to a full project as well.

I still think we should still consider exploring the following option:

[ENTITY TYPE] [BUNDLE] [… autocomplete …]

jibran’s picture

Version: » 1.0.x-dev
Status: Active » Needs review
Issue tags: -Needs tests
p-neyens’s picture

My patch contains the fix for the incorrect namespace path.

rp7’s picture

Updated patch because the schema info for the new widget was missing.

PS: I also added a patch based on #14 (where the widget was still part of the dynamic_entity_reference module, and not the der_extra module). This because the der_extra module apparently requires the Chosen module - something that isn't needed for this widget & I don't want to install on my project. IMO we'll have to address this as well before this is committed to the der_extra module.

rp7’s picture

rp7’s picture

Updated patch so that the first select option is now selected by default

gugalamaciek made their first commit to this issue’s fork.

gugalamaciek’s picture