DomainAccessContent.php in Domain Access 8
File
domain_access/src/Plugin/views/access/DomainAccessContent.php
View source
<?php
namespace Drupal\domain_access\Plugin\views\access;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Plugin\views\access\AccessPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Drupal\domain\DomainStorageInterface;
use Drupal\domain_access\DomainAccessManagerInterface;
use Drupal\user\UserStorageInterface;
class DomainAccessContent extends AccessPluginBase implements CacheableDependencyInterface {
protected $usesOptions = FALSE;
protected $permission = 'publish to any assigned domain';
protected $allPermission = 'publish to any domain';
protected $domainStorage;
protected $userStorage;
protected $manager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, DomainStorageInterface $domain_storage, UserStorageInterface $user_storage, DomainAccessManagerInterface $manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->domainStorage = $domain_storage;
$this->userStorage = $user_storage;
$this->manager = $manager;
}
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')
->getStorage('domain'), $container
->get('entity_type.manager')
->getStorage('user'), $container
->get('domain_access.manager'));
}
public function summaryTitle() {
return $this
->t('Domain editor');
}
public function access(AccountInterface $account) {
if ($account
->hasPermission($this->allPermission)) {
return TRUE;
}
if (isset($this->view->element['#arguments'])) {
foreach ($this->view->element['#arguments'] as $value) {
if ($domain = $this->domainStorage
->load($value)) {
break;
}
}
}
if (!empty($domain)) {
return $this->manager
->hasDomainPermissions($account, $domain, [
$this->permission,
]);
}
return FALSE;
}
public function alterRouteDefinition(Route $route) {
if ($domains = $this->domainStorage
->loadMultiple()) {
$list = array_keys($domains);
}
$list[] = 'all_affiliates';
$route
->setRequirement('_domain_access_views', (string) implode('+', $list));
$route
->setDefault('domain_permission', $this->permission);
$route
->setDefault('domain_all_permission', $this->allPermission);
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
return [
'user',
];
}
public function getCacheTags() {
return [];
}
}