diff --git a/entityreference.module b/entityreference.module index 08f5c86..6578d53 100644 --- a/entityreference.module +++ b/entityreference.module @@ -818,20 +818,23 @@ 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 (!empty($item['target_id'])) { + $entity_ids[] = $item['target_id']; + } } - // Load those entities and loop through them to extract their labels. - $entities = entity_load($field['settings']['target_type'], $entity_ids); - - foreach ($entities as $entity_id => $entity_item) { - $label = $handler->getLabel($entity_item); - $key = "$label ($entity_id)"; - // Labels containing commas or quotes must be wrapped in quotes. - if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) { - $key = '"' . str_replace('"', '""', $key) . '"'; + if ($entity_ids) { + // Load those entities and loop through them to extract their labels. + $entities = entity_load($field['settings']['target_type'], $entity_ids); + foreach ($entities as $entity_id => $entity_item) { + $label = $handler->getLabel($entity_item); + $key = "$label ($entity_id)"; + // Labels containing commas or quotes must be wrapped in quotes. + if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) { + $key = '"' . str_replace('"', '""', $key) . '"'; + } + $entity_labels[] = $key; } - $entity_labels[] = $key; } // Prepare the autocomplete path.