Creating Geo Widget

This page will describes how to make a Geo field widget module, by using the OpenLayers CCK module as an example.

Step 01: Create theme implementation

This will be used for the element we will create later.

<?php
/**
* Implementation of hook_theme().
*/
function openlayers_cck_theme() {
  return array(
   
'openlayers_cck_geo_field' => array(
     
'arguments' => array(
       
'element' => NULL
     
),
    ),
  );
}
?>

Step 02: Implement GIS Input Info

This tells Geo about what kind of geo input and outputs will be available for the widget. Geo will build a widget based on this information.

<?php
/**
* Implementation of hook_gis_input_info().
*/
function openlayers_cck_gis_input_info($gis_type = NULL) {
 
$inputs = array(
   
'openlayers_cck_geo_field' => array(
     
'label' => t('OpenLayers Map'),
     
'gis input' => 'wkt',
     
'safe reverse' => TRUE,
     
'gis types' => array('point', 'linestring', 'polygon'),
     
'element' => array(
       
'#type' => 'openlayers_cck_geo_field',
      ),
    ),
  );
  return
$inputs;
}
?>

Step 03: Create Element

Elements are a fundamental part of Drupal (not CCK or Geo). We definte the basic outline here for the element that will be used as the widget.

<?php
/**
* Implementation of FAPI hook_elements().
*/
function openlayers_cck_elements() {
  return array(
   
'openlayers_cck_geo_field' => array(
     
'#input' => TRUE,
     
'#columns' => array('geo_wkt'),
     
'#delta' => 0,
     
'#process' => array('openlayers_cck_geo_field_process'),
    ),
  );
}
?>

Step 04: Process Element

This function tells Drupal how to render the element that will be the input for our widget.

<?php
/**
* Process an individual element.
*
* Build the form element. When creating a form using FAPI #process,
* note that $element['#value'] is already set.
*
* The $fields array is in $form['#field_info'][$element['#field_name']].
*/
function openlayers_cck_geo_field_process($element, $edit, $form_state, $form) {
 
 
// Define some variables
 
$field = $form['#field_info'][$element['#parents'][0]];
 
$delta = $element['#delta'];
 
$field_name = $field['field_name'];
 
$widget = $field['#widget'];
 
 
// Make map for input
 
$rendered_map = _openlayers_cck_render_element_map($field_name, $element['#value'], $field);

 
// Create map
 
$element['map'] = array(
   
'#value' => $rendered_map['themed'],
  );
 
 
// Define field for wkt input
 
$element['geo_wkt'] = array(
   
'#type' => 'textarea',
   
'#rows' => 2,
   
'#title' => $element['#title'],
   
'#description' => $element['#description'],
   
'#default_value' => isset($element['#value']) ? $element['#value'] : NULL,
   
'#required' => $field['required'],
   
'#attributes' => array('rel' => OPENLAYERS_CCK_MAP_ID_PREFIX .'-'. $field['field_name']),
  );
 
  return
$element;
}
?>

Step 05: Output for Element

This is the theme implementation that we defined in Step 01. This is pretty standard.

<?php
/**
* Theme function for openlayers_cck_geo_field element
*/
function theme_openlayers_cck_geo_field($element) {
  return
$element['#children'];
}
?>

Related modules: