You are here

function synonyms_autocomplete_format in Synonyms 7

Supportive function to format autocomplete suggestions.

Parameters

array $matches: Array of matched entries. It should follow this structure:

  • name: (string) String to be inserted into autocomplete textfield if user chooses this autocomplete entry
  • synonym: (string) If this entry is matched through a synonym, put that synonym here
  • behavior_implementation: (array) If this entry is matched through a synonym, put here the behavior implementation array that provided this match
  • bundle: (string) Bundle of the entity that is suggested in this entry

string $prefix: Any prefix to be appended to 'name' property of $matches array when inserting into the autocomplete textfield. Normally it is the already entered entries in the textfield

Return value

array Array of formatted autocomplete response entries ready to be returned to the autocomplete JavaScript

3 calls to synonyms_autocomplete_format()
synonyms_autocomplete_entity in ./synonyms.pages.inc
Page callback: Outputs JSON for entity autocomplete suggestions.
synonyms_autocomplete_taxonomy_term in ./synonyms.pages.inc
Page callback: Outputs JSON for taxonomy autocomplete suggestions.
synonyms_commerce_autocomplete in synonyms_commerce/synonyms_commerce.pages.inc
Menu page callback for synonyms commerce autocomplete widget.

File

./synonyms.pages.inc, line 360
Menu page callbacks of Synonyms module.

Code

function synonyms_autocomplete_format($matches, $prefix) {
  $output = array();
  $entity_info = array();
  foreach ($matches as $match) {
    $n = synonyms_autocomplete_escape($match['name']);
    while (isset($output[$prefix . $n])) {
      $n .= ' ';
    }
    $wording = check_plain($match['name']);
    if (isset($match['synonym'])) {
      if (!isset($entity_info[$match['behavior_implementation']['entity_type']])) {
        $entity_info[$match['behavior_implementation']['entity_type']] = entity_get_info($match['behavior_implementation']['entity_type']);
      }
      $wording = format_string(filter_xss_admin($match['behavior_implementation']['settings']['wording']), array(
        '@entity' => $match['name'],
        '@synonym' => $match['synonym'],
        '@field_name' => drupal_strtolower($match['behavior_implementation']['label']),
        '@bundle' => $entity_info[$match['behavior_implementation']['entity_type']]['bundles'][$match['bundle']]['label'],
      ));
    }
    $output[$prefix . $n] = $wording;
  }
  return $output;
}