You are here

function gm3_field_field_widget_form in Google Maps API V3 7

Implements hook_field_widget_form().

File

gm3_field/gm3_field.module, line 886

Code

function gm3_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Data field type
  $data_field_type = isset($field['settings']['allow_text_entry']) ? $field['settings']['allow_text_entry'] : $field['type'] == 'gm3_point' && $field['cardinality'] == 1 ? 1 : 0;
  if ($data_field_type) {
    $data_field_type = 'textfield';
  }
  else {
    $data_field_type = 'hidden';
  }
  $widget = $element;

  // FIXME - Is this "delta" required? I don't think so.
  $widget['#delta'] = $delta;
  switch ($instance['widget']['type']) {
    case 'gm3_point_gm3':

      // We need to add a GM3 map to the page.
      // We'll also need some additional JS to record the points and save them
      // actually into a form element.
      foreach ($items as $key => $item) {
        $items[$key]['editable'] = TRUE;
      }
      $widget += array(
        '#prefix' => theme('gm3_map', array(
          'map' => array(
            'id' => $element['#field_name'],
            'libraries' => array(
              'point' => array(
                'points' => $items,
              ),
              'field_point' => array(
                'module' => 'gm3_field',
              ),
            ),
            'tools' => array(
              theme('gm3_point_button', array(
                'id' => $element['#field_name'],
              )),
            ),
            'max_objects' => $field['cardinality'] ? $field['cardinality'] : 1000000,
          ),
        )),
        '#attached' => array(
          'css' => array(
            drupal_get_path('module', 'gm3_field') . '/css/gm3_field.css',
          ),
        ),
        '#attributes' => array(
          'class' => array(
            $element['#field_name'] . '-point',
          ),
          'placeholder' => '(' . t('Latitude') . ', ' . t('Longitude') . ')',
        ),
        '#default_value' => _gm3_field_get_points_string_from_array($items),
        '#type' => $data_field_type,
      );
      break;
    case 'gm3_polyline_gm3':
    case 'gm3_polygon_gm3':
    case 'gm3_rectangle_gm3':
      $poly_type = explode("_", $instance['widget']['type']);
      $poly_type = $poly_type[1];
      $polys = array();
      $default_value_string = '';
      if (count($items)) {
        module_load_include('functions.inc', 'gm3');
        foreach ($items as $key => $item) {
          if (strlen($default_value_string)) {
            $default_value_string .= "\n";
          }
          $default_value_string .= $item[$poly_type];
          $polys = array_merge($polys, gm3_convert_polygon_string($item[$poly_type]));
        }
      }
      $widget += array(
        '#prefix' => theme('gm3_map', array(
          'map' => array(
            'id' => $element['#field_name'],
            'libraries' => array(
              $poly_type => array(
                $poly_type . 's' => $polys,
              ),
              'field_' . $poly_type => array(
                'module' => 'gm3_field',
              ),
            ),
            'tools' => array(
              theme('gm3_' . $poly_type . '_button', array(
                'id' => $element['#field_name'],
              )),
            ),
            'max_objects' => $field['cardinality'] ? $field['cardinality'] : 1000000,
          ),
        )),
        '#attached' => array(
          'css' => array(
            drupal_get_path('module', 'gm3_field') . '/css/gm3_field.css',
          ),
        ),
        '#attributes' => array(
          'class' => array(
            $element['#field_name'] . '-' . $poly_type,
          ),
        ),
        '#default_value' => $default_value_string,
        '#type' => $data_field_type,
      );
      break;
    case 'gm3_combination_gm3':
      $map = array(
        'id' => $element['#field_name'],
        'libraries' => array(),
        'tools' => array(),
        'max_objects' => $field['cardinality'] ? $field['cardinality'] : 1000000,
      );
      $widget += array(
        '#attached' => array(
          'css' => array(
            drupal_get_path('module', 'gm3_field') . '/css/gm3_field.css',
          ),
        ),
      );
      foreach ($field['settings']['field_types'] as $field_type => $value) {
        $default_values = array(
          'point' => array(),
          'polygon' => array(),
          'polyline' => array(),
          'rectangle' => array(),
        );
        $default_values_string = array(
          'polygon' => '',
          'polyline' => '',
          'rectangle' => '',
        );
        foreach ($items as $item) {
          if (isset($item['gm3_type'])) {
            switch ($item['gm3_type']) {
              case 'polygon':
              case 'polyline':
              case 'rectangle':
                module_load_include('functions.inc', 'gm3');
                if ($default_values_string[$item['gm3_type']] != '') {
                  $default_values_string[$item['gm3_type']] .= "\n";
                }
                $default_values_string[$item['gm3_type']] .= $item[$item['gm3_type']];
                $merge_array = gm3_convert_polygon_string($item[$item['gm3_type']]);
                $default_values[$item['gm3_type']] = array_merge(is_array($default_values[$item['gm3_type']]) ? $default_values[$item['gm3_type']] : array(), is_array($merge_array) ? $merge_array : array());
                break;
              case 'point':
                $default_values['point'][] = array(
                  'latitude' => $item['latitude'],
                  'longitude' => $item['longitude'],
                );
                break;
              default:
                $default_values[$item['gm3_type']][] = $item['region_id'];
                break;
            }
          }
        }
        switch ($field_type) {
          case 'gm3_point':
            $map['libraries']['point']['points'] = $default_values['point'];
            $map['libraries']['field_point']['module'] = 'gm3_field';
            $map['tools'][] = theme('gm3_point_button', array(
              'id' => $element['#field_name'],
            ));
            $widget['children']['gm3_point'] = array(
              '#type' => $data_field_type,
              '#title' => t('Point'),
              '#attributes' => array(
                'class' => array(
                  $element['#field_name'] . '-point',
                ),
              ),
              '#default_value' => _gm3_field_get_points_string_from_array($default_values['point']),
            );
            break;
          case 'gm3_polygon':
          case 'gm3_polyline':
          case 'gm3_rectangle':
            $poly_type = explode("_", $field_type);
            $poly_type = $poly_type[1];
            $map['libraries'][$poly_type][$poly_type . 's'] = $default_values[$poly_type];
            $map['libraries']['field_' . $poly_type]['module'] = 'gm3_field';
            $map['tools'][] = theme('gm3_' . $poly_type . '_button', array(
              'id' => $element['#field_name'],
            ));
            $widget['children']['gm3_' . $poly_type] = array(
              '#type' => $data_field_type,
              '#title' => $poly_type,
              '#maxlength' => 10000000,
              '#attributes' => array(
                'class' => array(
                  $element['#field_name'] . '-' . $poly_type,
                ),
              ),
              '#default_value' => $default_values_string[$poly_type],
            );
            break;
          default:

            // We have a field type not defined by this module, most likely it
            // is the gm3_region module.
            if (function_exists("{$field_type}_gm3_combination_form_alter")) {
              $function_name = "{$field_type}_gm3_combination_form_alter";
              $element = $function_name($form, $form_state, $field, $instance, $langcode, $items, $delta, $element, $map, $widget, $default_values);
            }
            break;
        }
      }
      $widget += array(
        '#prefix' => theme('gm3_map', array(
          'map' => $map,
        )),
      );
      break;
  }
  $title = $widget['#title'];
  unset($widget['#title']);
  $element['map'] = array(
    '#type' => 'fieldset',
    '#title' => $title,
    '#attributes' => array(
      'class' => array(
        'gm3_fieldset',
      ),
    ),
    'map' => $widget,
  );
  return $element;
}