geocode($input, $options)) { list ($type, $item) = explode(':', $return); return $handler->get_result($type, $item); } } /** * Return a list of all handlers that might geocode something for you. */ function geocode_handler_info($field_type = NULL, $return_type = NULL) { static $handlers; if (!$handlers) { module_load_include('inc', 'geocode', 'includes/geocode'); $handlers = module_invoke_all('geocode_handler_info'); drupal_alter('geocode_handlers', $handlers); } $return = $handlers; if ($field_type) { foreach($return as $i => $handler) { if (!in_array($field_type, $handler['field types'])) { unset($return[$i]); } } } /* // TODO parse return types. if ($return_type) { list($group, $type) = explode(':', $return_type); foreach($return as $key => $items) { if ($key != if (!in_array($field_type, $handler['field types'])) { unset($return[$i]); } } } */ return $return; } /** * Implementation of our own hook_geocode_handler_info(). */ function geocode_geocode_handler_info() { $handlers = array(); // A default geocoding handler. $handlers['geocode_google'] = array( 'label' => t('Google API'), 'callback' => 'geocode_handler_google', 'module' => 'geocode', 'file' => 'geocode.inc', 'file path' => drupal_get_path('module', 'geocode') .'/includes', 'field types' => array('postal_field', 'postal', 'text'), 'element types' => array('postal', 'textfield', 'textarea'), 'return types' => array( 'geo' => array('point'), 'postal' => array('postal'), 'text' => array('country', 'city', 'state', 'zip'), ), ); // Based on availablilty, include handlers that leverage installed modules. // This is for some out-of-the-box interoperability, but should be supplanted // by modules implementing hook_geocode_hander_info() on their own. $dir = drupal_get_path('module', 'geocode') . '/includes/modules/'; foreach (file_scan_directory($dir, '\.inc$') as $file) { if (module_exists($module = $file->name)) { $func = $module . '_geocode_handler_info'; // Call hook_geocode_handler_info() if the module doesn't account for it. if (!function_exists($func) && !function_exists($func .'_alter')) { require $dir . $module .'.inc'; $handlers = array_merge($handlers, $func()); } } } return $handlers; } /** * Implementation of hook_gis_input_info(). */ function geocode_gis_input_info($gis_type = NULL) { $inputs = array(); foreach (geocode_handler_info() as $handler => $info) { // Only deal with handlers that attach to a form element. if (!isset($info['element types']) || !isset($info['return types']['geo'])) { continue; } foreach ($info['element types'] as $type) { $inputs[$handler .'_'. $type] = array( 'label' => $info['label'] .': '. $type, 'gis input' => 'wkt', // TODO this is for compatibility with GIS tools. Perhaps there's a way to define what we expect? 'gis types' => $info['return types']['geo'], 'gis callback' => 'geocode_convert', 'handler' => $handler, 'element' => array( '#type' => $type, ), ); } } return $inputs; } // TODO this should be deprecated completely. Currently proof of concept. function geocode_convert($input, $value, $return_format) { $handler = $input['handler']; return geocode($handler, $value, $return_format); }