You are here

class RouteSubscriber in Block Content Permissions 8

Listens to the dynamic route events.

Hierarchy

  • class \Drupal\Core\Routing\RouteSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RouteSubscriber

1 string reference to 'RouteSubscriber'
block_content_permissions.services.yml in ./block_content_permissions.services.yml
block_content_permissions.services.yml
1 service uses RouteSubscriber
block_content_permissions.route_subscriber in ./block_content_permissions.services.yml
Drupal\block_content_permissions\Routing\RouteSubscriber

File

src/Routing/RouteSubscriber.php, line 12

Namespace

Drupal\block_content_permissions\Routing
View source
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * The AccessControlHandler class name.
   *
   * @var string
   */
  private $accessControlHandlerClassName = 'Drupal\\block_content_permissions\\AccessControlHandler';

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

  /**
   * Remove required "administer blocks" permission from route.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The Route object.
   */
  private function removePermissionRequirement(Route $route) {
    if ($route
      ->hasRequirement('_permission')) {
      $requirements = $route
        ->getRequirements();
      unset($requirements['_permission']);
      $route
        ->setRequirements($requirements);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$accessControlHandlerClassName private property The AccessControlHandler class name.
RouteSubscriber::alterRoutes public function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
RouteSubscriber::removePermissionRequirement private function Remove required "administer blocks" permission from route.
RouteSubscriberBase::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. 5
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1