0

I'm using Drupal 7 + Rules. I would like to create a rule that unpublishes all nodes authored by a user when they have been given a particular role.

  • EVENT - After updating an existing user account
  • CONDITION - User has role(s): SelectedRole
  • ACTION - ???

BONUS: If this could be limited to nodes of a certain type, that would be even better.

If there is a better way outside of Rules to do this, I'm open to other ideas.

Thanks so much!

areikiera
  • 35
  • 4

1 Answers1

1

You can create a custom ruleset to loop through the nodes or a Views Bulk Operation action.

An easiest option is to add a custom PHP function on your rule (PHP > Execute custom PHP code). Of course you have to enable the php filter core module if you didn't already.

In the PHP action you have to get all the nids of the published nodes the current user and loop through them to unpublish them. I will use the EntityFieldQuery API class but you can use the database functions also.

// Get updated user id    
$uid = $account -> uid;

// Get all nodes from user that are of NODE_TYPE and are published
$query = new EntityFieldQuery();
$query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'NODE_TYPE')
  ->propertyCondition('status', 1)
  ->propertyCondition('uid', $uid);

$result = $query->execute();
$nids = array_keys($result['node']);

// Load all nodes in one go for better performance.
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
  // set status property to 0 (unpublished)
  $node->status = 0;
  // re-save the node
  node_save($node);
}

I would also suggest to add one more Condition for the user before the one you are using: User Has Roles: (NOT) SelectedRole so that the action do not run everytime the user profile is updated.

References:

Community
  • 1
  • 1
TheodorosPloumis
  • 2,396
  • 1
  • 17
  • 31
  • This worked perfectly! Thank you! I have a follow-up question for you, if you don't mind. Instead of implementing this in a rule, I'm creating a custom module with it. I had to add a bit to the user area to get the $account variables, but I'm getting an error "Undefined index: node" for this line "$nids = array_keys($result['node']);". Do you know what I need to do to define 'node'? – areikiera Sep 12 '13 at 15:07
  • The query is the same for every approach (module, rules etc). I think the error is because you do not get the $account variable. Print render the $account to see if I am correct. – TheodorosPloumis Sep 12 '13 at 21:22
  • Might be the case. My module works with the Recurly module and the Recurly processes appear to be happening behind the scenes, so I'm having trouble printing the $account variable. However, it's unpublishing the right node(s) authored by the user with the processed uid (gotten by $uid = $account->uid;), so I assumed that meant that the $account variable is available. On the other hand, I'm getting a "Trying to get property of non-object" notice in the dblog on a line "$account = user_load($account->entity_id);". Are you available for freelance/debugging work? – areikiera Sep 12 '13 at 22:54
  • Looks like your code was working perfectly. I'd added some code to redirect and display a message after the role had been assigned which wasn't working. I'll post that as a separate question if I can't troubleshoot it myself. Again, thank you so much! – areikiera Sep 13 '13 at 15:02