Unless I'm missing something, the only autocomplete option we have for taxonomy terms is the Term reference: Autocomplete term widget (tagging). This widget allows to reference exisiting terms, but will also add non-existing terms to the vocabulary.

I think it would be very handy when one can disallow the creation of new terms through that autocomplete widget, so that only exisiting terms can be referenced.
Alternatively: This is already possible with Entity Reference (https://drupal.org/project/entityreference) or Content taxonomy (https://drupal.org/project/content_taxonomy) as mentioned in the issue comments.

Started a new issue, but I see this feature has been proposed before: http://drupal.org/node/526122#comment-1836544 .

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

j.stuyts’s picture

I also would love to see an option to disable creation of terms and forums completely. I do not want the users of my site to accidentally create new terms or forums when they enter an incorrect title in the field.

I don't see any widget type settings, so I guess it is not possible to add a checkbox to indicate whether creation of terms/forums is allowed or not. To solve it a new widget type probably has to be introduced: Term reference: Autocomplete term widget (tagging, no creation).

j.stuyts’s picture

One more thing: the reason I need to use an autocomplete field and cannot use a selection list, is that a selection list does not pick up new terms that are created during editing of the referencing node. This allows a more natural workflow for the user:

  • Create a new node that references one or more other terms.
  • When a term reference needs to be entered but the user forgot to create it beforehand, the user can create a new one in a new window/tab (using a link provided in the help text of the field) without having to abandon the current changes.
  • After creation of the new term, the user can enter part of its title in the term reference field and select it from the list of matching terms shown.
j.stuyts’s picture

Status: Active » Needs review
FileSize
4.53 KB

Here is a patch that adds a field setting to term reference fields that allows the user to disable autocreation of terms for unknown values.

j.stuyts’s picture

Changed encoding of patch to UTF-8 (was UTF-16).

j.stuyts’s picture

Sorry, uploaded UTF-16 version of file again. Here is the patch in UTF-8.

Steven Jones’s picture

Version: 7.0 » 8.x-dev
Status: Needs review » Needs work

Functionally this patch works, so thanks for contributing it, but:

  • Given that there are string changes, and arguably feature changes, I suspect that this will need to go into Drupal 8?
  • There are no tests, we should test to make sure we can still add new terms to fields when we want to, and we can't add new terms when we want to disallow it.
  • The error message set is quite brutal, and could do with being a bit more friendly, 'You can only select existing terms' or something along those lines maybe?
Steven Jones’s picture

Actually, although this is a lovely core patch, I wonder if this would be better as a contrib project?

Steven Jones’s picture

Status: Needs work » Closed (won't fix)

Actually the Content taxonomy project does the trick, and core can go about its merry business!

j.stuyts’s picture

Thanks for pointing us to the Content Taxonomy project. I will check it out.

agentrickard’s picture

Category: feature » bug
Status: Closed (won't fix) » Needs work

I would still like to see this behavior accounted for in Drupal 8 and I think it is properly called a bug in the module design. Yes, we can fix it in contrib, but it seems like an oversight that needs correcting.

Marking needs work due to the latest reviews and the recent /core change.

carsonblack’s picture

I would also like to see this in core. It is only a slight, but very useful, modification of the functionality of this type of widget that totally makes sense to be there (it makes sense that if you're going to enable some functionality that you should be able to disallow it as well). Permissions could be the solution to that, but there is an exception, read on.
Also, when using this widget with a hierarchical taxonomy, something you can do using core, it really doesn't make sense to allow the use of this widget for adding new terms to the taxonomy because they would not be able to stick the term in the right spot/level in the taxonomy, but it is a very nice way to select terms from that taxonomy. But I still want to give that same user the ability to edit that hierarchical taxonomy via the taxonomy interface. #2 kind of points this out as well. There are also use cases where the user that is entering content doesn't really care about the hierarchy of the taxonomy while they are entering a new piece of content, but the site builder is relying on the integrity of the hierarchy to be maintained. Offering some very long list of hierarchical taxonomy list in a select as an alternative to this is really cumbersome UX.
It seems heavy handed and overkill to have to rely on a contrib module for a such a simple little additional checkbox that could go a long way to helping core content management functionality and usability.

carsonblack’s picture

Question: so the patch in #5 is for D7 or D8? I would like to test it.

agentrickard’s picture

The patch is against D7 and needs a re-roll against D8.

amitaibu’s picture

"Auto create" is a checkbox in #1801304: Add Entity reference field, which will deprecate taxonomy term field.

Frank Ralf’s picture

Thanks for the pointer - and the work!

rp7’s picture

Been almost 2 years now - any chance this can finally get in?

Patch in #5 works.

ThePickwickProject’s picture

FileSize
24.71 KB

That would be really nice.

fyi - We're currently migrating a D6 site and apparently this feature existed in the past (see attachment).

agentrickard’s picture

I think this may already be in D8. See this code in \Drupal\taxonomy\Plugin\entity_reference\selection\TermSelection:

  /**
   * Overrides SelectionBase::settingsForm().
   */
  public static function settingsForm(&$field, &$instance) {
    $form = parent::settingsForm($field, $instance);

    // @todo: Currently allow auto-create only on taxonomy terms.
    $form['auto_create'] = array(
      '#type' => 'checkbox',
      '#title' => t("Create referenced entities if they don't already exist"),
      '#default_value' => $instance['settings']['handler_settings']['auto_create'],
    );
    return $form;

  }

And this in entity_reference_field_presave():

  foreach ($items as $delta => $item) {
    if ($item['target_id'] == 'auto_create') {
      $bundle_key = $entity_info['entity_keys']['bundle'];
      $label_key = $entity_info['entity_keys']['label'];
      $values = array(
        $label_key => $item['label'],
        $bundle_key => $bundle,
        // @todo: Use wrapper to get the user if exists or needed.
        'uid' => isset($entity->uid) ? $entity->uid : $user->uid,
      );
      $target_entity = entity_create($target_type, $values);
      $target_entity->save();
      $items[$delta]['target_id'] = $target_entity->id();
    }
  }

Can someone track down when this was committed? If so this is a duplicate now and just needs a backport to D7.

TravisCarden’s picture

Version: 8.x-dev » 7.x-dev

Re: #18. This was added in #1801304: Add Entity reference field. It came in the form of a whole module, so a straight backport to D7 won't be possible. It would be nice to have an intermediate solution in D7, but we can always just use the Entity reference module in contrib, too. (Just use its entity reference field in place of core's term reference field.)

Cyclodex’s picture

Status: Needs work » Postponed (maintainer needs more info)

This issue is getting older...

I updated the issue summary but I am not sure what the task is here now.
We have several stuff in here, modules which would fix it already like:
https://drupal.org/project/content_taxonomy
https://drupal.org/project/entityreference

and D8 has it also already in there with Entity Reference

can we close this or do you want to provide a proper patch for D7 to fix it? (see comment 6 https://drupal.org/node/1078878#comment-4292112)

Cyclodex’s picture

Issue summary: View changes

updating issue summary - its possible with entity reference

Cyclodex’s picture

Issue summary: View changes

adding content_taxonomy as possible alternative

pratip.ghosh’s picture

Issue summary: View changes

Does anyone plan to release a patch for the latest version of Drupal? Cuz this one is giving error while patching...

pratip.ghosh’s picture

sibany’s picture

FileSize
17.4 KB

Hey pratip.ghosh,

am using drupal 7.28

its good , patch OK and work perfect!

anyway i'have attched the taxonomy.module file in zip ,

just replace it with the one you have and hit the update button after you put site on maintenance mode ;)

"Make Back-up of the tox.. file first"

davidwhthomas’s picture

Here's another way of limiting creation of new terms in a taxonomy_autocomplete field widget.
This one doesn't require a core patch and uses a custom module instead.

<?php
/**
 * Implements hook_form_WIDGET_TYPE_alter
 */
function MODULE_field_widget_taxonomy_autocomplete_form_alter(&$form, &$form_state){
  // Add our validate handler
  $form['#element_validate'][] = 'MODULE_taxonomy_autocomplete_validate';
}

/**
 * Form element validate handler for taxonomy term autocomplete element.
 * 
 * This is a clone of taxonomy_autocomplete_validate
 * It adds a single additional additional access check
 * prior to creating a new taxonomy term.
 */
function MODULE_taxonomy_autocomplete_validate($element, &$form_state, $form){
  // Autocomplete widgets do not send their tids in the form, so we must detect
  // them here and process them independently.
  $value = array();
  if ($tags = $element['#value']) {
    // Collect candidate vocabularies.
    $field = field_widget_field($element, $form_state);
    $vocabularies = array();
    foreach ($field['settings']['allowed_values'] as $tree) {
      if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
        $vocabularies[$vocabulary->vid] = $vocabulary;
      }
    }

    // Translate term names into actual terms.
    $typed_terms = drupal_explode_tags($tags);
    foreach ($typed_terms as $typed_term) {
      // See if the term exists in the chosen vocabulary and return the tid;
      // otherwise, create a new 'autocreate' term for insert/update.
      if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => array_keys($vocabularies)))) {
        $term = array_pop($possibilities);
      }
      // Only allow create term access to users with the 'administer taxonomy' permission.
      else if (user_access('administer taxonomy')) {
        $vocabulary = reset($vocabularies);
        $term = array(
          'tid' => 'autocreate',
          'vid' => $vocabulary->vid,
          'name' => $typed_term,
          'vocabulary_machine_name' => $vocabulary->machine_name,
        );
      }
      else {
        $term = NULL;
        // Display error message to user entering a non-existent term.
        form_error($element, t('You have entered an invalid value %value for %name. Please select one of the available terms.', array('%name' => t($element['#title']), '%value' => $typed_term)));
      }
      if (isset($term)) {
        $value[] = (array)$term;
      }
    }
  }

  form_set_value($element, $value, $form_state);
}
?>
mark_fullmer’s picture

Thanks for that solution, davidwhthomas! Building off your code, which defines the ability to add via autocomplete globally to all taxonomies, via the "administer taxonomy", I've written a module that defines individual permissions per existing taxonomy:

https://www.drupal.org/sandbox/markfullmer/2492455

michel.settembrino’s picture