Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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;
}
Comment | File | Size | Author |
---|---|---|---|
#12 | 102800-4.patch | 996 bytes | montesq |
#10 | 102800-3.patch | 1.02 KB | montesq |
#8 | 102800-2.patch | 942 bytes | montesq |
#3 | 102800.patch | 943 bytes | montesq |
suggested.patch | 743 bytes | oddbit | |
Comments
Comment #1
montesq CreditAttribution: montesq commentedWhen 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...
Comment #2
oddbit CreditAttribution: oddbit commentedAlthough, 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.
Comment #3
montesq CreditAttribution: montesq commentedBy 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!
Comment #5
montesq CreditAttribution: montesq commented#3: 102800.patch queued for re-testing.
Comment #6
steus CreditAttribution: steus commentedsuggested.patch queued for re-testing.
Comment #8
montesq CreditAttribution: montesq commentedComment #10
montesq CreditAttribution: montesq commentedOK 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.
Comment #12
montesq CreditAttribution: montesq commentedI'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...
Comment #13
oddbit CreditAttribution: oddbit commentedI 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.