【问题标题】:Custom permissions for route created within Views在视图中创建的路由的自定义权限
【发布时间】:2020-01-19 16:34:13
【问题描述】:

我有一个在节点页面上创建选项卡的自定义视图。我有几种内容类型,但我只希望标签显示在其中一些上。如果这是一条常规路线,我只会在要求下抛出一个 custom_access,但似乎没有办法使用在 routing.yml 文件之外创建的路线来做到这一点。

有没有合理的方法来做到这一点?

【问题讨论】:

    标签: drupal-8 drupal-views


    【解决方案1】:

    您需要创建自定义路由订阅者。文件 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
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2019-09-03
      • 1970-01-01
      相关资源
      最近更新 更多