You are here

function location_cck_widget in Location 6.3

Same name and namespace in other branches
  1. 5.3 contrib/location_cck/location_cck.module \location_cck_widget()

Implementation of hook_widget().

File

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

Code

function location_cck_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  if ($field['widget']['type'] == 'location') {
    $settings = $field['location_settings'];

    // Load location data for existing locations.
    if (isset($items[$delta]['lid']) && $items[$delta]['lid']) {

      // Are we in a node preview?
      // If we aren't, then we only have the lid, because that's all cck
      // actually knows about internally. So, we have to pull in the location
      // at this point.
      if (empty($items[$delta]['cck_preview_in_progress'])) {
        $location = location_load_location($items[$delta]['lid']);
      }
      else {

        // Otherwise, the data was already populated and we're running
        // off the form state.
        $location = $items[$delta];
      }
    }
    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 = 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']);
      }
    }
    $element = array(
      '#type' => 'location_element',
      '#title' => t($field['widget']['label']),
      '#description' => t($field['widget']['description']),
      '#required' => $field['required'],
      '#location_settings' => $settings,
      '#default_value' => $location,
    );

    // This is used to determine whether we are in a preview or not, because
    // several pieces of code work differently when previewing.
    $element['cck_preview_in_progress'] = array(
      '#type' => 'value',
      '#value' => TRUE,
    );
    return $element;
  }
}