You are here

function weather_location_settings_form in Weather 7

Same name and namespace in other branches
  1. 7.3 weather.forms.inc \weather_location_settings_form()
  2. 7.2 weather.forms.inc \weather_location_settings_form()

Create a settings form for a weather location.

Parameters

string $location_id: ID of the location.

Return value

array Form array.

1 string reference to 'weather_location_settings_form'
weather_menu in ./weather.module
Implement hook_menu().

File

./weather.forms.inc, line 396
Provide forms for configuration of weather displays.

Code

function weather_location_settings_form($form, &$form_state, $display_type, $display_number, $location_id = NULL) {
  $mode = 'edit';

  // Handle the addition of a new location.
  if ($location_id == 'add') {
    $mode = 'add';
    $location_id = NULL;
  }

  // If the location exists, get the settings. If it does not exist,
  // get the default location settings.
  $settings = weather_get_location_settings($location_id);
  if (!empty($form_state['values']['country'])) {
    $settings->country = $form_state['values']['country'];
  }
  $settings->places = weather_get_places($settings->country);
  $form['country'] = array(
    '#type' => 'select',
    '#title' => t('Country'),
    '#description' => t('Select a country to narrow down your search.'),
    '#default_value' => $settings->country,
    '#options' => drupal_map_assoc(weather_get_countries()),
    '#ajax' => array(
      'callback' => 'weather_location_settings_form_country_callback',
      'wrapper' => 'weather_place_replace',
    ),
  );
  $form['place'] = array(
    '#type' => 'select',
    '#title' => t('Place'),
    '#description' => t('Select a place in that country for the weather display.'),
    '#default_value' => $settings->icao,
    '#options' => $settings->places,
    '#prefix' => '<div id="weather_place_replace">',
    '#ajax' => array(
      'callback' => 'weather_location_settings_form_place_callback',
      'wrapper' => 'weather_real_name_replace',
    ),
  );
  $form['real_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Alternative name for the selected place'),
    '#default_value' => $settings->real_name,
    '#description' => t('You may enter another name for the place selected above.'),
    '#required' => TRUE,
    '#size' => '30',
    '#prefix' => '<div id="weather_real_name_replace">',
    '#suffix' => '</div></div>',
  );
  $form['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#default_value' => $settings->weight,
    '#description' => t('Optional. In the block, the heavier locations will sink and the lighter locations will be positioned nearer the top. Locations with equal weights are sorted alphabetically.'),
  );
  $form['id'] = array(
    '#type' => 'value',
    '#value' => $location_id,
  );
  $form['display_type'] = array(
    '#type' => 'value',
    '#value' => $display_type,
  );
  $form['display_number'] = array(
    '#type' => 'value',
    '#value' => $display_number,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  // Do not show the 'delete' button if not in 'edit' mode.
  if ($mode == 'edit') {
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#submit' => array(
        'weather_location_delete_submit',
      ),
    );
  }

  // If the form is regenerated during an AJAX callback, get the
  // country selected by the user.
  if (isset($form_state['triggering_element'])) {
    $settings->country = $form_state['values']['country'];
    if ($form_state['triggering_element']['#title'] == t('Country')) {
      $settings->places = weather_get_places($settings->country);
      $settings->icao = key($settings->places);
      $settings->real_name = $settings->places[$settings->icao];
      $form['place']['#options'] = $settings->places;
      $form['place']['#value'] = $settings->icao;
      $form['real_name']['#value'] = $settings->real_name;
    }
    if ($form_state['triggering_element']['#title'] == t('Place')) {
      $settings->real_name = $settings->places[$form_state['values']['place']];
      $form['real_name']['#value'] = $settings->real_name;
    }
  }
  return $form;
}