WeatherUserDisplayBlock.php in Weather 8
File
src/Plugin/Block/WeatherUserDisplayBlock.php
View source
<?php
namespace Drupal\weather\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\weather\Entity\WeatherDisplayInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WeatherUserDisplayBlock extends BlockBase implements ContainerFactoryPluginInterface {
use WeatherDisplayBlockTrait;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, AccountProxyInterface $currentUser) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $currentUser;
$weatherDisplay = $entityTypeManager
->getStorage('weather_display')
->loadByProperties([
'type' => WeatherDisplayInterface::USER_TYPE,
'number' => $this->currentUser
->id(),
]);
$this->weatherDisplay = reset($weatherDisplay);
$this->weatherDisplayPlaceStorage = $entityTypeManager
->getStorage('weather_display_place');
$this->destination = WeatherDisplayInterface::USER_TYPE;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('current_user'));
}
public function blockAccess(AccountInterface $account, $return_as_object = FALSE) {
return AccessResult::allowedIf($this->currentUser
->hasPermission('administer custom weather block') && $this->weatherDisplay instanceof WeatherDisplayInterface);
}
}