【发布时间】:2020-07-02 16:58:09
【问题描述】:
我为 Drupal 8.9 制作了一个模块
https://git.drupalcode.org/sandbox/zenimagine-3076032/-/tree/master
在我的/user/ID/tasks 页面上出现此错误:
网站遇到意外错误。请稍后再试。 TypeError:参数 1 传递给 Drupal\Core\Access\AccessResult::allowedIfHasPermission() 必须 实现接口 Drupal\Core\Session\AccountInterface,字符串 给定的,调用的 /var/www/www-example-com/web/modules/custom/task_notify/src/Controller/TaskNotifyUserController.php 在第 19 行 Drupal\Core\Access\AccessResult::allowedIfHasPermission() (第 116 行 核心/lib/Drupal/Core/Access/AccessResult.php)。
我在模块中做错了什么?
<?php
namespace Drupal\task_notify\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
class TaskNotifyUserController extends ControllerBase {
public function Tasks() {
return [
'#theme' => 'task_notify_user_template',
];
}
public function taskAccess(AccountInterface $account) {
return AccessResult::allowedIf($account->id() == $this->currentUser()->id())
->orIf(AccessResult::allowedIfHasPermission('administer users'));
}
}
【问题讨论】:
-
请将代码粘贴到问题中。很多人不会关注链接,但是他们可能看起来很表面
-
不应将链接粘贴到您的代码,而应将您的代码粘贴到此处 - 这样它可能会得到更好的响应。
-
第一个参数需要是
AccessResult::allowedIfHasPermission($account, 'administer users')中的$account对象 -
@baikho 我不明白我应该做什么
标签: php drupal drupal-8 drupal-modules