Error: Call to a member function access() on null in Drupal\comment\CommentAccessControlHandler->checkAccess()

Created on 9 August 2019, over 5 years ago
Updated 25 October 2024, about 2 months ago

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

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component

comment.module

Created by

πŸ‡¨πŸ‡³China jungle Chongqing, China

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life
  • πŸ‡ΊπŸ‡ΈUnited States bradjones1 Digital Nomad Life

    It is possible this is due to a missing or broken comment type. See the just added related issue.

    I'm going to close this as won't fix/duplicate as the underlying issue appears to be either misconfiguration or an orphaned comment. Both of which can be handled in their own right.

Production build 0.71.5 2024