You are here

public function AddCustomPlaceForm::buildForm in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Form/AddCustomPlaceForm.php \Drupal\weather\Form\AddCustomPlaceForm::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 FormInterface::buildForm

File

src/Form/AddCustomPlaceForm.php, line 113

Class

AddCustomPlaceForm
Configure Weather settings for this site.

Namespace

Drupal\weather\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $placesStorage = $this->entityTypeManager
    ->getStorage('weather_place');
  $form['weather_yrno_url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('URL of English weather forecast on yr.no'),
    '#description' => $this
      ->t('Example: https://www.yr.no/place/Ukraine/Kiev/Kyiv/.'),
    '#required' => TRUE,
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save place'),
  ];

  // Create tables for modified places.
  $tables = [
    WeatherPlaceInterface::STATUS_ADDED => t('Added places'),
    WeatherPlaceInterface::STATUS_MODIFIED => t('Modified places'),
  ];
  foreach ($tables as $status => $caption) {
    $header = [
      $this
        ->t('GeoID'),
      $this
        ->t('Latitude'),
      $this
        ->t('Longitude'),
      $this
        ->t('Country'),
      $this
        ->t('Name'),
      $this
        ->t('Link'),
    ];
    $rows = [];
    $result = $placesStorage
      ->getQuery()
      ->condition('status', $status)
      ->sort('country', 'ASC')
      ->sort('name', 'ASC')
      ->execute();
    if (!empty($result)) {
      foreach ($placesStorage
        ->loadMultiple($result) as $place) {
        $rows[] = [
          $place->geoid->value,
          $place->latitude->value,
          $place->longitude->value,
          $place->country->value,
          $place->name->value,
          $place->link->value,
        ];
      }
      $form['places'][] = [
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#caption' => $caption,
        '#empty' => $this
          ->t('No places.'),
      ];
    }
  }
  return $form;
}