跳转到主要内容

主页内容

Drupal9 报错:The 'entity:user' context is required and not present的两种解决办法

由 webadmin 发布于 阅读 22 次

报错信息:

The website encountered an unexpected error. Please try again later.
Drupal\Component\Plugin\Exception\ContextException: The 'entity:user' context is required and not present. in Drupal\Core\Plugin\Context\Context->getContextValue() (line 73 of core/lib/Drupal/Core/Plugin/Context/Context.php). 

报错影响:网站崩溃无法正常运行。

解决方法1:

修改/core/lib/Drupal/Core/ParamConverter/EntityConverter.php文件:

原代码:148~152行

$context_id = '@user.current_user_context:current_user';
if (isset($contexts[$context_id])) {
      $account = $contexts[$context_id]->getContextValue();
      unset($account->_skipProtectedUserFieldConstraint);
      unset($contexts[$context_id]);
}

做如下修改:

引入use Drupal\Component\Plugin\Exception\ContextException;

$context_id = '@user.current_user_context:current_user';
if (isset($contexts[$context_id])) {
      try {
        $account = $contexts[$context_id]->getContextValue();
        unset($account->_skipProtectedUserFieldConstraint);
      }catch (ContextException $e){}
      unset($contexts[$context_id]);
}
$entity = $this->entityRepository->getCanonical($entity_type_id, $value, $contexts);

参照:https://www.drupal.org/project/drupal/issues/3357354

解决方式2:

查看users表中有没有uid0的,如果没有,需要添加一个uid0的用户:

INSERT INTO users (uid, uuid, langcode)
VALUES (0, '875953e0-1f5b-4cce-9ef9-d58bef2b15f8', 'en');

执行以上sql语句添加成功后发现uid不为0,需要手动将uid改成0即可。