I would like the option to set the "Add existing items" field into a select field with all the existing items populated to choose from. Now it seems it is always an autocomplete which works great in most cases. But sometimes there aren't supposed to be that many items and in those cases a selected field would work better I think.

What do you think?

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

reekris created an issue. See original summary.

bojanz’s picture

Our default use case assumes that there will be too many entities to load at once. So the autocomplete must stay.

However, we also must make that dialog overridable, so that you can add that behavior yourself if you want.
That's going to be a post-beta1 task.

slashrsm’s picture

Let me just mentions that it is possible to replace autocomplete with an Entity browser. :)

Mirroar’s picture

Title: Select dropdown for "Add existing items" » Allow different widgets for "Add existing items"

Taking the liberty to rename the issue. I agree with bojanz that the dialog needs to be overridable, that would help me out in one of my current projects, where I would like to replace the autocomplete with a custom form.

I'd suggest using plugins for this, either a new Plugin type, or even the default FieldWidget plugins (minus IEF itself).

Mirroar’s picture

I know we're still in alpha, but I'd be very interested in getting something working for replacing the default autocomplete. I know entity_browser already replaces it by using some funky for alter hooks (`hook_inline_entity_form_reference_form_alter`), overwriting validate callbacks, etc. While that works, it seems like preddy bad developer experience, and I see trouble down the line once multiple modules try altering that form.

So I'd much rather have this solved through Plugins and was wondering whether there were any concrete plans / ideas for this already. I'm willing and able to put some time into this, but I don't want to just go off in a direction I deem appropriate when the maintainers of IEF might have different plans. Therefore I'd be happy about some pointers, otherwise I'll just come back with a patch if I get something decent working.

tkoleary’s picture

+1 to this issue.

It would be a far better experience if, for example, multiple value autocompletes could use Chosen or Select2 libraries.

bradjones1’s picture

As a proof of concept, this is rather easy to do with a form alter on the widget:

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function MYMODULE_field_widget_inline_entity_form_complex_form_alter(&$element, FormStateInterface $form_state, $context) {
  $triggering = $form_state->getTriggeringElement();
  if (!empty($triggering['#ief_form']) && $triggering['#ief_form'] == 'ief_add_existing') {
    /** @var \Drupal\Core\Field\EntityReferenceFieldItemList $items */
    $items = $context['items'];
    $options = $items->getFieldDefinition()
      ->getFieldStorageDefinition()
      ->getOptionsProvider('target_id', $items->getEntity())
      ->getSettableOptions(\Drupal::currentUser());
    $element['form']['entity_id'] = [
      '#type' => 'select',
      '#title' => $element['form']['entity_id']['#title'],
      '#title_display' => 'invisible',
      '#required' => TRUE,
      '#options' => $options,
    ];
  }
}

You would obviously want to focus this on just the parent forms that you would want to adjust, so as not to make the alter site-wide, but it's pretty straightforward.

gordon’s picture

Plugin's would be great here but we not need to reinvent the wheel as there is already plugins for entity reference fields. We just need to use the FieldWidget plugin for entity reference fields and make sure we do not include the Inline Entity Form widget, or things will get interesting.

thejimbirch’s picture

Thanks @bradjones1! Your hook in #7 works great.

geek-merlin’s picture

For us, the code in #7 does not work anymore, as since #2830328: Required IEF forms for existing entities don't work \Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex::extractFormValues has the assumption "autocomplete" hardcoded in it.

As much as i love pluginification, i think a first approach might add a autocomplete / select setting and then let's see how to proceed.

geek-merlin’s picture

Status: Active » Needs review
StatusFileSize
new13.72 KB

Patch flying in with a first shot at this.

EDIT: This patch is onto #2673548: Prioritize 'add existing' in nested entity creation UX to prevent duplication., so if that one gets in first, we already have the rebase...

geek-merlin’s picture

StatusFileSize
new13.82 KB

Patch flying in.

Status: Needs review » Needs work

The last submitted patch, 12: inline_entity_form-2683125-12-add-existing-widget.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

geek-merlin’s picture

Status: Needs work » Needs review
StatusFileSize
new14.32 KB

Forgot updating schema.yml. Here we go.

Status: Needs review » Needs work

The last submitted patch, 14: inline_entity_form-2683125-14-add-existing-widget.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

geek-merlin’s picture

Status: Needs work » Needs review
StatusFileSize
new14.49 KB

Ah OK, removed some erroneous $this from inline_entity_form_extract_autocomplete().
(Ideally all that functions should go into a class, and be left as BC proxies, but that's a followup.)

nimoatwoodway’s picture

Works like charm! Tested with Drupal 8.7.6 and Inline entity form version 8.x-1.0-rc2

geek-merlin’s picture

Status: Needs review » Reviewed & tested by the community

So RTBC if maintainer has no objections.

david.qdoscc’s picture

Patch applies cleanly to 8.x-1.0-rc2, but does not apply to 8.x-1.x-dev

geek-merlin’s picture

Status: Reviewed & tested by the community » Needs work

So this needs rebase. Also a test would be nice.

geek-merlin’s picture

Title: Allow different widgets for "Add existing items" » Allow select widget for "Add existing items"
the_glitch’s picture

Patch doesn't apply to 8.x-1.x-dev

rithesh bk’s picture

Assigned: Unassigned » rithesh bk
Issue tags: +VbContribution2020

currently working on it ....

rithesh bk’s picture

Assigned: rithesh bk » Unassigned
Status: Needs work » Needs review
StatusFileSize
new14.2 KB

Please find the updated patch .......

the_glitch’s picture

Patch #24 works. Thank you Rithesh.

dhirendra.mishra’s picture

+1 for RTBC.

geek-merlin’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

This is great. To keep this maintainable we need a test though.
Just a very simple: "When we select a select widget, we get a select widget. When we select autocomplete, we get autocomplete."

nodecode’s picture

Status: Needs work » Reviewed & tested by the community

It works. Select the select widget and that's what you get. Select the autocomplete widget and hey there's an autocomplete widget.

dww’s picture

Status: Reviewed & tested by the community » Needs work

Still needs (automated) tests written, not just manual testing.

Thanks,
-Derek

tipmisle’s picture

Patch in #24 works for me with the latest dev branch. Awesome work.

zvonimirr’s picture

Patch #24 works with Drupal 9.

geek-merlin’s picture

The related issue reports double-escaping of this patch's select list items.

Crosslinking it, but as it looks more like an upstream issue than this patch's fault, let's keep it separate for now.

geek-merlin’s picture

As the_glitch (crediting here) noted in #3182415: [With patch 2683125-24] Add-existing-item select list double escaped (special characters broken), this breaks with special characters.

Whoever writes TESTS: Please include special characters.

Idea how to fix: In \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase::getOptions i found this that should fix special characters:

      array_walk_recursive($options, [$this, 'sanitizeLabel']);
...
  protected function sanitizeLabel(&$label) {
    // Allow a limited set of HTML tags.
    $label = FieldFilteredMarkup::create($label);
  }

which boils down to something like

      array_walk_recursive($options, [FieldFilteredMarkup::class, 'create']);
w01f’s picture

...

le72’s picture

Why this is not included in the latest release? :-(

dv-vc’s picture

Patch no longer applies cleanly to RC12 or dev

Al Munnings made their first commit to this issue’s fork.

goodmood’s picture

Adding re-rolled patch to apply over RC12 version

geek-merlin’s picture

> Why this is not included in the latest release? :-(

I like the feature. Code looks good overall, but it needs tests.
There are many tests in the module, that can be extended.

kopeboy’s picture

Up

arunkumark’s picture

Status: Needs work » Needs review
StatusFileSize
new394.06 KB
new14.06 KB

The feature is super cool for the IEF.

The patch is not applied with the latest version of the IEF module. I have rerolled the patch #38. Attached patch to,

IEF

podarok’s picture

Version: 8.x-1.x-dev » 2.0.x-dev
Status: Needs review » Needs work
git apply ief-entity-add-select-2683125-41.patch 
error: patch failed: inline_entity_form.module:11
error: inline_entity_form.module: patch does not apply
error: patch failed: src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php:118
error: src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php: patch does not apply

#41 needs reroll

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

jennakoo’s picture

Status: Needs work » Needs review
StatusFileSize
new13.74 KB

Rerolled patch #41 for Drupal 10

  • podarok committed 6304c781 on 2.0.x
    Issue #2683125 by geek-merlin, arunkumark, Rithesh BK, almunnings,...
podarok’s picture

Status: Needs review » Fixed

#45 is in
tnx

Status: Fixed » Closed (fixed)

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

dqd’s picture

Version: 2.0.x-dev » 8.x-1.x-dev
Related issues: +#3401656: Clean up problematic 2.x branch

Regarding #3401656: Clean up problematic 2.x branch we need to tackle issues possibly somewhat got under the mist of the latest branch confusion. And since this FR here has been hijacked for the now-closed 2.x branch in the middle of its progress for the 1.x branch, I do not know if it has ever landed in the 1.x branch. Let me link this issue to the clean-up issue and let me set version worked on back to 8.x-1.x-dev for a possible re-roll. Maintainers need to re-open if required.

Latest patch possibly useful to get forward in 1.x branch is in comment #41.

Tested on latest ief 8.x dev:

git apply -v ief-entity-add-select-2683125-41.patch
Checking patch config/schema/inline_entity_form.schema.yml...
Checking patch inline_entity_form.module...
Checking patch src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php...
Applied patch config/schema/inline_entity_form.schema.yml cleanly.
Applied patch inline_entity_form.module cleanly.
Applied patch src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php cleanly.
geek-merlin’s picture

Status: Closed (fixed) » Needs review

Bulk reopen.

geek-merlin’s picture

Status: Needs review » Needs work

As tagged and re-stated in #33 ands #39, this needs a test.

dww’s picture

StatusFileSize
new14.06 KB

Still needs tests, but here's a reroll of #41 that applies to both 8.x-1.x and 3.x branches.

dww’s picture

StatusFileSize
new13.97 KB
new1.46 KB

The settings summary was previously buggy, since it would talk about the autocomplete matching operator even when set to use the select widget. This cleans that up and makes the summary more verbose.

dww’s picture

StatusFileSize
new14.7 KB
new2.65 KB

yeah, seems the automated tests are kinda broken until #3410055: Adopt GitlabCi is merged. But here's an updated patch that, once the latest code from #3410055 is applied, gives me passing tests locally...

dww’s picture

StatusFileSize
new25.13 KB

E.g. here's #55 and the latest code from #3410055 in 1 patch. This should be green. 🤞🙏

geek-merlin’s picture

Version: 8.x-1.x-dev » 3.x-dev
dww’s picture

StatusFileSize
new14.41 KB

Re-roll now that #3410055: Adopt GitlabCi landed. Also, removing the bogus config schema change for the simple widget. This is only a new setting for the complex widget.

Still needs tests of the new feature, so leaving NW, although the existing tests should be green at last.

gaurav_manerkar’s picture

Hello,
Callback added for #ief_extract_values_callback doesn't trigger for all the items when parent field contains cardinality > 1.
I am facing the problem on v1 for IEF. The same problem may be present on v3 as well.

flefle’s picture

Version: 3.x-dev » 3.0.0-rc15
StatusFileSize
new14.4 KB

Updated Patch for 3.0RC17

mlncn’s picture

Version: 3.0.0-rc15 » 3.0.0-rc20
Status: Needs work » Needs review
kopeboy’s picture

Status: Needs review » Reviewed & tested by the community

Yep, patch in #60 worked, and didn't seem to break anything! Thank you all!

geek-merlin’s picture

Version: 3.0.0-rc20 » 3.x-dev
Status: Reviewed & tested by the community » Needs work

Drupalorg does not test patches anymore. Can someone create a MR of it?

arunkumark’s picture

Status: Needs work » Needs review

Created MR for patch.

geek-merlin’s picture

Status: Needs review » Needs work

Thanks for the MR! Which has test failures, so NW for that.
https://git.drupalcode.org/issue/inline_entity_form-2683125/-/jobs/3297570

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

pameeela’s picture

StatusFileSize
new14.2 KB

Re-rolled the branch and attaching a patch file to use for this. It's working for us, but I haven't looked into the test fails so still NW.