1

I would like to unset terms that show up in taxonomy autocomplete.

For example if TID 1 2 3 4, are in the taxonomy I would only like 1 2 and 3 to show in the dropdown for autocomplete.

1
+100

You could try to alter the query but according to a comment there is no alter tag set on the query

- https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_query_alter/7.x - https://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.pages.inc/function/taxonomy_autocomplete/7.x

Another solution would be to alter the form with the autocomplete field to alter the path

iirc as a last resort you could use javascript to drop the unwanted item from the list using css display:none

2

Your best bet would be to create your own autocomplete path. Here are the steps to implement this:

Create a custom module

In hook menu create a custom autocomplete path:

$items['my-module-taxonomy/autocomplete'] = array(
  'title' => 'Custom Autocomplete taxonomy',
  'page callback' => 'my_module_taxonomy_autocomplete',
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK,
);

Create my_module_taxonomy_autocomplete function. The easy way would be to copy everything from the taxonomy_autocomplete() function and add conditions in your query. For example:

->condition('t.tid', array(1,2,3), 'IN')

In hook_form_alter() set the autocomplete field from using the default one to the custom one you just made:

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'my_node') {
    $form['field_term'][LANGUAGE_NONE]['#autocomplete_path'] = 'my-module-taxonomy/autocomplete';
  }
}
1

Overview

Use an Entity Reference field to reference taxonomy terms filtered by an entity reference view.

Method

  1. Create a View showing "Taxonomy terms" of type "vocabulary name"
  2. Add a Views display of type "Entity Reference"
  3. Under "Format" next to "Entity Reference list" click "Settings" and select relevant "Search fields"
  4. Add any filters under "Filter criteria", such as "Taxonomy term: Term ID"
  5. For "Operator" in step 4, select "Is not equal to" or "Is less than" for example. See below.
  6. Save the view.
  7. Add an "Entity reference" field to your entity with "Target type" of "Taxonomy term" and select your "Target bundles"
  8. For "Mode" under "Entity selection", select "Views: filter by an entity reference view"
  9. Select the "View used to select the entities"
  10. Provide any arguments if required and review other field options.
  11. Save the field settings.

Example

In your example, for step 5 you could add a single filter for "Taxonomy term: Term ID":

Operator: Is less than

Value: 4

You can add as many filters as you like and even perform calculations if required.

More filter options are available via custom code or contributed modules such as Code Filter which allows the use of PHP. See also Views PHP and Global Filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.