Warnings in user_node_load() for deleted users

Created on 20 January 2016, almost 9 years ago
Updated 5 December 2023, about 1 year ago

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; // <-
  }
}
πŸ› Bug report
Status

Needs review

Version

7.0 ⚰️

Component
User moduleΒ  β†’

Last updated 7 days ago

Created by

πŸ‡·πŸ‡ΊRussia maximpodorov

Live updates comments and jobs are added and updated live.
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.

Production build 0.71.5 2024