modules/custom/lter_unit/lter_unit.module

Summary

Maintainability
Test Coverage
<?php

/**
 * @file
 * Integrates with the LTER Unit service API.
 *
 * http://unit.lternet.edu/unitregistry/
 */

/**
 * Implements hook_menu().
 */
function lter_unit_menu() {
  $items['lter-unit/autocomplete'] = array(
    'page callback' => 'lter_unit_get_unit_autocomplete',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Autocomplete callback for units.
 */
function lter_unit_get_unit_autocomplete($string = '') {
  $options = LterUnitHelper::getUnitOptions(0);
  $results = array();

  foreach ($options as $key => $label) {
    if (stripos($label, $string) !== FALSE) {
      $results[$key] = $label;
    }
  }

  $results = array_slice($results, 0, 10, TRUE);
  $results = array_map('check_plain', $results);

  drupal_json_output($results);
}