diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php index 0d5dfd4..fee6a98 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php @@ -24,6 +24,7 @@ * settings = { * "size" = "60", * "autocomplete_path" = "taxonomy/autocomplete", + * "match" = "anywhere", * "placeholder" = "" * }, * multiple_values = TRUE @@ -35,12 +36,32 @@ class TaxonomyAutocompleteWidget extends WidgetBase { * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). */ public function settingsForm(array $form, array &$form_state) { + $element['size'] = array( + '#type' => 'number', + '#title' => t('Size of autocomplete textfield'), + '#default_value' => $this->getSetting('size'), + '#required' => TRUE, + '#min' => 1, + ); + $element['placeholder'] = array( '#type' => 'textfield', '#title' => t('Placeholder'), '#default_value' => $this->getSetting('placeholder'), '#description' => t('The placeholder is a short hint (a word or short phrase) intended to aid the user with data entry. A hint could be a sample value or a brief description of the expected format.'), ); + + $element['match'] = array( + '#type' => 'radios', + '#title' => t('Match'), + '#default_value' => $this->getSetting('match'), + '#options' => array( + 'anywhere' => t('Anywhere'), + 'start' => t('Starts with'), + ), + '#description' => t('Where the autocomplete matches values: either anywhere, or only at the beginning of a term.'), + ); + return $element; } diff --git a/core/modules/taxonomy/taxonomy.pages.inc b/core/modules/taxonomy/taxonomy.pages.inc index 4481e60..2752d05 100644 --- a/core/modules/taxonomy/taxonomy.pages.inc +++ b/core/modules/taxonomy/taxonomy.pages.inc @@ -140,11 +140,27 @@ function taxonomy_autocomplete($field_name) { if (!empty($tags_typed)) { $query->condition('t.name', $tags_typed, 'NOT IN'); } + + // @todo Get the match anywhere value from the field widget setting. + // $match = $field->getWidget->getSetting('match'); + // $match = drupal_container->get('something')->createInstance['widget']['settings']['match']; + $match = "anywhere"; + switch ($match) { + case 'start': + $condition_pattern = db_like($tag_last) . '%'; + break; + + case 'anywhere': + default: + $condition_pattern = '%' . db_like($tag_last) . '%'; + break; + } + // Select rows that match by term name. $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();