在视图中创建的路由的自定义权限

问题描述 投票:0回答:1

我有一个自定义视图,可在节点页面上创建一个选项卡。我有几种内容类型,但我只希望选项卡显示在其中一些内容上。如果这是一条常规路线,我会根据需要抛出一个custom_access,但是似乎没有办法对在routing.yml文件外部创建的路线进行此操作。

有合理的方法吗?

drupal-8 drupal-views
1个回答
0
投票

您需要创建自定义路由订阅者。文件custom_module.services.yml:

services:
  custom_module.route_subscriber:
    class: Drupal\custom_module\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }

文件RouteSubscriber.php:

<?php
namespace Drupal\custom_module\Routing;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {
  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    if($route = $collection->get('view.<view_name>.<view_bundle>')){ // Need to change view_name and view_bundle.
      $route->setRequirement(
        '_custom_access',
        '\Drupal\custom_module\Routing\RouteSubscriber::viewsAccess'
      );
    }
  }

  public function viewsAccess() {
    return AccessResult::allowedIf(
      // Add condition when view has access
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.