Problem/Motivation
Found a lot of errors below in the database log on one of our sites.
Error: Call to a member function access() on null in Drupal\comment\CommentAccessControlHandler->checkAccess() (line 40 of /var/www/html/web/core/modules/comment/src/CommentAccessControlHandler.php) #0 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityAccessControlHandler.php(105): Drupal\comment\CommentAccessControlHandler->checkAccess(Object(Drupal\comment\Entity\Comment), 'view', Object(Drupal\Core\Session\AccountProxy)) #1 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php(709): Drupal\Core\Entity\EntityAccessControlHandler->access(Object(Drupal\comment\Entity\Comment), 'view', Object(Drupal\Core\Session\AccountProxy), false) #2 /var/www/html/web/core/modules/content_translation/content_translation.module(375): Drupal\Core\Entity\ContentEntityBase->access('view') #3 /var/www/html/web/core/modules/comment/src/CommentLazyBuilders.php(231): content_translation_translate_access(Object(Drupal\comment\Entity\Comment)) #4 /var/www/html/web/core/modules/comment/src/CommentLazyBuilders.php(212): Drupal\comment\CommentLazyBuilders->access(Object(Drupal\comment\Entity\Comment))
Code related
case 'view':
$access_result = AccessResult::allowedIf($account->hasPermission('access comments') && $entity->isPublished())->cachePerPermissions()->addCacheableDependency($entity)
->andIf($entity->getCommentedEntity()->access($operation, $account, TRUE));
if (!$access_result->isAllowed()) {
$access_result->setReason("The 'access comments' permission is required and the comment must be published.");
}
return $access_result;
The culprit line:
->andIf($entity->getCommentedEntity()->access($operation, $account, TRUE));
The reason is that the Commented Entity($entity->getCommentedEntity()
) is NULL if the comment is an orphan.
/**
* Returns the entity to which the comment is attached.
*
* @return \Drupal\Core\Entity\FieldableEntityInterface|null
* The entity on which the comment is attached or NULL if the comment is an
* orphan.
*/
public function getCommentedEntity();
Proposed resolution
Break the line 39 and 40 -- one statement, into two parts, and check if the Commented Entity is NULL
switch ($operation) {
case 'view':
- $access_result = AccessResult::allowedIf($account->hasPermission('access comments') && $entity->isPublished())->cachePerPermissions()->addCacheableDependency($entity)
- ->andIf($entity->getCommentedEntity()->access($operation, $account, TRUE));
+ $access_result = AccessResult::allowedIf($account->hasPermission('access comments') && $entity->isPublished())->cachePerPermissions()->addCacheableDependency($entity);
+ $commented_entity = $entity->getCommentedEntity();
+ if ($commented_entity !== NULL) {
+ $access_result = $access_result->andIf($commented_entity->access($operation, $account, TRUE));
+ }
if (!$access_result->isAllowed()) {
$access_result->setReason("The 'access comments' permission is required and the comment must be published.");
}
Remaining tasks
User interface changes
No
API changes
No
Data model changes
No
Release notes snippet
No