Problem/Motivation

On my website, when I go to the revisions for a node:
https://www.example.org/node/2959/revisions/view/38665/39409/split_fields

We get the following error

Call to a member function getDisplayName() on null
in Drupal\diff\DiffLayoutBase->buildRevisionData()
line 182 of modules/contrib/diff/src/DiffLayoutBase.php

The problem is that $revision->getRevisionUser() is returning null.

This is a probably a case of bad data on my site, but I attach my patch here in case someone finds it useful.

The fix is simply to do a check for null:

$revision_link['author'] = [
  '#type' => 'link',
  '#title' => ($revision_user = $revision->getRevisionUser()) ? $revision_user->getDisplayName() : '',

Issue fork diff-3206057

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

michaellenahan created an issue. See original summary.

michaellenahan’s picture

Issue summary: View changes

michaellenahan’s picture

StatusFileSize
new670 bytes

Adding the patch manually here, in addition to the Merge Request.

We're using cweagans/composer-patches in our composer.json - so we will use the url https://www.drupal.org/files/issues/2021-03-27/3206057-4.patch in our composer.json

A method for using gitlab merge requests for patch management is still being worked on here - https://www.drupal.org/project/drupalorg/issues/3204538

michaellenahan’s picture

Status: Active » Needs review
mingsong’s picture

StatusFileSize
new1.52 KB

In the revisions table, the user name for NULL is 'Anonymous (not verified)'. So I suggest using the same name rather than '' for the consistency.

Here is the patch.

I am happy to push it to the PR created for this issue. Just let me know what you think.

mingsong’s picture

StatusFileSize
new1.47 KB

Sorry, patch from #6 was failed from the test.
Here is the new one.

michaellenahan’s picture

I am happy to push it to the PR created for this issue. Just let me know what you think.

Yes, that's a good improvement I think.

mingsong’s picture

Unfortunately, I don't have the access to push code to the repository.

I attach the git diff code here, in case you want to push it back to your repository.

Author: AMDS <AMDS@2986445.no-reply.drupal.org>
Date:   Sat Apr 3 11:02:58 2021 +1100

    Issue #3206057: Call to a member function getDisplayName() on null

diff --git a/src/DiffLayoutBase.php b/src/DiffLayoutBase.php
index e53c32e..b05ca73 100644
--- a/src/DiffLayoutBase.php
+++ b/src/DiffLayoutBase.php
@@ -167,7 +167,17 @@ abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface,
   protected function buildRevisionData(ContentEntityInterface $revision) {
     if ($revision instanceof RevisionLogInterface) {
       $revision_log = Xss::filter($revision->getRevisionLogMessage());
+      $revision_user = $revision->getRevisionUser();
       $user_id = $revision->getRevisionUserId();
+      // Get the revision user name.
+      // For historical reason, the revision user might be NULL.
+      // If it is the case, use 'Anonymous' as the user name.
+      if ($revision_user) {
+        $user_name = $revision_user->getDisplayName();
+      }
+      else {
+        $user_name = $this->t('Anonymous (not verified)');
+      }
 
       $revision_link['date'] = [
         '#type' => 'link',
@@ -179,10 +189,10 @@ abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface,
 
       $revision_link['author'] = [
         '#type' => 'link',
-        '#title' => ($revision_user = $revision->getRevisionUser()) ? $revision_user->getDisplayName() : '',
+        '#title' => $user_name,
         '#url' => Url::fromUri(\Drupal::request()->getUriForPath('/user/' . $user_id)),
         '#theme' => 'username',
-        '#account' => $revision->getRevisionUser(),
+        '#account' => $revision_user,
         '#prefix' => '<div class="diff-revision__item diff-revision__item-author">',
         '#suffix' => '</div>',
       ];

mingsong’s picture

My contribute module to compare revisions for entity types other than Node.

https://www.drupal.org/project/entity_diff_ui

This module fixes this issue too.

martijn de wit’s picture

StatusFileSize
new1.45 KB

Patch from #7 doens't apply anymore to 8.x-1.1 / 8.x-1.dev

Made new patch

martijn de wit’s picture

Usernames can be empty due cancelation. We had this at certain entities.

This is still a issue with media entities, see: #3043725: Provide a Entity Handler for user cancelation

adriancid’s picture

Status: Needs review » Reviewed & tested by the community

This fixed the issue for me on a project I'm working now.

caspervoogt’s picture

#11 worked for me

gngn’s picture

#11 worked for me too.

In my case the revision user had been deleted (so getDisplayName() was called on null).

acbramley’s picture

Version: 8.x-1.0 » 8.x-1.x-dev
Status: Reviewed & tested by the community » Closed (duplicate)

mvnovick’s picture

The problem with anonymous user still persists, and patch #11 still works in Drupal 10.3.

opi’s picture

Use -dev version of diff module solve the same issue for me. The fix from #3167126 is commited but no release has been made.

acbramley’s picture

@opi or upgrade to Diff v2 :) 8.x-1.x is essentially obselete