Problem/Motivation

There is a bug in "add another" button inside the entity reference field when using the PostgreSQL database. The bug consists on a sql error inside the ajax requisition, as shown on the attached image, saying that '' isn't a valid integer. This error doesn't happens when using a MySQL database.

Proposed resolution

After some bug tracking, I've found that the root of the problem is that, inside the entityreference_field_widget_form function, there is an entity_load call sending an empty string as an entity_id. The clean solution is if the id is empty inside a previous foreach lace on the code. After this simple change the problem is solved.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tcarmona’s picture

There is the diff patch file of the changes, as described on https://www.drupal.org/patch/submit .

Liam Morland’s picture

Chris Matthews’s picture

Assigned: tcarmona » Unassigned
Status: Needs review » Needs work
Issue tags: +Needs reroll

The 5 year old patch to entityreference.module does not apply to the latest 7.x-1.x-dev and if still relevant needs a reroll.

Checking patch entityreference.module...
error: while searching for:

    // Build an array of entities ID.
    foreach ($items as $item) {
      $entity_ids[] = $item['target_id'];
    }

    // Load those entities and loop through them to extract their labels.

error: patch failed: entityreference.module:818
error: entityreference.module: patch does not apply
rpayanm’s picture

joseph.olstad’s picture

Status: Needs work » Closed (outdated)
commit 6462ae564d067ca95023fa332fa295056df8c3b8
Author: odegard <odegard@703222.no-reply.drupal.org>
Date:   Mon Dec 19 15:28:29 2016 +0000

    Issue #1683964 by hefox, odegard: Entity Reference Error (array_flip, undefined target_id) when using autocomplete and no entites selected

diff --git a/entityreference.module b/entityreference.module
index 3bcfb8d..bde1b4c 100644
--- a/entityreference.module
+++ b/entityreference.module
@@ -849,7 +849,9 @@ function entityreference_field_widget_form(&$form, &$form_state, $field, $instan
 
     // Build an array of entities ID.
     foreach ($items as $item) {
-      $entity_ids[] = $item['target_id'];
+      if (isset($item['target_id'])) {
+        $entity_ids[] = $item['target_id'];
+      }
     }
 
     // Load those entities and loop through them to extract their labels.