FieldWidget for the multyselect entity reference fiels does not work correctly. It attachs right. But It renders nothing. Look two screenshots to find a problem.

Comments

Anonymous’s picture

Status: Active » Needs review
StatusFileSize
new1.24 KB

The problem was that the variable is modified because we send it as an object by reference. I copied the instance of the object.

Status: Needs review » Needs work

The last submitted patch, 1: drupal-autocomplete_widget_bug-2156649-1.patch, failed testing.

Anonymous’s picture

StatusFileSize
new1.21 KB
Anonymous’s picture

Status: Needs work » Needs review

I ran --class "Drupal\entity_reference\Tests\EntityReferenceAutocompleteTest" without patch. It showed everything OK.

Anonymous’s picture

I can not find a test for multiple values.

andypost’s picture

Component: field_ui.module » entity_reference.module

proper component

andypost’s picture

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

So just extend one of existing tests to multple items, it's not clear how to reproduce bug

jrockowitz’s picture

I can confirm the issue described and the patch provided by likin in comment 3 fixed the issue.

Anonymous’s picture

StatusFileSize
new3.11 KB
Anonymous’s picture

Status: Needs work » Needs review
StatusFileSize
new5.15 KB
Anonymous’s picture

Status: Needs review » Needs work
Anonymous’s picture

Status: Needs work » Needs review
StatusFileSize
new5.21 KB
Anonymous’s picture

This patch, task exposes new two problems:
* entity_reference_create_instance does not support FieldDefinitionInterface::CARDINALITY_UNLIMITED
https://drupal.org/node/2167975
* $entity_loaded->{$this->fieldName}->count() always return ammount + 1. $entity_loaded->{$this->fieldName} also contains empty object.

dawehner’s picture

  1. +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldWidget/AutocompleteWidget.php
    @@ -41,14 +41,15 @@ class AutocompleteWidget extends AutocompleteWidgetBase {
    +    $item = clone $items;
    

    If this clone is really required you should at least explain why

  2. +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceWidgetTest.php
    @@ -0,0 +1,113 @@
    +      } else {
    

    The codestyle of drupal adds a linebreak here.

  3. +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceWidgetTest.php
    @@ -0,0 +1,113 @@
    +    // Checks.
    

    It would be maybe helpful to exlain what is checked...

  4. +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceWidgetTest.php
    @@ -0,0 +1,113 @@
    +class EntityReferenceWidgetTest extends EntityUnitTestBase {
    

    A kind of unit test is certainly great here, but I wonder whether having a webtest would be kind of helpful here, given that this is some actual high level user problem.

Anonymous’s picture

$items->setValue(array($items[$delta]->getValue()))

It makes from multi value to a single value.

We need clone $items.

johnpitcairn’s picture

Patch at #12 applies to D8 alpha 7, and fixes a problem where only the first field value is loaded when editing a node with a multiple-value entity reference field, and saving the node destroys subsequent field values.

amateescu’s picture

Title: FieldWidget for the multyselect entity reference fiels does not work correctly. » The 'entity_reference_autocomplete' widget doesn't handle multiple values correctly
Priority: Normal » Major
Issue tags: -Needs tests
StatusFileSize
new6.91 KB
new11.75 KB

Here's another approach which doesn't mess with the $items object anymore by moving that piece of functionality to a separate helper method.

I also brought in the complete tests from #2160575-27: Option widgets integration is broken for the entity reference field which covers multiple values properly.

dawehner’s picture

+++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldWidget/AutocompleteTagsWidget.php
@@ -49,6 +49,9 @@ public function elementValidate($element, &$form_state, $form) {
+        elseif (preg_match("/.+\(([\w.]+)\)/", $input, $matches)) {
+          $match = $matches[1];
+        }

Is there a reason why this should be part of this patch?

amateescu’s picture

That's because the new tests expose a bug in the 'tags' widget so I needed to bring in the fix as well to get them to pass.

The last submitted patch, 17: 2156649-17-test-only.patch, failed testing.

The last submitted patch, 17: 2156649-17-test-only.patch, failed testing.

dawehner’s picture

Mh I wonder whether we could document this new regex, these are just always hard to understand.

amateescu’s picture

StatusFileSize
new12.08 KB
new1.12 KB

Fair enough :)

Status: Needs review » Needs work

The last submitted patch, 23: 2156649-23.patch, failed testing.

The last submitted patch, 23: 2156649-23.patch, failed testing.

amateescu’s picture

Status: Needs work » Needs review

I manually stopped the testbot since we have a backlog of 150 (!!!) patches and the last patch is just about some new comments (and the one from #17 passed).

amateescu’s picture

Issue tags: +SprintWeekend2014
berdir’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, this should be ready then, nice clean-up and comes with tests :)

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

amateescu’s picture

Status: Fixed » Reviewed & tested by the community
StatusFileSize
new1.35 KB
amateescu’s picture

Status: Reviewed & tested by the community » Fixed

Committed by @catch: 60fb1aa.

yched’s picture

I don't really understand the logic of
getEntityIds($items, $delta)
getLabels($items, $delta)

- If they're supposed to work on a specific $delta, why would they return an *array* of ids / labels ?
- AutocompleteWidgetBase::getEntityIds($items, $delta) receives a $delta but doesn't use it, instead it iterates on all $items ?
- The override in AutocompleteWidget returns only the id for the specific $delta ?

Related : I would really welcome anyone able to work on #2175343: Pass a FieldItem instead of FieldItemListInterface in WidgetInterface::formElements :-)

berdir’s picture

That's because entity reference has two different autocomplete widgets, one with a text field per item and one that lists all. This dates back to 7.x code, we just switched to a less crazy override for that use case.

yched’s picture

Well, then it seems the two implementations of getEntityIds() are actually different use cases that cannot really live in the same method under the same contract / phpdoc ?

amateescu’s picture

Well, the use case to get entity IDs from the $items array, one widget needs all of them and the other needs just the one matching a delta. I'm not sure why this is so different..

Status: Fixed » Closed (fixed)

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