WorkbenchAccessSections.php in Workbench Access 8
File
src/Controller/WorkbenchAccessSections.php
View source
<?php
namespace Drupal\workbench_access\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Link;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
use Drupal\workbench_access\RoleSectionStorageInterface;
use Drupal\workbench_access\UserSectionStorageInterface;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkbenchAccessSections extends ControllerBase implements ContainerInjectionInterface {
protected $manager;
protected $roleSectionStorage;
protected $userSectionStorage;
public function __construct(WorkbenchAccessManagerInterface $manager, RoleSectionStorageInterface $role_section_storage, UserSectionStorageInterface $user_section_storage) {
$this->manager = $manager;
$this->roleSectionStorage = $role_section_storage;
$this->userSectionStorage = $user_section_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.workbench_access.scheme'), $container
->get('workbench_access.role_section_storage'), $container
->get('workbench_access.user_section_storage'));
}
public function page(AccessSchemeInterface $access_scheme) {
$rows = [];
$tree = $access_scheme
->getAccessScheme()
->getTree();
foreach ($tree as $id => $data) {
foreach ($data as $item_id => $item) {
$editor_count = count($this->userSectionStorage
->getEditors($access_scheme, $item_id));
$role_count = count($this->roleSectionStorage
->getRoles($access_scheme, $item_id));
$row = [];
$row[] = str_repeat('-', $item['depth']) . ' ' . $item['label'];
$row[] = Link::fromTextAndUrl($this
->t('@count editors', [
'@count' => $editor_count,
]), Url::fromRoute('entity.access_scheme.by_user', [
'access_scheme' => $access_scheme
->id(),
'id' => $item_id,
]));
$row[] = Link::fromTextAndUrl($this
->t('@count roles', [
'@count' => $role_count,
]), Url::fromRoute('entity.access_scheme.by_role', [
'access_scheme' => $access_scheme
->id(),
'id' => $item_id,
]));
$rows[] = $row;
}
}
return [
'#type' => 'table',
'#header' => [
$access_scheme
->getPluralLabel(),
$this
->t('Editors'),
$this
->t('Roles'),
],
'#rows' => $rows,
'#empty' => $this
->t('No sections are available.'),
];
}
}