diff --git a/entityreference.module b/entityreference.module index 5f03c5b..c4ef879 100644 --- a/entityreference.module +++ b/entityreference.module @@ -693,6 +693,7 @@ function entityreference_field_widget_info() { // the two widgets, and the Field API doesn't update default // settings when the widget changes. 'path' => '', + 'show_identifier' => TRUE, ), ); @@ -707,6 +708,7 @@ function entityreference_field_widget_info() { // the two widgets, and the Field API doesn't update default // settings when the widget changes. 'path' => '', + 'show_identifier' => TRUE, ), 'behaviors' => array( 'multiple values' => FIELD_BEHAVIOR_CUSTOM, @@ -753,6 +755,13 @@ function entityreference_field_widget_settings_form($field, $instance) { '#element_validate' => array('_element_validate_integer_positive'), '#required' => TRUE, ); + + $form['show_identifier'] = array( + '#type' => 'checkbox', + '#title' => t('Show entity ID'), + '#description' => t('Show the entity ID next to the label e.g. "node-label (1)". Note that in case you use an autocomplete widget and have entities with a duplicate label, and this option is disabled, the first entity will be used.'), + '#default_value' => $settings['show_identifier'], + ); } return $form; @@ -819,9 +828,10 @@ function entityreference_field_widget_form(&$form, &$form_state, $field, $instan // Load those entities and loop through them to extract their labels. $entities = entity_load($field['settings']['target_type'], $entity_ids); + $show_identifier = $instance['widget']['settings']['show_identifier']; foreach ($entities as $entity_id => $entity_item) { $label = $handler->getLabel($entity_item); - $key = "$label ($entity_id)"; + $key = $show_identifier ? "$label ($entity_id)" : $label; // Labels containing commas or quotes must be wrapped in quotes. if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) { $key = '"' . str_replace('"', '""', $key) . '"'; @@ -885,8 +895,6 @@ function _entityreference_autocomplete_validate($element, &$form_state, $form) { else { // Try to get a match from the input string when the user didn't use the // autocomplete but filled in a value manually. - $field = field_info_field($element['#field_name']); - $handler = entityreference_get_selection_handler($field); $field_name = $element['#field_name']; $field = field_info_field($field_name); $instance = field_info_instance($element['#entity_type'], $field_name, $element['#bundle']); @@ -1033,16 +1041,15 @@ function entityreference_autocomplete_callback_get_matches($type, $field, $insta $entity_labels = $handler->getReferencableEntities($tag_last, $instance['widget']['settings']['match_operator'], 10); // Loop through the products and convert them into autocomplete output. - foreach ($entity_labels as $values) { - foreach ($values as $entity_id => $label) { - $key = "$label ($entity_id)"; - // Strip things like starting/trailing white spaces, line breaks and tags. - $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key))))); - // Names containing commas or quotes must be wrapped in quotes. - if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) { - $key = '"' . str_replace('"', '""', $key) . '"'; - } - $matches[$prefix . $key] = '
' . $label . '
'; + + $show_identifier = $instance['widget']['settings']['show_identifier']; + foreach ($entity_labels as $entity_id => $label) { + $key = $show_identifier ? "$label ($entity_id)" : $label; + // Strip things like starting/trailing white spaces, line breaks and tags. + $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key))))); + // Names containing commas or quotes must be wrapped in quotes. + if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) { + $key = '"' . str_replace('"', '""', $key) . '"'; } } } diff --git a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php index 35f06f5..d0ef689 100644 --- a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php +++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php @@ -213,24 +213,27 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select // Error if there are no entities available for a required field. form_error($element, t('There are no entities matching "%value"', array('%value' => $input))); } - elseif (count($entities) > 5) { - // Error if there are more than 5 matching entities. - form_error($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)"', array( - '%value' => $input, - '@value' => $input, - '@id' => key($entities), - ))); - } - elseif (count($entities) > 1) { - // More helpful error if there are only a few matching entities. - $multiples = array(); - foreach ($entities as $id => $name) { - $multiples[] = $name . ' (' . $id . ')'; + if ($this->instance['widget']['settings']['show_identifier']) { + if (count($entities) > 5) { + // Error if there are more than 5 matching entities. + form_error($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)"', array( + '%value' => $input, + '@value' => $input, + '@id' => key($entities), + ))); + } + elseif (count($entities) > 1) { + // More helpful error if there are only a few matching entities. + $multiples = array(); + foreach ($entities as $id => $name) { + $multiples[] = $name . ' (' . $id . ')'; + } + form_error($element, t('Multiple entities match this reference; "%multiple"', array('%multiple' => implode('", "', $multiples)))); } - form_error($element, t('Multiple entities match this reference; "%multiple"', array('%multiple' => implode('", "', $multiples)))); } else { - // Take the one and only matching entity. + // Take the one and only matching entity, or the first one in case + // "Show-identifier" is disabled. return key($entities); } }