You are here

function weather_location_settings_form in Weather 7.3

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

Create a form for a weather place.

Parameters

string $id: ID of the place in the table weather_displays_places.

Return value

array Form array.

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

File

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

Code

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

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

  // If the place exists, get the configuration. If it does not exist,
  // get the default place configuration.
  $settings = weather_get_location_settings($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->place_geoid,
    '#options' => $settings->places,
    '#prefix' => '<div id="weather_place_replace">',
    '#ajax' => array(
      'callback' => 'weather_location_settings_form_place_callback',
      'wrapper' => 'weather_displayed_name_replace',
    ),
  );
  $form['displayed_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Displayed name for the selected place'),
    '#default_value' => $settings->displayed_name,
    '#description' => t('You may enter another name for the place selected above.'),
    '#required' => TRUE,
    '#size' => '30',
    '#prefix' => '<div id="weather_displayed_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' => $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->place_geoid = key($settings->places);
      $settings->displayed_name = $settings->places[$settings->place_geoid];
      $form['place']['#options'] = $settings->places;
      $form['place']['#attributes']['value'] = $settings->place_geoid;
      $form['displayed_name']['#attributes']['value'] = $settings->displayed_name;
    }
    if ($form_state['triggering_element']['#title'] == t('Place')) {
      $settings->displayed_name = $settings->places[$form_state['values']['place']];
      $form['displayed_name']['#attributes']['value'] = $settings->displayed_name;
    }
  }
  return $form;
}