The code in user_node_load() function assumes the user having uid = $node->uid exists. Warnings are generated if such user is missing in 'users' table. I suggest to check user existence:

function user_node_load($nodes, $types) {
  // Build an array of all uids for node authors, keyed by nid.
  $uids = array();
  foreach ($nodes as $nid => $node) {
    $uids[$nid] = $node->uid;
  }

  // Fetch name, picture, and data for these users.
  $user_fields = db_query("SELECT uid, name, picture, data FROM {users} WHERE uid IN (:uids)", array(':uids' => $uids))->fetchAllAssoc('uid');

  // Add these values back into the node objects.
  foreach ($uids as $nid => $uid) {
    $user_exists = isset($user_fields[$uid]); // <-
    $nodes[$nid]->name = $user_exists ? $user_fields[$uid]->name : ''; // <-
    $nodes[$nid]->picture = $user_exists ? $user_fields[$uid]->picture : 0; // <-
    $nodes[$nid]->data = $user_exists ? $user_fields[$uid]->data : NULL; // <-
  }
}

Comments

maximpodorov created an issue. See original summary.

maximpodorov’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new754 bytes

Status: Needs review » Needs work

The last submitted patch, 2: D7-user_node_load_ckecks-2653342-2.patch, failed testing.

maximpodorov’s picture

Status: Needs work » Needs review
maximpodorov’s picture

So the patch passes tests now.

arun.kumar09’s picture

Hi,

Tested the patch at my end and works fine.

Revathi Manohar’s picture

hi, i have tested and it is working fine

mstrelan’s picture

StatusFileSize
new816 bytes

Alternative patch with only one conditional per loop instead of four. Should we be setting the name to variable_get('anonymous', t('Anonymous')) instead of ''?

bkosborne’s picture

Status: Needs review » Reviewed & tested by the community

Works for me and makes sense.

David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs review

Why would the users be missing given that https://api.drupal.org/api/drupal/modules%21node%21node.module/function/... removes a user's content when it is deleted?

If the issue is older sites (e.g. Drupal 6 or earlier) that already had this problem when they upgraded to Drupal 7, maybe we should think about fixing this via an update function instead - i.e. explicitly assign the orphaned nodes to the anonymous user or something like that.

bkosborne’s picture

It's a valid point. Somehow things got into a weird state where the node author was not updated for whatever reason. In my particular case it was not due to a migration path.

tamarpe’s picture

Status: Needs review » Reviewed & tested by the community

Had those warnings as well , #8 works +1

collinhaines’s picture

StatusFileSize
new670 bytes

Re-rolling for line number changes in modules/user/user.module with Drupal 7.71.

marcelovani’s picture

I found the same issue because of deleted users. This patch works.

marcelovani’s picture

Issue tags: +Drupal 7.76 target
collinhaines’s picture

StatusFileSize
new754 bytes

Re-rolling for line number changes in modules/user/user.module with Drupal 7.76. Also reverted coalesce operator from previous patch since Drupal 7.x still officially supports PHP 5.x.

marcelovani’s picture

Alternatively

$nodes[$nid]->name = isset($user_fields[$uid]->name) ? $user_fields[$uid]->name : '';
$nodes[$nid]->picture = isset($user_fields[$uid]->picture) ? $user_fields[$uid]->picture : 0;
$nodes[$nid]->data = isset($user_fields[$uid]->data) ? $user_fields[$uid]->data : NULL;
collinhaines’s picture

StatusFileSize
new760 bytes

Re-rolling for line number changes in modules/user/user.module with Drupal 7.90.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 18: user-node-load-patch-2653342-18.patch, failed testing. View results

avpaderno’s picture

Issue tags: -Drupal 7.76 target
klausi’s picture

Status: Needs work » Reviewed & tested by the community

Latest patch is still good, back to RTBC.

poker10’s picture

Status: Reviewed & tested by the community » Needs review

I think there are few important comments here, like #8 and #10, so let's discuss this a bit more. This seems to be caused by inconsistent database as normally it should not be possible to delete a user while the UID is left in the node table.

This patch seems like an easy workaround, but it can probably hide more problems - there can be other parts in code where the existence of that user is expected. I think the cleaner solution will be something like David proposed in #10, for example to update all nodes where UID is not in users table to 0 (anonymous user). However I can imagine this can have a pretty big impact on some sites with lot of content referencing non-existing users. On the other hand, I would not expect to have an empty username in any case (I would expect either Anonymous, or user name). So moving this to Needs Review for some additional discussion. Thanks.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.