diff --git a/entityreference.module b/entityreference.module index bdcb562..53078f3 100644 --- a/entityreference.module +++ b/entityreference.module @@ -689,6 +689,7 @@ function entityreference_field_widget_info() { 'settings' => array( 'match_operator' => 'CONTAINS', 'size' => 60, + 'hide_ids' => FALSE, // We don't have a default here, because it's not the same between // the two widgets, and the Field API doesn't update default // settings when the widget changes. @@ -703,6 +704,7 @@ function entityreference_field_widget_info() { 'settings' => array( 'match_operator' => 'CONTAINS', 'size' => 60, + 'hide_ids' => FALSE, // We don't have a default here, because it's not the same between // the two widgets, and the Field API doesn't update default // settings when the widget changes. @@ -753,6 +755,12 @@ function entityreference_field_widget_settings_form($field, $instance) { '#element_validate' => array('_element_validate_integer_positive'), '#required' => TRUE, ); + $form['hide_ids'] = array( + '#type' => 'checkbox', + '#title' => t('Hide ids'), + '#description' => t('If your target entities have unique labels you may choose not to have the ids shown to the user. Note that this setting will make it impossible to reference entities with non-unique labels!'), + '#default_value' => $settings['hide_ids'], + ); } return $form; @@ -826,7 +834,7 @@ function entityreference_field_widget_form(&$form, &$form_state, $field, $instan foreach ($entities as $entity_id => $entity_item) { $label = $handler->getLabel($entity_item); - $key = "$label ($entity_id)"; + $key = $instance['widget']['settings']['hide_ids'] ? $label : "$label ($entity_id)"; // Labels containing commas or quotes must be wrapped in quotes. if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) { $key = '"' . str_replace('"', '""', $key) . '"'; @@ -884,8 +892,8 @@ function _entityreference_autocomplete_validate($element, &$form_state, $form) { $value = ''; if (!empty($element['#value'])) { // Take "label (entity id)', match the id from parenthesis. - if (preg_match("/.+\((\d+)\)/", $element['#value'], $matches)) { - $value = $matches[1]; + if (preg_match('/^(?:\s*|(.*) )?(\[\s*id\s*:\s*(\d+)\s*\]|\(\s*(\d+)\s*\))$/', rtrim($element['#value']), $matches)) { + $value = array_pop($matches); } else { // Try to get a match from the input string when the user didn't use the @@ -911,9 +919,9 @@ function _entityreference_autocomplete_tags_validate($element, &$form_state, $fo $value = array(); foreach ($entities as $entity) { // Take "label (entity id)', match the id from parenthesis. - if (preg_match("/.+\((\d+)\)/", $entity, $matches)) { + if (preg_match('/^(?:\s*|(.*) )?(\[\s*id\s*:\s*(\d+)\s*\]|\(\s*(\d+)\s*\))$/', rtrim($element['#value']), $matches)) { $value[] = array( - 'target_id' => $matches[1], + 'target_id' => array_pop($matches), ); } else { @@ -1054,14 +1062,14 @@ function entityreference_autocomplete_callback_get_matches($type, $field, $insta // 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)"; + $key = $instance['widget']['settings']['hide_ids'] ? $label : "$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 . '
'; + $matches[$prefix . $key] = '
' . $key . '
'; } } } diff --git a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php index 57a3a37..a228cc2 100644 --- a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php +++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php @@ -159,6 +159,13 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select $options = array(); $entity_type = $this->field['settings']['target_type']; + // Check if using hide_ids option and them remove the extra " when have an ',' or '"'. + if (isset($this->instance['widget']['settings']['hide_ids']) && $this->instance['widget']['settings']['hide_ids'] && $match_operator == '=') { + if (strpos($match, ',') !== FALSE || strpos($match, '"') !== FALSE) { + $match = substr($match, 1, -1); + } + } + $query = $this->buildEntityFieldQuery($match, $match_operator); if ($limit > 0) { $query->range(0, $limit); @@ -209,6 +216,7 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select */ public function validateAutocompleteInput($input, &$element, &$form_state, $form) { $entities = $this->getReferencableEntities($input, '=', 6); + $entities = empty($entities) ? $entities : $entities[key($entities)]; if (empty($entities)) { // Error if there are no entities available for a required field. form_error($element, t('There are no entities matching "%value"', array('%value' => $input)));