Posted by semiautomata on August 4, 2011 at 4:38am
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
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: