You are here

autocomplete_deluxe.module in Autocomplete Deluxe 7.2

Define enhanced autocomplete wdiget.

File

autocomplete_deluxe.module
View source
<?php

/**
 * @file
 * Define enhanced autocomplete wdiget.
 */

/**
 * Implements hook_field_widget_info().
 */
function autocomplete_deluxe_field_widget_info() {
  return array(
    'autocomplete_deluxe_taxonomy' => array(
      'label' => t('Autocomplete Deluxe'),
      'field types' => array(
        'taxonomy_term_reference',
      ),
      'settings' => array(
        'size' => 60,
        'autocomplete_deluxe_path' => 'autocomplete_deluxe/taxonomy',
      ),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
      ),
    ),
  );
}

/**
 * Custom taxonomy callback, which also accepts an empty string search.
 */
function autocomplete_deluxe_taxonomy_callback($field_name, $tags_typed = '', $limit = 10) {
  $field = field_info_field($field_name);
  $use_synonyms = !empty($_GET['synonyms']);

  // The user enters a comma-separated list of tags. We only autocomplete the
  // last tag.
  $tags_typed = drupal_explode_tags(urldecode($tags_typed));
  $tag_last = drupal_strtolower(array_pop($tags_typed));

  // Initiate tag collections.
  $tags_return = $beginning_tags = $middle_tags = array();

  // Part of the criteria for the query come from the field's own settings.
  $vids = array();
  $vocabularies = taxonomy_vocabulary_get_names();
  foreach ($field['settings']['allowed_values'] as $tree) {

    // If the content taxonomy setting content_taxonomy_ignore_in_suggestions
    // is set, then the vocabulary is ignored.
    if (empty($tree['content_taxonomy_ignore_in_suggestions'])) {
      $vids[] = $vocabularies[$tree['vocabulary']]->vid;
    }
  }
  $query = db_select('taxonomy_term_data', 't');
  $query
    ->addTag('translatable');
  $query
    ->addTag('term_access');
  if (module_exists('synonyms') && !empty($use_synonyms)) {
    $query
      ->leftJoin('field_data_synonyms_synonym', 'fdss', 'fdss.entity_id = t.tid');
  }
  if ($tag_last != '') {

    // Do not select already entered terms.
    if (!empty($tags_typed)) {
      $query
        ->condition('t.name', $tags_typed, 'NOT IN');
    }

    // Select rows that match by term name.
    $query
      ->fields('t', array(
      'tid',
      'name',
    ))
      ->condition('t.vid', $vids);

    // Clone query, because we need the current state for the "middle" query.
    $middle_query = clone $query;

    // Apply general limit.
    if (isset($limit) && $limit > 0) {
      $query
        ->range(0, $limit);
    }

    // Check for terms beginning with the given term first.
    if (module_exists('synonyms') && !empty($use_synonyms)) {
      $or = db_or();
      $or
        ->condition('fdss.synonyms_synonym_value', db_like($tag_last) . '%', 'LIKE');
      $or
        ->condition('t.name', db_like($tag_last) . '%', 'LIKE');
      $query
        ->condition($or);
    }
    else {
      $query
        ->condition('t.name', db_like($tag_last) . '%', 'LIKE');
    }
    $beginning_tags = $query
      ->execute()
      ->fetchAllKeyed();

    // Check for terms having the term somewhere in it second.
    // Apply new limit, which is the general one minus the results of the
    // beginning query.
    $middle_limit = $limit - count($beginning_tags);

    // Only proceed, if we really need more results.
    if ($middle_limit > 0) {

      // Apply limit.
      $middle_query
        ->range(0, $middle_limit);

      // Make sure we are not getting the same results as for the query above.
      if (!empty($beginning_tags)) {
        $beginning_term_ids = array_keys($beginning_tags);
        $middle_query
          ->condition('t.tid', $beginning_term_ids, 'NOT IN');
      }
      if (module_exists('synonyms') && !empty($use_synonyms)) {
        $or = db_or();
        $or
          ->condition('fdss.synonyms_synonym_value', '%' . db_like($tag_last) . '%', 'LIKE');
        $or
          ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE');
        $middle_query
          ->condition($or);
      }
      else {
        $middle_query
          ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE');
      }
      $middle_tags = $middle_query
        ->execute()
        ->fetchAllKeyed();
    }

    // Build tags.
    $tags_return = $beginning_tags + $middle_tags;
  }
  else {
    $query
      ->fields('t', array(
      'tid',
      'name',
    ))
      ->condition('t.vid', $vids);
    if (isset($limit) && $limit > 0) {
      $query
        ->range(0, $limit);
    }
    $tags_return = $query
      ->execute()
      ->fetchAllKeyed();
  }
  $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
  $term_matches = array();
  foreach ($tags_return as $tid => $name) {
    $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);
}

/**
 * Returns all allowed terms for a field without any prefix.
 */
function autocomplete_deluxe_allowed_terms($field) {
  $options = array();
  foreach ($field['settings']['allowed_values'] as $tree) {
    if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
      if ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'])) {
        foreach ($terms as $term) {
          $options[$term->name] = $term->name;
        }
      }
    }
  }
  return $options;
}

/**
 * Implements hook_field_widget_settings_form().
 */
function autocomplete_deluxe_field_widget_settings_form($field, $instance) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];
  $form['size'] = array(
    '#type' => 'textfield',
    '#title' => t('Size of textfield'),
    '#default_value' => isset($settings['size']) ? $settings['size'] : 6,
    '#element_validate' => array(
      '_element_validate_integer_positive',
    ),
    '#required' => TRUE,
  );
  $form['limit'] = array(
    '#type' => 'textfield',
    '#title' => t('Limit of the output.'),
    '#description' => t('If set to zero no limit will be used'),
    '#default_value' => isset($settings['limit']) ? $settings['limit'] : 10,
    '#element_validate' => array(
      '_element_validate_integer',
    ),
  );
  $form['min_length'] = array(
    '#type' => 'textfield',
    '#title' => t('Minimum length.'),
    '#description' => t('The minimum length of characters to enter to open the suggestion list.'),
    '#default_value' => isset($settings['min_length']) ? $settings['min_length'] : 0,
    '#element_validate' => array(
      '_element_validate_integer',
    ),
  );
  $form['delimiter'] = array(
    '#type' => 'textfield',
    '#title' => t('Delimiter.'),
    '#description' => t('A character which should be used beside the enter key, to seperate terms.'),
    '#default_value' => isset($settings['delimiter']) ? $settings['delimiter'] : '',
    '#size' => 1,
  );
  $form['not_found_message'] = array(
    '#type' => 'textfield',
    '#title' => t('Term not found message.'),
    '#description' => t('A message text which will be displayed, if the entered term was not found.'),
    '#default_value' => isset($settings['not_found_message']) ? t($settings['not_found_message']) : t("The term '@term' will be added."),
  );
  $form['show_link'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show manage terms link'),
    '#description' => t('Should users be able to have direct access to manage terms for vocabulary of this field?'),
    '#default_value' => isset($settings['show_link']) ? $settings['show_link'] : '',
  );
  if (module_exists('synonyms')) {
    $form['use_synonyms'] = array(
      '#type' => 'checkbox',
      '#title' => t('Allow synonyms'),
      '#description' => t('Should users be able to search for synonyms of terms?'),
      '#default_value' => isset($settings['use_synonyms']) ? $settings['use_synonyms'] : FALSE,
    );
  }
  return $form;
}

/**
 * Implodes the tags from the taxonomy module.
 *
 * This function is essentially the same as taxonomy_implode_tags, with the
 * difference, that it uses only a comma instead of a comma and a space to
 * implode the tags. It will help keep problems with delimiters to a minimum.
 */
function autocomplete_deluxe_taxonomy_implode_tags($tags, $vid = NULL) {
  $typed_tags = array();
  foreach ($tags as $tag) {

    // Extract terms belonging to the vocabulary in question.
    if (!isset($vid) || $tag->vid == $vid) {

      // Make sure we have a completed loaded taxonomy term.
      if (isset($tag->name)) {

        // Commas and quotes in tag names are special cases, so encode 'em.
        if (strpos($tag->name, ',') !== FALSE || strpos($tag->name, '"') !== FALSE) {
          $tag->name = '"' . str_replace('"', '""', $tag->name) . '"';
        }
        $typed_tags[] = $tag->name;
      }
    }
  }
  return implode(',', $typed_tags);
}

/**
 * Implements hook_field_widget_error().
 */
function autocomplete_deluxe_field_widget_error($element, $error, $form, &$form_state) {
  form_error($element, $error['message']);
}

/**
 * Implements hook_field_widget_form().
 */
function autocomplete_deluxe_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  global $base_url;
  if (isset($instance['widget']['settings']['show_link']) && !empty($instance['widget']['settings']['show_link']) && user_access('administer taxonomy')) {
    $vocabulary = $field['settings']['allowed_values'][0]['vocabulary'];
    $suffix = '<a target="_blank" href="' . $base_url . '/admin/structure/taxonomy/' . $vocabulary . '">' . t('Manage terms') . '</a>';
  }
  else {
    $suffix = '';
  }
  $element += array(
    '#type' => 'autocomplete_deluxe',
    '#size' => $instance['widget']['settings']['size'],
    '#limit' => isset($instance['widget']['settings']['limit']) ? $instance['widget']['settings']['limit'] : 10,
    '#min_length' => isset($instance['widget']['settings']['min_length']) ? $instance['widget']['settings']['min_length'] : 0,
    '#use_synonyms' => isset($instance['widget']['settings']['use_synonyms']) ? $instance['widget']['settings']['use_synonyms'] : 0,
    '#delimiter' => isset($instance['widget']['settings']['delimiter']) ? $instance['widget']['settings']['delimiter'] : '',
    '#not_found_message' => isset($instance['widget']['settings']['not_found_message']) ? $instance['widget']['settings']['not_found_message'] : t("The term '@term' will be added."),
    '#suffix' => $suffix,
  );
  $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED ? TRUE : FALSE;
  $tags = array();
  foreach ($items as $item) {
    $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
  }
  $element['#element_validate'] = array(
    'taxonomy_autocomplete_validate',
  );
  $element += array(
    '#multiple' => $multiple,
    '#autocomplete_deluxe_path' => url($instance['widget']['settings']['autocomplete_deluxe_path'] . '/' . $field['field_name'], array(
      'absolute' => TRUE,
    )),
    '#default_value' => autocomplete_deluxe_taxonomy_implode_tags($tags),
  );
  return $element;
}

/**
 * Generates the basic form elements and javascript settings.
 */
function autocomplete_deluxe_element_process($element) {
  $element['#attached'] = array(
    'library' => array(
      array(
        'system',
        'ui.autocomplete',
      ),
      array(
        'system',
        'ui.button',
      ),
      array(
        'system',
        'ui.sortable',
      ),
    ),
    'js' => array(
      drupal_get_path('module', 'autocomplete_deluxe') . '/autocomplete_deluxe.js',
    ),
    'css' => array(
      drupal_get_path('module', 'autocomplete_deluxe') . '/autocomplete_deluxe.css',
    ),
  );

  // Workaround for problems with jquery css in seven theme.
  if ($GLOBALS['theme'] == 'seven') {
    $element['#attached']['css'][] = drupal_get_path('module', 'autocomplete_deluxe') . '/autocomplete_deluxe.seven.css';
  }
  $html_id = drupal_html_id('autocomplete-deluxe-input');
  $element['#after_build'][] = 'autocomplete_deluxe_after_build';

  // Set default options for multiple values.
  $element['#multiple'] = isset($element['#multiple']) ? $element['#multiple'] : FALSE;
  $element['textfield'] = array(
    '#type' => 'textfield',
    '#size' => isset($element['#size']) ? $element['#size'] : '',
    '#attributes' => array_merge_recursive($element['#attributes'], array(
      'class' => array(
        'autocomplete-deluxe-form',
      ),
      'id' => array(
        $html_id,
      ),
      'aria-label' => t('Autocomplete'),
    )),
    '#default_value' => '',
    '#prefix' => '<div class="autocomplete-deluxe-container">',
    '#suffix' => '</div>',
  );
  $js_settings['autocomplete_deluxe'][$html_id] = array(
    'input_id' => $html_id,
    'multiple' => $element['#multiple'],
    'required' => $element['#required'],
    'limit' => isset($element['#limit']) ? $element['#limit'] : 10,
    'min_length' => isset($element['#min_length']) ? $element['#min_length'] : 0,
    'use_synonyms' => isset($element['#use_synonyms']) ? $element['#use_synonyms'] : 0,
    'delimiter' => isset($element['#delimiter']) ? $element['#delimiter'] : '',
    'not_found_message' => isset($element['#not_found_message']) ? $element['#not_found_message'] : t("The term '@term' will be added."),
  );
  if (isset($element['#autocomplete_deluxe_path'])) {
    if (isset($element['#default_value'])) {

      // Split on the comma only if that comma has zero, or an even number of
      // quotes in ahead of it.
      // http://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes
      $default_value = preg_replace('/,(?=([^\\"]*\\"[^\\"]*\\")*[^\\"]*$)/i', '"" ""', $element['#default_value']);
      $default_value = '""' . $default_value . '""';
    }
    else {
      $default_value = '';
    }
    if ($element['#multiple']) {
      $element['value_field'] = array(
        '#type' => 'textfield',
        '#attributes' => array(
          'class' => array(
            'autocomplete-deluxe-value-field',
          ),
          'aria-label' => t('Autocomplete Value'),
        ),
        '#default_value' => $default_value,
        '#prefix' => '<div class="autocomplete-deluxe-value-container">',
        '#suffix' => '</div>',
        '#required' => isset($element['#required']) ? $element['#required'] : FALSE,
        '#element_validate' => array(
          'autocomplete_deluxe_validate_multiple',
        ),
        '#parent_element' => &$element,
      );
      $element['textfield']['#attributes']['style'] = array(
        'display:none',
      );
    }
    else {
      $element['textfield']['#default_value'] = isset($element['#default_value']) ? $element['#default_value'] : '';
    }
    $js_settings['autocomplete_deluxe'][$html_id] += array(
      'type' => 'ajax',
      'uri' => $element['#autocomplete_deluxe_path'],
    );
  }
  else {

    // If there is no source (path or data), we don't want to add the js
    // settings and so the functions will be abborted.
    return $element;
  }
  $element['#attached']['js'][] = array(
    'data' => $js_settings,
    'type' => 'setting',
  );
  $element['#tree'] = TRUE;
  return $element;
}

/**
 * Validation callback for "multiple" mode.
 */
function autocomplete_deluxe_validate_multiple($element, &$form_state) {
  if (isset($element['#required']) && $element['#required']) {
    $tags = autocomplete_deluxe_extract_tags($element['#value']);
    if (!$tags) {
      form_error($element['#parent_element'], t('!name field is required.', array(
        '!name' => $element['#parent_element']['#title'],
      )));
    }
  }
}

/**
 * Extracts tags from string which is a result of tags implosion.
 */
function autocomplete_deluxe_extract_tags($str) {
  preg_match_all('/""([^"]*)""/', $str, $matches);
  $tags = $matches[1];
  foreach ($tags as $key => &$tag) {
    $tag = trim($tag);
    if (!$tag) {
      unset($tags[$key]);
    }
  }
  return empty($tags) ? NULL : $tags;
}

/**
 * Helper function-determines the value for an autocomplete deluxe form element.
 */
function autocomplete_deluxe_value(&$element, $input = FALSE, $form_state = NULL) {

  // This runs before child elements are processed, so we cannot calculate the
  // value here. But we have to make sure the value is an array, so the form
  // API is able to process the children to set their values in the array. Thus
  // once the form API has finished processing the element, the value is an
  // array containing the child element values. Then finally the after build
  // callback converts it back to the numeric value and sets that.
  return array();
}

/**
 * FAPI after build callback for the duration parameter type form.
 *
 * Fixes up the form value by applying the multiplier.
 */
function autocomplete_deluxe_after_build($element, &$form_state) {

  // By default drupal sets the maxlength to 128 if the property isn't
  // specified, but since the limit isn't useful in some cases,
  // we unset the property.
  unset($element['textfield']['#maxlength']);

  // Set the elements value from either the value field or text field input.
  $element['#value'] = isset($element['value_field']) ? $element['value_field']['#value'] : $element['textfield']['#value'];
  if (isset($element['value_field'])) {

    // Remove any empty double double quoted strings that throw
    // off downstream str_replace functions, then trim whitespace.
    $element['#value'] = str_replace('""""', '', $element['#value']);
    $element['#value'] = trim($element['#value']);

    // Replace all cases of double double quotes and one or more spaces with a
    // comma. This will allow us to keep entries in double quotes.
    $element['#value'] = preg_replace('/"" +""/', '", "', $element['#value']);

    // Remove the double quotes at the beginning and the end from the first and
    // the last term.
    $element['#value'] = substr($element['#value'], 1, strlen($element['#value']) - 2);

    // One final check to turn all double double quotes into single double
    // quotes. Necessary when re-saving existing values.
    $element['#value'] = str_replace('""', '"', $element['#value']);
    unset($element['value_field']['#maxlength']);
  }
  elseif ($form_state['process_input']) {
    $element['#value'] = '"' . $element['#value'] . '"';
  }
  form_set_value($element, $element['#value'], $form_state);
  return $element;
}

/**
 * Implements hook_element_info().
 */
function autocomplete_deluxe_element_info() {
  $types['autocomplete_deluxe'] = array(
    '#input' => TRUE,
    '#value_callback' => 'autocomplete_deluxe_value',
    '#pre_render' => array(
      'form_pre_render_conditional_form_element',
    ),
    '#process' => array(
      'autocomplete_deluxe_element_process',
    ),
  );
  return $types;
}

/**
 * Implements hook_menu().
 */
function autocomplete_deluxe_menu() {
  $items['autocomplete_deluxe/taxonomy'] = array(
    'title' => 'Autocomplete deluxe taxonomy',
    'page callback' => 'autocomplete_deluxe_taxonomy_callback',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_help().
 */
function autocomplete_deluxe_help($path, $arg) {
  switch ($path) {
    case 'admin/help#autocomplete_deluxe':
      $filepath = dirname(__FILE__) . '/README.md';
      if (file_exists($filepath)) {
        $readme = file_get_contents($filepath);
      }
      else {
        $filepath = dirname(__FILE__) . '/README.txt';
        if (file_exists($filepath)) {
          $readme = file_get_contents($filepath);
        }
      }
      if (!isset($readme)) {
        return NULL;
      }
      if (module_exists('markdown')) {
        $filters = module_invoke('markdown', 'filter_info');
        $info = $filters['filter_markdown'];
        if (function_exists($info['process callback'])) {
          $output = $info['process callback']($readme, NULL);
        }
        else {
          $output = '<pre>' . $readme . '</pre>';
        }
      }
      else {
        $output = '<pre>' . $readme . '</pre>';
      }
      return $output;
  }
}

Functions

Namesort descending Description
autocomplete_deluxe_after_build FAPI after build callback for the duration parameter type form.
autocomplete_deluxe_allowed_terms Returns all allowed terms for a field without any prefix.
autocomplete_deluxe_element_info Implements hook_element_info().
autocomplete_deluxe_element_process Generates the basic form elements and javascript settings.
autocomplete_deluxe_extract_tags Extracts tags from string which is a result of tags implosion.
autocomplete_deluxe_field_widget_error Implements hook_field_widget_error().
autocomplete_deluxe_field_widget_form Implements hook_field_widget_form().
autocomplete_deluxe_field_widget_info Implements hook_field_widget_info().
autocomplete_deluxe_field_widget_settings_form Implements hook_field_widget_settings_form().
autocomplete_deluxe_help Implements hook_help().
autocomplete_deluxe_menu Implements hook_menu().
autocomplete_deluxe_taxonomy_callback Custom taxonomy callback, which also accepts an empty string search.
autocomplete_deluxe_taxonomy_implode_tags Implodes the tags from the taxonomy module.
autocomplete_deluxe_validate_multiple Validation callback for "multiple" mode.
autocomplete_deluxe_value Helper function-determines the value for an autocomplete deluxe form element.