You are here

function google_map_field_field_widget_form in Google Map Field 7.2

Same name and namespace in other branches
  1. 7 google_map_field.module \google_map_field_field_widget_form()

Implements hook_field_widget_form().

File

./google_map_field.module, line 219
This file defines all the necessary hooks and functions to create a Google Map Field field type for inserting maps directly into content items (node, entities etc).

Code

function google_map_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $form['#attached']['js'][] = google_map_field_get_protocol() . 'maps.googleapis.com/maps/api/js?sensor=false&key=' . variable_get('google_map_field_apikey', '');
  $field_id = is_a($element['#entity'], 'FieldCollectionItemEntity') ? $element['#entity']->item_id : $field['id'];
  $field_id = empty($field_id) ? rand(10000000, 99999999) : $field_id;
  $element['#title'] = t('Define Location');
  $element['#description'] = '<p>' . t('To set a location for the map, click the "Set Map Marker" button, or enter the latitude/longitude in the field provided.') . '</p>';
  $element += array(
    '#type' => 'fieldset',
    '#collapsed' => FALSE,
    '#collapsible' => TRUE,
    '#attached' => array(
      'library' => array(
        array(
          'system',
          'ui.dialog',
        ),
      ),
      'js' => array(
        drupal_get_path('module', 'google_map_field') . '/js/google_map_field_previews.js',
        drupal_get_path('module', 'google_map_field') . '/js/google_map_field_setter.js',
      ),
      'css' => array(
        drupal_get_path('module', 'google_map_field') . '/css/google_map_field.css',
      ),
    ),
  );
  $defaults = array(
    'name' => isset($instance['default_value'][0]['name']) ? $instance['default_value'][0]['name'] : t('Default'),
    'lat' => isset($instance['default_value'][0]['lat']) ? $instance['default_value'][0]['lat'] : 51.524295,
    'lon' => isset($instance['default_value'][0]['lon']) ? $instance['default_value'][0]['lon'] : -0.1279899999999543,
    'zoom' => isset($instance['default_value'][0]['zoom']) ? $instance['default_value'][0]['zoom'] : 9,
  );
  $name = isset($items[$delta]['name']) ? $items[$delta]['name'] : '';
  $lat = isset($items[$delta]['lat']) ? $items[$delta]['lat'] : '';
  $lng = isset($items[$delta]['lon']) ? $items[$delta]['lon'] : '';
  $zoom = isset($items[$delta]['zoom']) ? $items[$delta]['zoom'] : '';
  $element['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Map Name'),
    '#default_value' => $name,
    '#prefix' => '<div class="google-map-field-fields-container">',
    '#attributes' => array(
      'data-name-delta' => $delta,
      'data-name-field-id' => $field_id,
    ),
  );
  $element['lat'] = array(
    '#type' => 'textfield',
    '#title' => t('Latitude'),
    '#required' => $instance['required'],
    '#element_validate' => array(
      'google_map_field_latlon_validate',
    ),
    '#default_value' => $lat,
    '#attributes' => array(
      'data-lat-delta' => $delta,
      'data-lat-field-id' => $field_id,
      'class' => array(
        'google-map-field-watch-change',
      ),
    ),
  );
  $element['lon'] = array(
    '#type' => 'textfield',
    '#title' => t('Longitude'),
    '#required' => $instance['required'],
    '#element_validate' => array(
      'google_map_field_latlon_validate',
    ),
    '#default_value' => $lng,
    '#attributes' => array(
      'data-lng-delta' => $delta,
      'data-lng-field-id' => $field_id,
      'class' => array(
        'google-map-field-watch-change',
      ),
    ),
  );
  $element['zoom'] = array(
    '#type' => 'textfield',
    '#title' => t('Zoom'),
    '#default_value' => $zoom,
    '#attributes' => array(
      'data-zoom-delta' => $delta,
      'data-zoom-field-id' => $field_id,
      'class' => array(
        'google-map-field-watch-change',
      ),
    ),
  );
  $element['enter_defaults'] = array(
    '#type' => 'button',
    '#value' => t('Defaults'),
    '#attributes' => array(
      'id' => 'enter_defaults_' . $field_id . '_' . $delta,
      'data-delta' => $delta,
      'data-field-id' => $field_id,
      'data-default-name' => $defaults['name'],
      'data-default-lat' => $defaults['lat'],
      'data-default-lon' => $defaults['lon'],
      'data-default-zoom' => $defaults['zoom'],
      'class' => array(
        'google-map-field-defaults',
      ),
    ),
  );
  $element['clear_fields'] = array(
    '#type' => 'button',
    '#value' => t('Clear Fields'),
    '#attributes' => array(
      'data-delta' => $delta,
      'data-field-id' => $field_id,
      'id' => 'clear_fields_' . $field_id . '_' . $delta,
      'class' => array(
        'google-map-field-clear',
      ),
    ),
    '#suffix' => '</div>',
  );
  $element['map_preview_' . $delta] = array(
    '#type' => 'container',
    '#prefix' => '<div class="google-map-field-setter-container">',
    '#suffix' => '<div><strong>' . t('Map Preview') . '</strong></div><div class="google-map-field-preview" date-field-name="' . $field['field_name'] . '" data-field-id="' . $field_id . '" data-delta="' . $delta . '"></div>',
  );
  $element['open_map'] = array(
    '#type' => 'button',
    '#value' => t('Set Map Marker'),
    '#attributes' => array(
      'data-delta' => $delta,
      'data-field-id' => $field_id,
      'id' => 'map_setter_' . $field_id . '_' . $delta,
      'data-field-name' => $field['field_name'],
    ),
    '#suffix' => '</div><div style="clear: both;"></div>',
  );
  return $element;
}