You are here

public function WeatherDisplayForm::buildForm in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Form/WeatherDisplayForm.php \Drupal\weather\Form\WeatherDisplayForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/WeatherDisplayForm.php, line 90

Class

WeatherDisplayForm
Form controller for the weather_display entity edit forms.

Namespace

Drupal\weather\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, string $display_type = '', int $display_number = 0) {
  $form = parent::buildForm($form, $form_state);
  $savedConfig = [];

  // Try to load saved config when we are editing
  // Default or User weather display.
  if ($display_type == WeatherDisplayInterface::DEFAULT_TYPE) {
    $display_number = 1;
    $defaultDisplay = $this->weatherDisplayStorage
      ->loadByProperties([
      'type' => WeatherDisplayInterface::DEFAULT_TYPE,
    ]);
    $defaultDisplay = reset($defaultDisplay);
    if ($defaultDisplay instanceof WeatherDisplayInterface) {
      $savedConfig = $defaultDisplay->config
        ->getValue()[0];
    }
  }
  elseif ($display_type == WeatherDisplayInterface::USER_TYPE) {
    $display_number = $this
      ->currentUser()
      ->id();
    $displayExists = $this->weatherDisplayStorage
      ->loadByProperties([
      'type' => WeatherDisplayInterface::USER_TYPE,
      'number' => $this
        ->currentUser()
        ->id(),
    ]);
    $systemwideDisplay = reset($displayExists);
    if ($systemwideDisplay instanceof WeatherDisplayInterface) {
      $savedConfig = $systemwideDisplay->config
        ->getValue()[0];
    }
  }
  elseif ($display_type == WeatherDisplayInterface::SYSTEM_WIDE_TYPE) {
    $displayExists = $this->weatherDisplayStorage
      ->loadByProperties([
      'type' => WeatherDisplayInterface::SYSTEM_WIDE_TYPE,
      'number' => $display_number,
    ]);
    $systemwideDisplay = reset($displayExists);
    if ($systemwideDisplay instanceof WeatherDisplayInterface) {
      $savedConfig = $systemwideDisplay->config
        ->getValue()[0];
    }
  }
  $defaultConfig = $this->weatherHelperService
    ->getDisplayConfig(WeatherDisplayInterface::DEFAULT_TYPE);
  $form['config'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Display configuration'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  ];
  $form['config']['temperature'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Temperature'),
    '#description' => $this
      ->t('Unit for displaying temperatures.'),
    '#default_value' => $savedConfig['temperature'] ?? $defaultConfig['temperature'],
    '#options' => [
      'celsius' => $this
        ->t('Celsius'),
      'fahrenheit' => $this
        ->t('Fahrenheit'),
      'celsiusfahrenheit' => $this
        ->t('Celsius / Fahrenheit'),
      'fahrenheitcelsius' => $this
        ->t('Fahrenheit / Celsius'),
    ],
  ];
  $form['config']['windspeed'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Wind speed'),
    '#description' => $this
      ->t('Unit for displaying wind speeds.'),
    '#default_value' => $savedConfig['windspeed'] ?? $defaultConfig['windspeed'],
    '#options' => [
      'kmh' => $this
        ->t('km/h'),
      'mph' => $this
        ->t('mph'),
      'knots' => $this
        ->t('Knots'),
      'mps' => $this
        ->t('meter/s'),
      'beaufort' => $this
        ->t('Beaufort'),
    ],
  ];
  $form['config']['pressure'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Pressure'),
    '#description' => $this
      ->t('Unit for displaying pressure.'),
    '#default_value' => $savedConfig['pressure'] ?? $defaultConfig['pressure'],
    '#options' => [
      'hpa' => $this
        ->t('hPa'),
      'kpa' => $this
        ->t('kPa'),
      'inhg' => $this
        ->t('inHg'),
      'mmhg' => $this
        ->t('mmHg'),
    ],
  ];
  $form['config']['distance'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Distance'),
    '#description' => $this
      ->t('Unit for displaying distances.'),
    '#default_value' => $savedConfig['distance'] ?? $defaultConfig['distance'],
    '#options' => [
      'kilometers' => $this
        ->t('Kilometers'),
      'miles' => $this
        ->t('UK miles'),
    ],
  ];
  $form['config']['show_sunrise_sunset'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show times of sunrise and sunset'),
    '#default_value' => $savedConfig['show_sunrise_sunset'] ?? $defaultConfig['show_sunrise_sunset'],
    '#description' => $this
      ->t('Displays the times of sunrise and sunset. This is always the local time.'),
  ];
  $form['config']['show_windchill_temperature'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show windchill temperature'),
    '#default_value' => $savedConfig['show_windchill_temperature'] ?? $defaultConfig['show_windchill_temperature'],
    '#description' => $this
      ->t('Displays the windchill temperature. This is how the temperature <q>feels like</q>. Note that windchill temperature is only defined for temperatures below 10 °C (50 °F) and wind speeds above 1.34 m/s (3 mph).'),
  ];
  $form['config']['show_abbreviated_directions'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show abbreviated wind directions'),
    '#default_value' => $savedConfig['show_abbreviated_directions'] ?? $defaultConfig['show_abbreviated_directions'],
    '#description' => $this
      ->t('Displays abbreviated wind directions like N, SE, or W instead of North, Southeast, or West.'),
  ];
  $form['config']['show_directions_degree'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show degrees of wind directions'),
    '#default_value' => $savedConfig['show_directions_degree'] ?? $defaultConfig['show_directions_degree'],
    '#description' => $this
      ->t('Displays the degrees of wind directions, for example, North (20°).'),
  ];
  $form['type'] = [
    '#type' => 'value',
    '#value' => $display_type,
  ];
  $form['number'] = [
    '#type' => 'value',
    '#value' => $display_number,
  ];

  // Show a 'reset' button if editing the default or user display.
  if (in_array($display_type, [
    WeatherDisplayInterface::DEFAULT_TYPE,
    WeatherDisplayInterface::USER_TYPE,
  ])) {
    $form['actions']['reset'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Reset'),
      '#weight' => 10,
      '#submit' => [
        '::submitForm',
        '::save',
      ],
    ];
  }

  // Use different path for delete form for non-admin user.
  if ($display_type == WeatherDisplayInterface::USER_TYPE) {
    $form["actions"]["delete"]["#url"] = Url::fromRoute('weather.user.weather_display.delete_form', [
      'user' => $display_number,
      'weather_display' => $this->entity
        ->id(),
    ]);
  }
  return $form;
}