diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index e147c1c..17caac1 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -1470,6 +1470,7 @@ function taxonomy_field_widget_info() { 'label' => t('Autocomplete term widget (tagging)'), 'field types' => array('taxonomy_term_reference'), 'settings' => array( + 'match_operator' => 'CONTAINS', 'size' => 60, 'autocomplete_path' => 'taxonomy/autocomplete', ), @@ -1738,7 +1739,7 @@ function taxonomy_field_widget_form(&$form, &$form_state, $field, $instance, $la $element += array( '#type' => 'textfield', '#default_value' => taxonomy_implode_tags($tags), - '#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'] . '/' . $field['field_name'], + '#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'] . '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'], '#size' => $instance['widget']['settings']['size'], '#maxlength' => 1024, '#element_validate' => array('taxonomy_autocomplete_validate'), @@ -1748,6 +1749,36 @@ function taxonomy_field_widget_form(&$form, &$form_state, $field, $instance, $la } /** + * Implements hook_field_widget_settings_form(). + */ +function taxonomy_field_widget_settings_form($field, $instance) { + $widget = $instance['widget']; + $settings = $widget['settings']; + + if ($widget['type'] == 'taxonomy_autocomplete') { + $form['match_operator'] = array( + '#type' => 'radios', + '#title' => t('Autocomplete matching'), + '#default_value' => $settings['match_operator'], + '#options' => array( + 'STARTS_WITH' => t('Starts with'), + 'CONTAINS' => t('Contains'), + ), + '#description' => t('Select the method used to collect autocomplete suggestions. Note that Contains can cause performance issues on sites with thousands of entities.'), + ); + $form['size'] = array( + '#type' => 'textfield', + '#title' => t('Size of textfield'), + '#default_value' => $settings['size'], + '#required' => TRUE, + '#element_validate' => array('element_validate_integer_positive'), + ); + } + + return $form; +} + +/** * Form element validate handler for taxonomy term autocomplete element. */ function taxonomy_autocomplete_validate($element, &$form_state) { diff --git a/modules/taxonomy/taxonomy.pages.inc b/modules/taxonomy/taxonomy.pages.inc index 975ff12..720b905 100644 --- a/modules/taxonomy/taxonomy.pages.inc +++ b/modules/taxonomy/taxonomy.pages.inc @@ -122,6 +122,13 @@ function taxonomy_autocomplete($field_name = '', $tags_typed = '') { // If the request has a '/' in the search text, then the menu system will have // split it into multiple arguments, recover the intended $tags_typed. $args = func_get_args(); + if (count($args) > 2) { + $entity_type = $args[1]; + $bundle = $args[2]; + array_shift($args); + array_shift($args); + } + // Shift off the $field_name argument. array_shift($args); $tags_typed = implode('/', $args); @@ -148,6 +155,24 @@ function taxonomy_autocomplete($field_name = '', $tags_typed = '') { $vids[] = $vocabularies[$tree['vocabulary']]->vid; } + if (!empty($entity_type)) { + $instance = field_info_instance($entity_type, $field_name, $bundle); + $match_operator = $instance['widget']['settings']['match_operator']; + } + + switch ($match_operator) { + case 'STARTS_WITH': + $condition_pattern = db_like($tag_last) . '%'; + break; + + case 'CONTAINS': + $condition_pattern = '%' . db_like($tag_last) . '%'; + break; + + default: + $condition_pattern = '%' . db_like($tag_last) . '%'; + } + $query = db_select('taxonomy_term_data', 't'); $query->addTag('translatable'); $query->addTag('term_access'); @@ -160,7 +185,7 @@ function taxonomy_autocomplete($field_name = '', $tags_typed = '') { $tags_return = $query ->fields('t', array('tid', 'name')) ->condition('t.vid', $vids) - ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE') + ->condition('t.name', $condition_pattern, 'LIKE') ->range(0, 10) ->execute() ->fetchAllKeyed();