You are here

function entity_translation_taxonomy_term_autocomplete in Entity Translation 7

Entity translation taxonomy autocomplete callback.

Parameters

string $langcode: The input language.

string $field_name: The name of the term reference field.

string $tags_typed: (optional) A comma-separated list of term names entered in the autocomplete form element. Only the last term is used for autocompletion. Defaults to an empty string.

See also

taxonomy_autocomplete()

1 string reference to 'entity_translation_taxonomy_term_autocomplete'
entity_translation_menu in ./entity_translation.module
Implements hook_menu().

File

./entity_translation.taxonomy.inc, line 296
The taxonomy specific translation functions and hook implementations.

Code

function entity_translation_taxonomy_term_autocomplete($langcode = NULL, $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();

  // Shift off the $langcode and $field_name arguments.
  array_shift($args);
  array_shift($args);
  $tags_typed = implode('/', $args);

  // Make sure the field exists and is a taxonomy field.
  if (!($field = field_info_field($field_name)) || $field['type'] !== 'taxonomy_term_reference') {

    // Error string. The JavaScript handler will realize this is not JSON and
    // will display it as debugging information.
    print t('Taxonomy field @field_name not found.', array(
      '@field_name' => $field_name,
    ));
    exit;
  }

  // The user enters a comma-separated list of tags. We only autocomplete the
  // last tag.
  $tags_typed = drupal_explode_tags($tags_typed);
  $tag_last = drupal_strtolower(array_pop($tags_typed));
  $term_matches = array();
  if ($tag_last != '') {
    if (!isset($langcode) || $langcode == LANGUAGE_NONE) {
      $langcode = $GLOBALS['language_content']->language;
    }

    // Part of the criteria for the query come from the field's own settings.
    $vocabulary = _entity_translation_taxonomy_reference_get_vocabulary($field);
    $entity_type = 'taxonomy_term';
    $query = new EntityFieldQuery();
    $query
      ->addTag('taxonomy_term_access');
    $query
      ->entityCondition('entity_type', $entity_type);

    // If the Title module is enabled and the taxonomy term name is replaced for
    // the current bundle, we can look for translated names, otherwise we fall
    // back to the regular name property.
    if (module_invoke('title', 'field_replacement_enabled', $entity_type, $vocabulary->machine_name, 'name')) {
      $name_field = 'name_field';
      $language_group = 0;

      // Do not select already entered terms.
      $column = 'value';
      if (!empty($tags_typed)) {
        $query
          ->fieldCondition($name_field, $column, $tags_typed, 'NOT IN', NULL, $language_group);
      }
      $query
        ->fieldCondition($name_field, $column, $tag_last, 'CONTAINS', NULL, $language_group);
      $query
        ->fieldLanguageCondition($name_field, array(
        $langcode,
        LANGUAGE_NONE,
      ), NULL, NULL, $language_group);
    }
    else {
      $name_field = 'name';

      // Do not select already entered terms.
      if (!empty($tags_typed)) {
        $query
          ->propertyCondition($name_field, $tags_typed, 'NOT IN');
      }
      $query
        ->propertyCondition($name_field, $tag_last, 'CONTAINS');
    }

    // Select rows that match by term name.
    $query
      ->propertyCondition('vid', $vocabulary->vid);
    $query
      ->range(0, 10);
    $result = $query
      ->execute();

    // Populate the results array.
    $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
    $terms = !empty($result[$entity_type]) ? taxonomy_term_load_multiple(array_keys($result[$entity_type])) : array();
    foreach ($terms as $tid => $term) {
      $name = _entity_translation_taxonomy_label($term, $langcode);
      $n = $name;

      // Term names containing commas or quotes must be wrapped in quotes.
      if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
        $n = '"' . str_replace('"', '""', $name) . '"';
      }
      $term_matches[$prefix . $n] = check_plain($name);
    }
  }
  drupal_json_output($term_matches);
}