You are here

function location_cck_field_widget_form in Location 7.5

Same name and namespace in other branches
  1. 7.3 contrib/location_cck/location_cck.module \location_cck_field_widget_form()
  2. 7.4 contrib/location_cck/location_cck.module \location_cck_field_widget_form()

Implement hook_field_widget_form().

File

contrib/location_cck/location_cck.module, line 362
Defines location field type.

Code

function location_cck_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($field['type'] == 'location') {
    $settings = $field['settings']['location_settings'];

    // Load location data for existing locations.
    if ($form_state['rebuild'] && !empty($form_state['values'][$field['field_name']][$langcode][$delta])) {
      $location = $form_state['values'][$field['field_name']][$langcode][$delta];
    }
    elseif (isset($items[$delta]['lid']) && $items[$delta]['lid']) {
      $location = location_load_location($items[$delta]['lid']);
    }
    else {
      if (isset($items[$delta]) && is_array($items[$delta]) && !empty($items[$delta])) {

        // Initialize empty location.
        $location = location_empty_location($settings);
        foreach ($items[$delta] as $k => $v) {
          $location[$k] = $v;
        }

        // We can't trust that CCK is giving us the right information.
        // It can't tell us whether $items is defaults or multistep values.
        // Location *needs* the defaults to match the initial field values,
        // so we re-calculate the defaults here and stash them into the settings.
        // @@@ There is still a bug here!
        // If you go back and edit something, and you hadn't set a location the
        // first time, CCK fails to set up the defaults properly!
        // I'm just going to leave it like that for now, because I don't know how
        // to work around it.
        $temp = NULL;

        //content_default_value($form, $form_state, $field, 0);
        if (is_array($temp) && isset($temp[$delta]) && is_array($temp[$delta])) {
          foreach ($temp[$delta] as $k => $v) {
            $settings['form']['fields'][$k]['default'] = $v;
          }
        }

        //      unset($location['location_settings']);
        //      unset($location['locpick']);
      }
      else {
        $location = location_empty_location($settings);
      }
    }
    $element = array(
      '#type' => 'location_element',
      '#has_garbage_value' => TRUE,
      '#value' => '',
      '#title' => t($instance['label']),
      '#description' => t($instance['description']),
      '#required' => $instance['required'],
      '#location_settings' => $settings,
      '#default_value' => $location,
    );
    return $element;
  }
}