You are here

function getlocations_fields_field_widget_form in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_fields/getlocations_fields.module \getlocations_fields_field_widget_form()

Implements hook_field_widget_form(). Return the form for a single field widget.

Parameters

$form: The form structure where widgets are being attached to. This might be a full form structure, or a sub-element of a larger form.

$form_state: An associative array containing the current state of the form.

$field: The field structure.

$instance: The field instance.

$langcode: The language associated with $items.

$items: Array of default values for this field.

$delta: The order of this item in the array of subelements (0, 1, 2, etc).

$element: A form element array containing basic properties for the widget.

Return value

The form elements for a single widget for this field.

File

modules/getlocations_fields/getlocations_fields.module, line 1201
getlocations_fields.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_fields_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  switch ($instance['widget']['type']) {
    case 'getlocations_fields':

      // is this real or a demo in settings
      $active = TRUE;
      if (empty($element['#entity'])) {
        $active = FALSE;
      }
      $element['#delta'] = $delta;
      $field_name = '';
      if (isset($field['field_name'])) {
        $field_name = $field['field_name'];
        $items[$delta]['field_name'] = $field_name;
      }
      $cardinality = $field['cardinality'];
      $settings = $field['settings'];

      // Wrap in a fieldset for single fields
      if ($cardinality == 1) {
        $element['#type'] = 'fieldset';
        $element['#collapsible'] = TRUE;
        $element['#collapsed'] = FALSE;
      }

      // delete location
      if (isset($items[$delta]['glid']) && $items[$delta]['glid'] > 0) {
        $element['glid'] = array(
          '#type' => 'value',
          '#value' => $items[$delta]['glid'],
        );
        if (!$instance['required']) {
          $element['delete_location'] = array(
            '#type' => 'checkbox',
            '#title' => t('Delete'),
            '#default_value' => FALSE,
            '#description' => t('Check this box to delete this location.'),
          );
        }
      }

      // search field
      if ($settings['use_address']) {
        $element['address'] = array(
          '#type' => 'textfield',
          '#default_value' => isset($items[$delta]['address']) ? $items[$delta]['address'] : (isset($instance['default_value'][$delta]['address']) ? $instance['default_value'][$delta]['address'] : ''),
          '#size' => $settings['input_address_width'],
          '#title' => t('Search'),
          '#description' => t('Start typing an address and select from the dropdown.'),
          '#field_name' => $instance['field_name'],
          '#maxlength' => 255,
        );
        if ($instance['required']) {
          if ($active) {
            $element['address']['#required'] = TRUE;
          }
          else {
            $element['address']['#field_suffix'] = t('(Required)');
          }
        }
      }

      // TODO add a message, required form or not
      // smart_ip
      if ($active) {
        if (module_exists('smart_ip') && $settings['use_smart_ip_button']) {
          $element['smart_ip_button'] = array(
            '#markup' => '',
          );
          if (!empty($settings['input_smart_ip_button_weight'])) {
            $element['smart_ip_button']['#weight'] = $settings['input_smart_ip_button_weight'];
          }
        }
        if ($settings['use_geolocation_button']) {
          $element['geolocation_button'] = array(
            '#markup' => '',
          );
          if (!empty($settings['input_geolocation_button_weight'])) {
            $element['geolocation_button']['#weight'] = $settings['input_geolocation_button_weight'];
          }
        }
      }

      // loop over these as they are all the same
      $required = array();
      $inputarr = array(
        'name' => t('Name'),
        'street' => t('Street'),
        'additional' => t('Additional'),
        'city' => t('City/Town'),
        'province' => t('Province/State/County'),
        'postal_code' => t('Post code/Zip code'),
      );
      foreach ($inputarr as $id => $title) {
        if ($settings['input_' . $id . '_required'] == 4) {

          // hidden form
          $element[$id] = array(
            '#type' => 'hidden',
            '#value' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
        }
        elseif ($settings['input_' . $id . '_required'] == 3) {
          $element[$id] = array(
            '#type' => 'hidden',
            '#value' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
          $element[$id . '_display'] = array(
            '#markup' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
        }
        else {
          $element[$id] = array(
            '#type' => 'textfield',
            '#default_value' => isset($items[$delta][$id]) ? htmlspecialchars_decode($items[$delta][$id], ENT_QUOTES) : (isset($instance['default_value'][$delta][$id]) ? htmlspecialchars_decode($instance['default_value'][$delta][$id], ENT_QUOTES) : ''),
            '#size' => $settings['input_' . $id . '_width'],
            '#title' => $title,
            '#maxlength' => 255,
          );

          // autocomplete for province
          if ($id == 'province' && $settings['province_autocomplete']) {
            $element[$id]['#autocomplete_path'] = 'getlocations_fields/province_autocomplete';
          }
          elseif ($id == 'city' && $settings['city_autocomplete']) {
            $element[$id]['#autocomplete_path'] = 'getlocations_fields/city_autocomplete';
          }
        }
        if ($settings['input_' . $id . '_required'] == 2) {
          $element[$id]['#disabled'] = TRUE;
        }
        elseif ($settings['input_' . $id . '_required'] == 1) {
          if ($active) {
            if ($cardinality == -1) {

              # needs to be replaced with a 'fake'
              $required[] = $id;
            }
            else {
              $element[$id]['#required'] = TRUE;
            }
          }
          else {
            $element[$id]['#field_suffix'] = t('(Required)');
          }
        }
        if (!empty($settings['input_' . $id . '_weight'])) {
          $element[$id]['#weight'] = $settings['input_' . $id . '_weight'];
        }
      }

      // country
      if ($settings['restrict_by_country'] && $settings['search_country']) {
        $default_country_value = isset($items[$delta]['country']) && $items[$delta]['country'] ? $items[$delta]['country'] : (isset($instance['default_value'][$delta]['country']) ? $instance['default_value'][$delta]['country'] : $settings['search_country']);
        $default_country_value_long = getlocations_get_country_name($default_country_value);
        $element['country'] = array(
          '#type' => 'hidden',
          '#value' => $default_country_value,
        );
        $element['country_display'] = array(
          '#markup' => $default_country_value_long,
        );
      }
      else {
        $default_country_value = isset($items[$delta]['country']) && $items[$delta]['country'] ? $items[$delta]['country'] : (isset($instance['default_value'][$delta]['country']) ? $instance['default_value'][$delta]['country'] : $settings['country']);
        $default_country_value_long = $default_country_value;
        if ($default_country_value && drupal_strlen($default_country_value) == 2) {
          $default_country_value_long = getlocations_get_country_name($default_country_value);
        }
        if ($settings['input_country_required'] == 4) {

          // hidden form
          $element['country'] = array(
            '#type' => 'hidden',
            '#value' => $default_country_value,
          );
        }
        elseif ($settings['input_country_required'] == 3) {

          // hidden form
          $element['country'] = array(
            '#type' => 'hidden',
            '#value' => $default_country_value,
          );
          $element['country_display'] = array(
            '#markup' => $default_country_value_long,
          );
        }
        else {
          if ($settings['use_country_dropdown'] == 1) {
            $only_continents = '';
            $only_countries = '';
            if (module_exists('countries')) {
              $only_continents = isset($settings['only_continents']) ? $settings['only_continents'] : '';
              $only_countries = isset($settings['only_countries']) ? $settings['only_countries'] : '';
            }
            $countries = getlocations_get_countries_list(TRUE, $only_continents, $only_countries);
            $countries = array_merge(array(
              '' => t('---Please select a country---'),
            ), $countries);
            $element['country'] = array(
              '#type' => 'select',
              '#default_value' => $default_country_value,
              '#options' => $countries,
              '#title' => t('Country'),
            );
          }
          else {
            $element['country'] = array(
              '#type' => 'textfield',
              '#default_value' => $default_country_value_long,
              '#autocomplete_path' => $settings['use_country_dropdown'] == 2 ? 'getlocations_fields/country_autocomplete' : '',
              '#size' => $settings['input_country_width'],
              '#title' => t('Country'),
              '#maxlength' => 255,
            );
          }
          if ($settings['input_country_required'] == 2) {
            $element['country']['#disabled'] = TRUE;
          }
          elseif ($settings['input_country_required'] == 1) {
            if ($active) {
              if ($cardinality == -1) {
                $required[] = 'country';
              }
              else {
                $element['country']['#required'] = TRUE;
              }
            }
            else {
              $element['country']['#field_suffix'] = t('(Required)');
            }
          }
        }
        if (!empty($settings['input_country_weight'])) {
          $element['country']['#weight'] = $settings['input_country_weight'];
        }
      }
      $inputarr = array(
        'phone' => t('Phone'),
        'mobile' => t('Mobile'),
        'fax' => t('Fax'),
      );
      foreach ($inputarr as $id => $title) {
        if ($settings['input_' . $id . '_required'] == 4) {

          // hidden form
          $element[$id] = array(
            '#type' => 'hidden',
            '#value' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
        }
        elseif ($settings['input_' . $id . '_required'] == 3) {
          $element[$id] = array(
            '#type' => 'hidden',
            '#value' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
          $element[$id . '_display'] = array(
            '#markup' => isset($items[$delta][$id]) ? $items[$delta][$id] : (isset($instance['default_value'][$delta][$id]) ? $instance['default_value'][$delta][$id] : ''),
          );
        }
        else {
          $element[$id] = array(
            '#type' => 'textfield',
            '#default_value' => isset($items[$delta][$id]) ? htmlspecialchars_decode($items[$delta][$id], ENT_QUOTES) : (isset($instance['default_value'][$delta][$id]) ? htmlspecialchars_decode($instance['default_value'][$delta][$id], ENT_QUOTES) : ''),
            '#size' => $settings['input_' . $id . '_width'],
            '#title' => $title,
            '#maxlength' => 255,
          );
        }
        if ($settings['input_' . $id . '_required'] == 2) {
          $element[$id]['#disabled'] = TRUE;
        }
        elseif ($settings['input_' . $id . '_required'] == 1) {
          if ($active) {
            if ($cardinality == -1) {

              # needs to be replaced with a 'fake'
              $required[] = $id;
            }
            else {
              $element[$id]['#required'] = TRUE;
            }
          }
          else {
            $element[$id]['#field_suffix'] = t('(Required)');
          }
        }
        if (!empty($settings['input_' . $id . '_weight'])) {
          $element[$id]['#weight'] = $settings['input_' . $id . '_weight'];
        }
      }

      // geobutton
      if ($active) {
        if ($settings['use_address'] < 2) {
          $element['geobutton'] = array(
            '#markup' => '',
          );
          if (!empty($settings['input_geobutton_weight'])) {
            $element['geobutton']['#weight'] = $settings['input_geobutton_weight'];
          }
        }
      }

      // Clear button
      if ($settings['use_clear_button']) {
        $element['clear_button'] = array(
          '#markup' => '',
        );
        if (!empty($settings['input_clear_button_weight'])) {
          $element['clear_button']['#weight'] = $settings['input_clear_button_weight'];
        }
      }

      // map
      $element['map'] = array(
        '#markup' => '',
      );
      if (!empty($settings['input_map_weight'])) {
        $element['map']['#weight'] = $settings['input_map_weight'];
      }

      // latitude
      $element['latitude'] = array(
        '#type' => 'textfield',
        '#default_value' => isset($items[$delta]['latitude']) ? $items[$delta]['latitude'] : (isset($instance['default_value'][$delta]['latitude']) ? $instance['default_value'][$delta]['latitude'] : NULL),
        '#size' => $settings['input_latitude_width'],
        '#title' => t('Latitude'),
        '#maxlength' => 20,
      );
      if (!empty($settings['input_latitude_weight'])) {
        $element['latitude']['#weight'] = $settings['input_latitude_weight'];
      }

      // longitude
      $element['longitude'] = array(
        '#type' => 'textfield',
        '#default_value' => isset($items[$delta]['longitude']) ? $items[$delta]['longitude'] : (isset($instance['default_value'][$delta]['longitude']) ? $instance['default_value'][$delta]['longitude'] : NULL),
        '#size' => $settings['input_longitude_width'],
        '#title' => t('Longitude'),
        '#maxlength' => 20,
      );
      if (!empty($settings['input_longitude_weight'])) {
        $element['longitude']['#weight'] = $settings['input_longitude_weight'];
      }

      // per_item_marker
      if ($settings['per_item_marker']) {
        $markers = getlocations_get_marker_titles();
        $markers = array_merge(array(
          '' => t('Default'),
        ), $markers);
        $element['marker'] = getlocations_element_map_marker(t('Map marker'), $markers, isset($items[$delta]['marker']) ? $items[$delta]['marker'] : (isset($instance['default_value'][$delta]['marker']) ? $instance['default_value'][$delta]['marker'] : ''));
        if (!empty($settings['input_marker_weight'])) {
          $element['marker']['#weight'] = $settings['input_marker_weight'];
        }
      }
      else {
        $element['marker'] = array(
          '#type' => 'value',
          '#value' => '',
        );
      }

      // streetview killswitch
      if (getlocations_fields_streetview_settings_allow()) {
        if (module_exists('views')) {
          $element['sv_enable'] = getlocations_element_map_checkbox(t('Enable Streetview'), isset($items[$delta]['sv_enable']) ? $items[$delta]['sv_enable'] : (isset($instance['default_value'][$delta]['sv_enable']) ? $instance['default_value'][$delta]['sv_enable'] : 1), t('Enables Streetview for this location in Views.'));
          $element['sv_enable']['#suffix'] = '<div id="wrap-streetview-enable">';
        }
        else {
          $element['sv_enable'] = array(
            '#type' => 'value',
            '#value' => 0,
          );
        }
        $element['sv_showfirst'] = getlocations_element_map_checkbox(t('Enable the Streetview overlay'), isset($items[$delta]['sv_showfirst']) ? $items[$delta]['sv_showfirst'] : (isset($instance['default_value'][$delta]['sv_showfirst']) ? $instance['default_value'][$delta]['sv_showfirst'] : 1), t('Enables Streetview on top of the initial map.'));
        if (module_exists('views')) {
          $element['sv_showfirst']['#suffix'] = '</div>';
        }
      }
      else {
        $element['sv_enable'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
        $element['sv_showfirst'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
      }

      // hidden fields, these are filled in by jQuery
      if (isset($items[$delta]['glid']) && $items[$delta]['glid'] > 0) {
        $d_mapzoom = isset($items[$delta]['mapzoom']) ? $items[$delta]['mapzoom'] : (isset($instance['default_value'][$delta]['mapzoom']) ? $instance['default_value'][$delta]['mapzoom'] : $settings['zoom']);
        $d_map_maptype = isset($items[$delta]['map_maptype']) ? $items[$delta]['map_maptype'] : (isset($instance['default_value'][$delta]['map_maptype']) ? $instance['default_value'][$delta]['map_maptype'] : $settings['maptype']);
        $d_sv_heading = isset($items[$delta]['sv_heading']) ? $items[$delta]['sv_heading'] : (isset($instance['default_value'][$delta]['sv_heading']) ? $instance['default_value'][$delta]['sv_heading'] : $settings['sv_heading']);
        $d_sv_zoom = isset($items[$delta]['sv_zoom']) ? $items[$delta]['sv_zoom'] : (isset($instance['default_value'][$delta]['sv_zoom']) ? $instance['default_value'][$delta]['sv_zoom'] : $settings['sv_zoom']);
        $d_sv_pitch = isset($items[$delta]['sv_pitch']) ? $items[$delta]['sv_pitch'] : (isset($instance['default_value'][$delta]['sv_pitch']) ? $instance['default_value'][$delta]['sv_pitch'] : $settings['sv_pitch']);
      }
      else {
        $d_mapzoom = $settings['zoom'];
        $d_map_maptype = $settings['maptype'];
        $d_sv_heading = $settings['sv_heading'];
        $d_sv_zoom = $settings['sv_zoom'];
        $d_sv_pitch = $settings['sv_pitch'];
      }
      $element['mapzoom'] = array(
        '#type' => 'textfield',
        '#default_value' => $d_mapzoom,
        '#prefix' => '<div class="js-hide">',
      );
      $element['map_maptype'] = array(
        '#type' => 'textfield',
        '#default_value' => $d_map_maptype,
      );
      $element['sv_heading'] = array(
        '#type' => 'textfield',
        '#default_value' => $d_sv_heading,
      );
      $element['sv_zoom'] = array(
        '#type' => 'textfield',
        '#default_value' => $d_sv_zoom,
      );
      $element['sv_pitch'] = array(
        '#type' => 'textfield',
        '#default_value' => $d_sv_pitch,
        '#suffix' => '</div>',
      );
      $element['active'] = array(
        '#type' => 'value',
        '#value' => $active,
      );
      if (count($required)) {
        $element['getlocations_required'] = array(
          '#type' => 'value',
          '#value' => implode(',', $required),
        );
      }
      $element['settings'] = array(
        '#type' => 'value',
        '#value' => $settings,
      );
      unset($element['#theme']);
      $element['#theme'] = 'getlocations_fields_field_widget_form';
      break;
  }
  return $element;
}