Frage

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!

War es hilfreich?

Lösung

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:

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top