Associate user with multiple nodes using VBO

Events happening in the community are now at Drupal community events on www.drupal.org.
semiautomata's picture

Hi, all- I am fairly new to Drupal and very new to rules, and I am facing a problem I am certain others have faced. I need a way to update a user field on multiple nodes in one submission, on Drupal 7 using the latest version of Views Batch Operations ("VBO"). VBO can easily do this for the "author" property - that appears to be a built-in drupal action. But if I use Rules to assign a user, I don't get the nice User field drop down list with a choice of all the users like the Change Author action does. Instead I get the "Set Parameter window with various data selectors.

How do I replicate the "change author" functionality for a different, non author, but User-type field in the node?

Comments

My Solution

semiautomata's picture

To manage this, I created a new module with an "associate user" action. I basically copied the code from the node_assign_owner_action(), node_assign_owner_action_form(), node_assign_owner_action_submit(), and node_assign_owner_action_validate() from the Node module, renaming them to match the name of module and the fact that they associate rather than assign an owner. (I also had to implement hook_action_info(), obviously.)

I had to tweak node_assign_owner_action. My code is below:

/**
* Assigns a node to a volunteer.
*
* @param $node
*   A node object to modify.
* @param $context
*   Array with the following elements:
*   - 'volunteer_uid': User ID to assign to the node.
*
* @ingroup actions
*/
function node_assign_volunteer_action($node, $context) {
  $node->field_assigned_to = array( LANGUAGE_NONE => array( 0 => array('uid' => $context['volunteer_uid'] )));
  dpm($node);
  $volunteer_name = db_query("SELECT name FROM {users} WHERE uid = :uid", array(':uid' => $context['volunteer_uid']))->fetchField();
  watchdog('action', 'Assigned @type %title to uid %name.', array('@type' =>  node_type_get_name($node), '%title' => $node->title, '%name' => $volunteer_name));
}

A couple of questions to which I would love a response, because I am new to Drupal but very willing to contribute to the community:

  • What is the best way to find out about the underlying data structures of Drupal? Figuring out that I had to pack the user in in two layers of arrays was literally a trial and error thing involving judicious use of the Devel module to see how the value was set in other areas. This seems like something api.drupal.org would have, but I cannot find it anywhere.
  • In this screencast describing how to use Rules with VBO to set a field value (which doesn't work for user fields, apparently - still don't understand why), RFay mentions that that functionality will be coming "soon" to VBO. Any idea when?

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: