RouteSubscriber.php in Block Content Permissions 8
File
src/Routing/RouteSubscriber.php
View source
<?php
namespace Drupal\block_content_permissions\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
private $accessControlHandlerClassName = 'Drupal\\block_content_permissions\\AccessControlHandler';
public function alterRoutes(RouteCollection $collection) {
$routeNames = [
'entity.block_content_type.collection',
'block_content.type_add',
];
foreach ($routeNames as $name) {
if ($route = $collection
->get($name)) {
$route
->addRequirements([
'_custom_access' => $this->accessControlHandlerClassName . '::blockContentTypeAdministerAccess',
]);
$this
->removePermissionRequirement($route);
}
}
if ($route = $collection
->get('block_content.add_page')) {
$route
->addRequirements([
'_custom_access' => $this->accessControlHandlerClassName . '::blockContentAddPageAccess',
]);
$route
->setDefault('_controller', 'Drupal\\block_content_permissions\\Controller\\BlockContentPermissionsAddPageController::add');
$this
->removePermissionRequirement($route);
}
if ($route = $collection
->get('block_content.add_form')) {
$route
->addRequirements([
'_custom_access' => $this->accessControlHandlerClassName . '::blockContentAddFormAccess',
]);
$this
->removePermissionRequirement($route);
}
}
private function removePermissionRequirement(Route $route) {
if ($route
->hasRequirement('_permission')) {
$requirements = $route
->getRequirements();
unset($requirements['_permission']);
$route
->setRequirements($requirements);
}
}
}