You are here

function location_cck_widget in Location 5.3

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

Implementation of hook_widget().

File

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

Code

function location_cck_widget($op, &$node, $field, &$items) {
  switch ($op) {
    case 'form':
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
      );
      if ($field['multiple']) {
        $form[$field['field_name']]['#type'] = 'fieldset';
        $form[$field['field_name']]['#description'] = t($field['widget']['description']);
        $delta = 0;
        foreach ($items as $data) {
          if ($data['lid']) {
            $form[$field['field_name']][$delta] = array(
              '#type' => 'location_element',
              '#title' => $delta == 0 ? t($field['widget']['label']) : '',
              '#default_value' => $data,
              '#required' => $delta == 0 ? $field['required'] : FALSE,
              '#maxlength' => $field['max_length'] ? $field['max_length'] : NULL,
              '#location_settings' => $field['location_settings'],
              '#weight' => $field['widget']['weight'],
            );
            $delta++;
          }
        }
        foreach (range($delta, $delta + 2) as $delta) {
          $form[$field['field_name']][$delta] = array(
            '#type' => 'location_element',
            '#title' => $delta == 0 ? t($field['widget']['label']) : '',
            '#default_value' => array(),
            '#required' => $delta == 0 ? $field['required'] : FALSE,
            '#weight' => $field['widget']['weight'],
            '#location_settings' => $field['location_settings'],
          );
        }
      }
      else {
        $form[$field['field_name']][0] = array(
          '#type' => 'location_element',
          '#title' => t($field['widget']['label']),
          '#default_value' => isset($items[0]) ? $items[0] : array(),
          '#required' => $field['required'],
          '#description' => t($field['widget']['description']),
          '#weight' => $field['widget']['weight'],
          '#location_settings' => $field['location_settings'],
        );
      }
      return $form;
    case 'process form values':

      // Don't save empty fields except the first value
      foreach ($items as $delta => $item) {

        //        if ($item['value'] == '' && $delta > 0) {
        //        unset($items[$delta]);
        //    }
      }
      break;
    case 'prepare form values':

      // Load locations for use in edit form.
      foreach ($items as $k => $item) {
        $items[$k] = location_load_location($item['lid']);
      }
      break;
  }
}