You are here

public function WeatherForecastBlock::blockForm in Wunderground weather 8

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/WeatherForecastBlock.php, line 67
Contains \Drupal\wunderground_weather\Plugin\Block\WeatherForecastBlock.

Class

WeatherForecastBlock
Provides a with a five day weather forecast.

Namespace

Drupal\wunderground_weather\Plugin\Block

Code

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

  // Autocomplete to get location.
  $form['location']['location_forecast'] = [
    '#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_forecast']) ? $config['location_forecast'] : '',
  ];
  $form['temperature_scale'] = [
    '#title' => t('Show temperature in'),
    '#type' => 'radios',
    '#options' => [
      'c' => t('Celsius'),
      'f' => t('Fahrenheit'),
    ],
    '#default_value' => empty($this->configuration['temperature_scale']) ? 'c' : $this->configuration['temperature_scale'],
  ];
  $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_forecast_defaults = [
    'image' => 'image',
    'conditions' => 'conditions',
    'temperature' => 'temperature',
    'rain' => 'rain',
    'wind' => 'wind',
  ];
  $form['forecast_fields'] = [
    '#title' => t('Fields'),
    '#type' => 'checkboxes',
    '#options' => [
      'image' => t('Weather icons'),
      'conditions' => t('Weather description'),
      'temperature' => t('Temperature'),
      'rain' => t('Chance of rain'),
      'wind' => t('Wind speed'),
    ],
    '#default_value' => isset($config['forecast_fields']) ? $config['forecast_fields'] : $settings_forecast_defaults,
  ];
  $form['number_of_days'] = [
    '#title' => t('For how many days you would like to display a forecast'),
    '#description' => t('You can display up to 10 days'),
    '#type' => 'textfield',
    '#default_value' => isset($config['number_of_days']) ? $config['number_of_days'] : 3,
    '#size' => 2,
    '#maxlength' => 2,
    '#required' => TRUE,
  ];
  $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;
}