WorkbenchAccessBlock.php in Workbench Access 8
File
src/Plugin/Block/WorkbenchAccessBlock.php
View source
<?php
namespace Drupal\workbench_access\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkbenchAccessBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
}
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'));
}
public function build() {
$build = [];
if (($node = $this
->getContextValue('node')) && $node instanceof NodeInterface) {
$scheme_storage = $this->entityTypeManager
->getStorage('access_scheme');
if ($schemes = $scheme_storage
->loadMultiple()) {
foreach ($schemes as $id => $scheme) {
$active = $scheme
->getAccessScheme();
if ($values = $active
->getEntityValues($node)) {
foreach ($values as $value) {
$element = $active
->load($value);
$build['#theme'] = 'item_list';
$build['#items']['#title'] = $this
->t('Editorial sections:');
$build['#items'][] = $element['label'];
$build['#plain_text'] = TRUE;
}
}
}
}
}
return $build;
}
public function getCacheContexts() {
$contexts = parent::getCacheContexts();
$contexts[] = 'user.permissions';
return $contexts;
}
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIfHasPermissions($account, [
'administer workbench access',
'view workbench access information',
], 'OR');
}
}