首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态本地任务-缓存问题

动态本地任务-缓存问题
EN

Drupal用户
提问于 2018-11-26 18:51:05
回答 2查看 1.4K关注 0票数 3

根据自定义实体的某些设置,我尝试创建一些本地二级任务。但不幸的是,有时这些本地任务不会出现--它们在重新构建缓存和第一级选项卡时再次出现,而不是这些动态创建的第二级选项卡。

我所做的(实体类型名称是"parliament_period"):

在mymodule.links.task.yml中

代码语言:javascript
复制
entity.parliament_period.canonical:
  route_name: entity.parliament_period.canonical
  base_route: entity.parliament_period.canonical
  title: 'Overview'

entity.parliament_period.edit_form:
  route_name: entity.parliament_period.edit_form
  base_route: entity.parliament_period.canonical
  title: 'Edit'

entity.parliament_period.local_tasks:
  deriver: 'Drupal\mymodule\Plugin\Derivative\ParliamentPeriodDynamicLocalTasks'
  weight: 100

这门课看上去如下:

代码语言:javascript
复制
class ParliamentPeriodDynamicLocalTasks extends DeriverBase  implements ContainerDeriverInterface {

  /**
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected $currentRouteMatch;


  /**
   * @var ParliamentPeriod|null
   */
  protected $parliamentPeriod;

  /**
   * DynamicLocalTasks constructor.
   *
   * @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match
   */
  public function __construct(CurrentRouteMatch $current_route_match) {
    $this->currentRouteMatch = $current_route_match;
    $this->parliamentPeriod = $this->currentRouteMatch->getParameter('parliament_period');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static(
      $container->get('current_route_match')
    );
  }



  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    if (!$this->parliamentPeriod instanceof ParliamentPeriod) {
      return [];
    }

    // Implement dynamic logic to provide values for the same keys as in example.links.task.yml.
    $this->derivatives['entity.parliament_period.default']['title'] = 'Details';
    $this->derivatives['entity.parliament_period.default']['provider'] = $base_plugin_definition["provider"];
    $this->derivatives['entity.parliament_period.default']['route_name'] = 'entity.parliament_period.canonical';
    $this->derivatives['entity.parliament_period.default']['parent_id'] = "entity.parliament_period.canonical";


    if ($this->parliamentPeriod->isElection()) {
      $this->derivatives['entity.parliament_period.candidacies']['title'] = "Candidacies";
      $this->derivatives['entity.parliament_period.candidacies']['provider'] = $base_plugin_definition["provider"];
      $this->derivatives['entity.parliament_period.candidacies']['route_name'] = 'entity.parliament_period.candidacies';
      $this->derivatives['entity.parliament_period.candidacies']['parent_id'] = "entity.parliament_period.canonical";


      $this->derivatives['entity.parliament_period.constituencies']['title'] = "Constituencies";
      $this->derivatives['entity.parliament_period.constituencies']['provider'] = $base_plugin_definition["provider"];
      $this->derivatives['entity.parliament_period.constituencies']['route_name'] = 'entity.parliament_period.constituencies';
      $this->derivatives['entity.parliament_period.constituencies']['parent_id'] = "entity.parliament_period.canonical";
    }

    if ($this->parliamentPeriod->isLegislature()) {
      $this->derivatives['entity.parliament_period.committees']['title'] = "Committees";
      $this->derivatives['entity.parliament_period.committees']['provider'] = $base_plugin_definition["provider"];
      $this->derivatives['entity.parliament_period.committees']['route_name'] = 'entity.parliament_period.committees';
      $this->derivatives['entity.parliament_period.committees']['parent_id'] = "entity.parliament_period.canonical";

      $this->derivatives['entity.parliament_period.committees']['title'] = "Mandates";
      $this->derivatives['entity.parliament_period.committees']['provider'] = $base_plugin_definition["provider"];
      $this->derivatives['entity.parliament_period.committees']['route_name'] = 'entity.parliament_period.mandates';
      $this->derivatives['entity.parliament_period.committees']['parent_id'] = "entity.parliament_period.canonical";
    }


    return $this->derivatives;
  }

}

EN

回答 2

Drupal用户

发布于 2018-11-30 07:52:58

只有在缓存清除或当您调用

代码语言:javascript
复制
\Drupal::service('plugin.manager.menu.local_task')->clearCachedDefinitions()

对于更改的配置或内容,但不是对常规请求,这将是太慢。

完全动态的是一个LocalTaskDefault插件,您可以将它添加到yaml文件中:

mymodule.links.task.yml

代码语言:javascript
复制
mymodule.my_example_tab:
  route_name: default.static.route.name
  title: 'Default Static Title'
  base_route: example.base_route
  class: '\Drupal\mymodule\Plugin\Menu\MyLocalTask

插件类:

代码语言:javascript
复制
class MyLocalTask extends LocalTaskDefault {

  public function getRouteName() {
    return 'dynamic.route.name';
  }

  public function getTitle() {
    return 'Dynamic Title';
  }
}

这个插件不需要注释,因为它是通过yaml文件发现的。

本地任务可以同时由所有三种方法的信息组合,例如,您可以在静态yaml中定义基本路由、根据内容实体在派生中定义标题以及根据请求的动态条件在插件中定义路由名称。

票数 3
EN

Drupal用户

发布于 2019-03-02 15:13:55

用于用例的另一种方法:与其对试图评估实体的本地任务使用派生器,不如静态地添加所有本地任务。然后,对是否在实体上显示这些任务的评估将委托给其路由的访问检查。如果当前用户无法访问路由,则不会显示本地任务。

因此,您的mymodule.links.task.yml可能包含所有可能选项卡的本地任务,包括默认选项卡、候选人、选区、委员会和授权。

为了简洁起见,下面只是候选人的一个例子。您可以将其扩展到所有剩余的本地任务/路由:

代码语言:javascript
复制
entity.parliament_period.candidacies:
  route_name: entity.parliament_period.candidacies
  base_route: entity.parliament_period.canonical
  title: 'Candidacies'

在检查访问时,如果在实体注释中将本地任务的链接/表单定义为相应的操作,则可以使用自定义控制器或专用EntityAccessControlHandler

这里是一个带有自定义控制器的可扩展示例。在mymodule.routing.yml中为您的路由定义一个自定义访问回调:

代码语言:javascript
复制
entity.parliament_period.candidacies:
  path: '/my-path/{parliament_period}/candidacies'
  defaults:
    [...]
  options:
    parameters:
      parliament_period:
        type: 'entity:parliament_period'
  requirements:
    _custom_access: '\Drupal\mymodule\Controller\ParliamentPeriodController::accessCandidacies'

访问回调accessCandidacies接收与路由回调/表单完全相同的URL参数。上面的选项指定{parliament_period}参数作为自定义实体的占位符。

回调检查议会期间的条件,并返回一个符合的AccessResult对象。若要为实体缓存它,在下面的示例中仅为当前用户缓存,您可以将两者都添加为缓存上下文依赖项。

src/Controller/ParliamentPeriodController.php

代码语言:javascript
复制
currentUser();

    return AccessResult::allowedIf(
      // Your original condition.
      $parliament_period->isElection()
      // Optional check for user permissions.
      && $account->hasPermission('view parliament_period')
    )
      ->addCacheableDependency($parliament_period)
      ->cachePerUser();
  }

}

(我假设您也为您的自定义实体定义了一个接口。如果不是,则需要调整控制器。)

票数 2
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/273062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档