You are here

function geolocation_field in Geolocation Field 6

Implementation of hook_field().

File

./geolocation.module, line 118

Code

function geolocation_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'presave':
      foreach ($items as $delta => $item) {

        // Precalculate some goodness.
        $item['lat_sin'] = sin(deg2rad($item['lat']));
        $item['lat_cos'] = cos(deg2rad($item['lat']));
        $item['lng_rad'] = deg2rad($item['lng']);
        $items[$delta] = $item;
      }
      break;
    case 'validate':

      // Do validation on the field values here. The widget
      // will do its own validation and you cannot make any
      // assumptions about what kind of widget has been used,
      // so don't validate widget values, only field values.
      if (is_array($items)) {
        foreach ($items as $delta => $item) {

          // The error_element is needed so that CCK can
          // set an error on the right sub-element when
          // fields are deeply nested in the form.
          $error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
          if (!empty($item['lat']) || !empty($item['lng'])) {
            if (!is_numeric($item['lat'])) {
              form_set_error($error_element, t('Invalid Latitude.'));
            }
            if (!is_numeric($item['lng'])) {
              form_set_error($error_element, t('Invalid Longitude.'));
            }
          }
        }
      }
      return $items;
    case 'sanitize':

      // This is where you make sure that user-provided
      // data is sanitized before being displayed.
      foreach ($items as $delta => $item) {
        $geolocation['lat'] = check_plain($item['lat']);
        $geolocation['lng'] = check_plain($item['lng']);
        $items[$delta]['safe'] = $geolocation;
      }
  }
}