You are here

public function WeatherCurrentBlock::blockForm in Wunderground weather 8

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/WeatherCurrentBlock.php, line 69
Contains \Drupal\wunderground_weather\Plugin\Block\WeatherCurrentBlock.

Class

WeatherCurrentBlock
Provides a block with current weather conditions.

Namespace

Drupal\wunderground_weather\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $config = $this
    ->getConfiguration();
  $form = parent::blockForm($form, $form_state);
  $form['location'] = [
    '#type' => 'fieldset',
    '#title' => t('Location'),
  ];

  // Autocomplete to get location.
  $form['location']['location_current'] = [
    '#title' => t('Location path'),
    '#type' => 'textfield',
    '#description' => t('Search for your city to determine the Wunderground location path.'),
    '#maxlength' => 120,
    '#required' => TRUE,
    '#autocomplete_route_name' => 'wunderground_weather.autocomplete',
    '#default_value' => isset($config['location_current']) ? $config['location_current'] : '',
  ];
  $form['temperature_scale'] = [
    '#title' => t('Show temperature in'),
    '#type' => 'radios',
    '#options' => [
      'c' => t('Celsius'),
      'f' => t('Fahrenheit'),
    ],
    '#default_value' => isset($config['temperature_scale']) ? $config['temperature_scale'] : 'c',
  ];
  $form['windspeed_scale'] = [
    '#title' => t('Show wind speed in'),
    '#type' => 'radios',
    '#options' => [
      'bft' => t('Beaufort'),
      'mph' => t('Miles per hour'),
      'kph' => t('Kilometers per hour'),
    ],
    '#default_value' => isset($config['windspeed_scale']) ? $config['windspeed_scale'] : 'bft',
  ];
  $settings_current_defaults = [
    'weather' => 'weather',
    'conditions' => 'conditions',
    'temperature' => 'temperature',
    'feels_like' => 'feels_like',
    'wind' => 'wind',
  ];
  $form['current_fields'] = [
    '#title' => t('Fields'),
    '#type' => 'checkboxes',
    '#options' => [
      'weather' => t('Weather description'),
      'temperature' => t('Temperature'),
      'feels_like' => t('Feels like'),
      'wind' => t('Wind speed'),
    ],
    '#default_value' => isset($config['current_fields']) ? $config['current_fields'] : $settings_current_defaults,
  ];
  $icons = [];
  foreach (range('a', 'k') as $set) {
    $icons[$set] = $this->wundergroundWeatherManager
      ->getIconSetSample($set);
  }
  $form['icon_set'] = [
    '#titel' => t('Select an icons set'),
    '#type' => 'radios',
    '#options' => $icons,
    '#default_value' => isset($config['icon_set']) ? $config['icon_set'] : 'a',
  ];
  return $form;
}