3

The site I am working on has a lot of Anonymous nodes because it is a migration from a system that didn't have a proper user system. Users can claim their articles by going in and editing the node and changing the system author, but I'd like that to happen by default whenever someone (preferably someone who is not the administrator) edits a node that has the anonymous author. I'm sure it must be imminently doable through some form alter or form preprocess hook, but I don't quite get the drupal form system yet. Solutions and pointers to good sources of information both welcome.

UPDATE I refined this question here: How can I set a default node owner for anonymous nodes on the node edit form?

3 Answers 3

2

You can use hook_form_alter() function like:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  // If edit page is node
  if (isset($form['#node_edit_form'])) {
    $form['actions']['submit']['#submit'][] = 'mymodule_node_edit_submit';
  }
}


/**
 * Implements submit function for node edit.
 */
function mymodule_node_edit_submit($form, &$form_state) {
   // Get node id
   $nid = $form_state['values']['nid'];
   // Get current user
   $current_uid = $form_state['values']['uid'];
   // Load node if owner is anonymous
   if($current_uid == 0){
     // Get logged in user id to append uid as owner to node.
     global $user;
     $node = node_load($nid);
     // Set owner id as logged in user
     $node->uid = $user->uid;
     // Save node
     node_save($node);  
    }
}
7
  • Thanks, I like what you are doing there, but is there any way to do it so that the fact that the user will become the author shows in the form? (In other words perform the alter not at the save stage, but when the form is loaded so that the user can change it if they want to)
    – UltraBob
    Commented Nov 7, 2016 at 6:19
  • You want user to change the author or while edit the node (before saving) you want to change node owner?
    – Ajay Reddy
    Commented Nov 7, 2016 at 6:22
  • I want to set the default node owner to be the current user when the user edits a node currently owned by anonymous. I would like the user to be able to change that if they want to (set it back to blank, or set it to a different user).
    – UltraBob
    Commented Nov 7, 2016 at 6:24
  • Updated answer But if any user wants to change node owner manually, that user need permissions to "Administer content" which is not good. Else need to write one custom field for change author and append it in save function.
    – Ajay Reddy
    Commented Nov 7, 2016 at 6:31
  • I don't see the updated answer, but I use the override node options module to allow the appropriate role to be able to change the author.
    – UltraBob
    Commented Nov 7, 2016 at 7:06
3

Option 1 - Adapt node author after editing it

If you have the Rules module enabled, you can simply use a rule that looks like so:

{ "rules_change_anonymous_user_to_current_user" : {
    "LABEL" : "Change anonymous user to current user",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "node-unchanged:author:uid" ], "value" : "0" } },
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "2" : "2" } }
        }
      },
      { "NOT user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "3" : "3" } }
        }
      }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "node:author" ], "value" : [ "site:current-user" ] } },
      { "drupal_message" : {
          "message" : "Be aware: this was a node with an anonymous author. Since you just edited it, you are now considered as the author of it.",
          "type" : "warning"
        }
      }
    ]
  }
}

After you enable the Rules UI, you should be able to import this rule in your own site.

Just in case it's not clear what this rule does:

  • Rules Event: after updating existing content.
  • Rules Conditions:
    • Author of node being edited has uid=0.
    • User editing the node is logged in.
    • User editing the node does NOT have role "administrator".
  • Rules Actions:
    • Set node author to current user.
    • Display a (warning) message like "Be aware: this was a node with an anonymous author. Since you just edited it, you are now considered as the author of it.".

Done! ... with Option 1.

Option 2 - Adapt node author after flagging it (instead of editing it)

As per your requirement like "...whenever someone (preferably someone who is not the administrator) edits a node that has the anonymous author ...") the previous option has a disadvantage I think: the node has to be edited. But what if the node content is actually fine, and does not need any edits at all? Therefor I'd recommend a possible alternative, which you either use together with the previous option, or as a replacement of it. Read on for more details ...

You could also use the Flag module to create a (non-global) flag that are allowed to be used by authenticated users. Let's say you define a flag labeled like "I am the author", with machine name "i_am_the_author". With such flag in place, you can use a variation of the rule from Option 1, which looks like so:

{ "rules_change_anonymous_user_to_flagging_user" : {
    "LABEL" : "Change anonymous user to flagging user",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "flag" ],
    "ON" : { "flag_flagged_i_am_the_author" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "flagged-node:author:uid" ], "value" : "0" } },
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "2" : "2" } }
        }
      },
      { "NOT user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "3" : "3" } }
        }
      }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "flagged-node:author" ], "value" : [ "site:current-user" ] } },
      { "drupal_message" : {
          "message" : "Be aware: this was a node with an anonymous author. Since you just flagged it, you are now considered as the author of it.",
          "type" : "warning"
        }
      }
    ]
  }
}

After you enable the Rules UI, you should be able to import this rule in your own site.

Just in case it's not clear what this rule does:

  • Rules Event: after a node has been flagged, under "I am the author".
  • Rules Conditions:
    • Author of node being flagged has uid=0.
    • User flagging the node is logged in.
    • User flagging the node does NOT have role "administrator".
  • Rules Actions:
    • Set node author to current user.
    • Display a (warning) message like "Be aware: this was a node with an anonymous author. Since you just flagged it, you are now considered as the author of it.".

Done! ... with Option 2.

Option 3 - Heading for the golden cradle

If you'd implement Option 2 (using the "I am the author" flag), you could even replace the rule from Option 1 by a variation of it, which is that you simply disallowed edit attempts of nodes that (still) have an anonymous user. Here is a summary of how such rule could look like:

  • Rules Event : Drupal is initializing.
  • Rules Condition: Current path is like /node/nid/edit (using a regular expression).
  • Rules actions:
    • Perform a redirect (to any path that fits, eg some help page to explain the reason for the direct).
    • Set a Drupal message to indicate that edits are only allowed by a user who flagged the node first by ... you guessed it ... the "I am the author" flag.

Voilà!

Option 4 - Resolving node author disputes

No matter which solution (option) you'd use, there is always a chance that multiple users might claim to be the node author.

To also take that scenario into consideration, you could create a variation of the rule from Option 2. Some details about such rule:

  • Replace 1st Rules Condition by "node author of flagged node is NOT uid=0.
  • Replace both Rules Actions with sending an eMail to (eg) the site administrator, to indicate that some type of conflict about a node author came up (and adapt the message being shown accordingly).

Game over!?!?

11
  • I seem to have done a poor job of explaining what I want to accomplish because all three answers have started out helping me automatically set the author to be the current user. I'll edit my question to make it clearer, and I apologize for the lack of clarity, but what I want is to set the default value in the node edit form. I want the user to still have the choice of who the author is set to, but I want it to be set to them unless they explicitly choose to not have that happen.
    – UltraBob
    Commented Nov 7, 2016 at 7:34
  • Hey @UltraBob : changing the question after it has been answered (3 times) is not really "acceptable" on this site (since it invalidates all existing answers ...). How about instead posting a folowup question (with reference to this one if you want), and content of the new question like your prior comment? Commented Nov 7, 2016 at 7:37
  • The thing is, my original question did say that I wanted it to happen by default, which implies that there is an option to change it. It just seems not to have been prominent enough for enough people to pick up on. I had already saved my edit before your comment came in so please have a look at it and see if you think it is an unfair edit. I'd be willing to make a new question if it is necessary, but I think my original question was asking what my new edit is asking. Just in less clear fashion.
    – UltraBob
    Commented Nov 7, 2016 at 7:41
  • Which means, that I think if your rules solution had some way of asking the user to confirm whether they wanted to change the author when they saved, your solution would be valid even though it is not really exactly what I had in mind.
    – UltraBob
    Commented Nov 7, 2016 at 7:42
  • @UltraBob I wanted it to happen by default, which implies that there is an option to change it. There is no way anyone could've guessed that.
    – No Sssweat
    Commented Nov 7, 2016 at 9:34
1

You can use hook_node_update

/**
 * Implements hook_node_update().
 */
function hook_node_update($node) {
   global $user;

     //administrator role id
     $admin_rid = 3;

     //To check if user is not admin
     if (!array_key_exists( $admin_rid , $user->roles)) {
         $node->uid = $user->uid;
         node_save($node);
     }
}
1
  • Thanks, but what I'm trying to do it present a default value in the node edit form. If the current owner is anonymous, set the default value for owner to be the current user, but allow the user the opportunity to change that if they want to.
    – UltraBob
    Commented Nov 7, 2016 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.