View source
<?php
namespace Drupal\weather\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
use Drupal\weather\Entity\WeatherDisplayInterface;
use Drupal\weather\Service\HelperService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class WeatherUserConfiguredDisplayController extends ControllerBase {
protected $weatherHelper;
protected $entityTypeManager;
protected $renderer;
public function __construct(HelperService $weather_helper, EntityTypeManagerInterface $entity_type_manager, Renderer $renderer) {
$this->weatherHelper = $weather_helper;
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('weather.helper'), $container
->get('entity_type.manager'), $container
->get('renderer'));
}
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();
$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,
];
$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;
}
public function access(AccountInterface $account, UserInterface $user) {
return AccessResult::allowedIf($user
->id() == $account
->id() && $account
->hasPermission('administer custom weather block'));
}
}