You are here

public function RouteSubscriber::alterRoutes in Block Content Permissions 8

Alters existing routes for a specific collection.

Parameters

\Symfony\Component\Routing\RouteCollection $collection: The route collection for adding routes.

Overrides RouteSubscriberBase::alterRoutes

File

src/Routing/RouteSubscriber.php, line 24

Class

RouteSubscriber
Listens to the dynamic route events.

Namespace

Drupal\block_content_permissions\Routing

Code

public function alterRoutes(RouteCollection $collection) {

  // Change access callback for the block content type pages.
  $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',
      ]);

      // Remove required "administer blocks" permission.
      $this
        ->removePermissionRequirement($route);
    }
  }

  /* Change access callback for the block content collection page. */

  /* "entity.block_content.collection" route name does not work. */

  // Change access and controller callback for the block content add page.
  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');

    // Remove required "administer blocks" permission.
    $this
      ->removePermissionRequirement($route);
  }

  // Change access callback for the block content add forms.
  if ($route = $collection
    ->get('block_content.add_form')) {
    $route
      ->addRequirements([
      '_custom_access' => $this->accessControlHandlerClassName . '::blockContentAddFormAccess',
    ]);

    // Remove required "administer blocks" permission.
    $this
      ->removePermissionRequirement($route);
  }
}