You are here

function geolocation_html5_field_widget_form in Geolocation Field 7

Implements hook_field_widget_form().

File

modules/geolocation_html5/geolocation_html5.module, line 64
HTML5 widget and formaters for Geolocation.

Code

function geolocation_html5_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $lat_value = isset($items[$delta]['lat']) ? $items[$delta]['lat'] : '';
  $lng_value = isset($items[$delta]['lng']) ? $items[$delta]['lng'] : '';
  $element += array(
    '#delta' => $delta,
  );
  switch ($instance['widget']['type']) {
    case 'geolocation_html5_widget':
      $element['lat'] = array(
        '#type' => 'hidden',
        '#attributes' => array(
          'class' => 'geolocation-lat',
        ),
        '#default_value' => $lat_value,
        '#maxlength' => 30,
      );
      $element['lng'] = array(
        '#type' => 'hidden',
        '#attributes' => array(
          'class' => 'geolocation-lng',
        ),
        '#default_value' => $lng_value,
        '#maxlength' => 30,
      );
      $element['save'] = array(
        '#type' => 'checkbox',
        '#title' => t('Save my location'),
        '#description' => t('Your web browser does not support geolocation (<a href="http://en.wikipedia.org/wiki/W3C_Geolocation_API">W3C Geolocation API</a>).'),
        '#default_value' => $lat_value !== '',
        '#required' => $instance['required'],
      );
      $dot_style = '';

      // If we have a lat/lng pair, calculate dot position.
      if ($lat_value != '' && $lng_value != '') {
        $latitude = $lat_value;
        $longitude = $lng_value;
        $left = _geolocation_html5_lng2px($longitude, -168, 450);
        $bottom = _geolocation_html5_lat2px($latitude, 78, -58, 250);
        $dot_style = ' style="display:block; left:' . $left . 'px; bottom:' . $bottom . 'px;"';
      }
      $element['map'] = array(
        '#type' => 'markup',
        '#markup' => '<div class="geolocation-html5-map"><div class="dot"' . $dot_style . '></div></div>',
      );
      $element['messages'] = array(
        '#type' => 'markup',
        '#markup' => '
          <div class="geolocation-html5-messages">
            <div class="geolocating">' . t('Your browser is looking for your location, you may need to approve this…') . '</div>
          </div>',
      );

      // Attach CSS and JS files via FAPI '#attached'.
      $element['map']['#attached']['css'][] = drupal_get_path('module', 'geolocation_html5') . '/geolocation_html5.css';
      $element['map']['#attached']['js'][] = array(
        'data' => drupal_get_path('module', 'geolocation_html5') . '/geolocation_html5.js',
        'type' => 'file',
      );
      $element['#element_validate'] = array(
        'geolocation_html5_field_widget_validate',
      );
      break;
  }
  return $element;
}