You are here

public function WeatherUserConfiguredDisplayController::content in Weather 2.0.x

Same name and namespace in other branches
  1. 8 src/Controller/WeatherUserConfiguredDisplayController.php \Drupal\weather\Controller\WeatherUserConfiguredDisplayController::content()

Builds the response.

1 string reference to 'WeatherUserConfiguredDisplayController::content'
weather.routing.yml in ./weather.routing.yml
weather.routing.yml

File

src/Controller/WeatherUserConfiguredDisplayController.php, line 73

Class

WeatherUserConfiguredDisplayController
Returns responses for Weather routes.

Namespace

Drupal\weather\Controller

Code

public function content(UserInterface $user) {
  $output = [];
  $weatherDisplayPlaceStorage = $this->entityTypeManager
    ->getStorage('weather_display_place');
  $header = [
    $this
      ->t('Displayed name'),
    $this
      ->t('Weight'),
  ];
  $rows = [];
  $result = $weatherDisplayPlaceStorage
    ->getQuery()
    ->condition('display_type', WeatherDisplayInterface::USER_TYPE)
    ->condition('display_number', $user
    ->id())
    ->sort('weight', 'ASC')
    ->sort('displayed_name', 'ASC')
    ->execute();
  if ($result) {
    foreach ($weatherDisplayPlaceStorage
      ->loadMultiple($result) as $location) {
      $rows[] = [
        Link::createFromRoute($location->displayed_name->value, 'weather.user.weather_display_place.edit_form', [
          'user' => $user
            ->id(),
          'weather_display_place' => $location
            ->id(),
        ]),
        $location->weight->value,
      ];
      $this->renderer
        ->addCacheableDependency($output, $location);
    }
  }
  $output["#cache"]["tags"][] = 'weather_display:' . $user
    ->id();

  // Insert link for adding locations into the table as last row.
  $rows[] = [
    [
      'data' => Link::createFromRoute($this
        ->t('Add location to this display'), 'weather.user.weather_display_place.add_form', [
        'user' => $user
          ->id(),
      ]),
      'colspan' => 2,
    ],
  ];
  $output['table'] = [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  ];

  // Generate link to Add or Edit user's weather display.
  $url = Url::fromRoute('weather.user.weather_display.add_form', [
    'user' => $user
      ->id(),
  ]);
  $output['edit_display'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Edit configuration of display'),
    '#url' => $url,
  ];
  return $output;
}