I think this bug has been touched before, but it seems like it is still hanging around.
The global $user is never used, and it seems like the fallback for $nid should be the same if $node->name is not set as if it fails to load the name. No?

function node_submit($node) {
  global $user;

  // A user might assign the node author by entering a user name in the node
  // form, which we then need to translate to a user ID.
  if (isset($node->name)) {
    if ($account = user_load_by_name($node->name)) {
      $node->uid = $account->uid;
    }
    else {
      $node->uid = 0;
    }
  }

  $node->created = !empty($node->date) ? strtotime($node->date) : REQUEST_TIME;
  $node->validated = TRUE;

  return $node;
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

montesq’s picture

Status: Active » Closed (works as designed)

When you try to add a new content, you can see in the tab "authoring information" the following explanation: "Leave blank for Anonymous." (who has the uid=0)
So, the behaviour is the same when you select a missing user or leave the field empty...

oddbit’s picture

Although, the name will be an empty string in case the node is created via the node form and if the author field is left blank.
So if (isset($node->name)) can never be false. I assume that the original idea was to check sanity of auto generated nodes, because $node->name will be set as long as the field is present in the form.

In any case, the global $user is never used and there is no handling of the case when $node->name is not set.

montesq’s picture

Status: Closed (works as designed) » Needs review
FileSize
943 bytes

By design, the database sets 0 as default uid for a node. However, I agree that, we must clarify this default choice in the PHP part to be also coherent with the current behaviour (as we set uid to 0 if Drupal don't find the username).
Moreover, the global $user variable doesn't make sense in this function (as it's not used)

Please find as attached a proposition of patch. Reviewer's comments are welcomed!

Status: Needs review » Needs work

The last submitted patch, 102800.patch, failed testing.

montesq’s picture

Status: Needs work » Needs review

#3: 102800.patch queued for re-testing.

steus’s picture

suggested.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 102800.patch, failed testing.

montesq’s picture

Status: Needs work » Needs review
FileSize
942 bytes

Status: Needs review » Needs work

The last submitted patch, 102800-2.patch, failed testing.

montesq’s picture

Status: Needs work » Needs review
FileSize
1.02 KB

OK the patch failed because in some cases $node->name is not provided but $node->uid already exists (like when you force some nodes to be updated) and in this case the last patch erases uid to 0.
The new patch attached, take care of this point and should pass the tests.

Status: Needs review » Needs work

The last submitted patch, 102800-3.patch, failed testing.

montesq’s picture

Title: node_submit() fails to set author if $node->name is not set » Clarify the behavior of node_submit()
Component: node system » node.module
Category: bug » task
Priority: Normal » Minor
Status: Needs work » Needs review
FileSize
996 bytes

I've changed the title and the category because finally all work as designed...
This final patch *suggests* adding some comments and making explicit the default case where the node is assigned to Anonymous user.
Is it for 7.x or 8.x? I don't know...

oddbit’s picture

I haven't looked into the 8.x code. It might just be that this whole function is removed in favor for merging into some "set default values" function instead. But otherwise it could just be a good idea to not carry it over to yet another version.