You are here

function location_cck_field_widget_form in Location 7.3

Same name and namespace in other branches
  1. 7.5 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 428
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 = isset($field['settings']['location_settings']) ? $field['settings']['location_settings'] : array();
    if (isset($form_state['values'])) {
      $form_state_field_values = drupal_array_get_nested_value($form_state['values'], $element['#field_parents']);
    }

    // Load location data for existing locations.
    if ($form_state['rebuild'] && !empty($form_state_field_values[$field['field_name']][$langcode][$delta])) {
      $location = $form_state_field_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.s
        $temp = NULL;
        if (is_array($temp) && isset($temp[$delta]) && is_array($temp[$delta])) {
          foreach ($temp[$delta] as $k => $v) {
            $settings['form']['fields'][$k]['default'] = $v;
          }
        }
      }
      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,
      '#delta' => $delta,
    );
    return $element;
  }
}