I have a one-time operation that I'd like to apply to a selection of a few hundred nodes. So, I'm thinking this looks like something Views Bulk Operations can deal with, and I'm sure it can.

The specific task is to update a couple of CCK fields in that collection of nodes. So, how do I code the body of an "Execute arbitrary PHP script" to do this?

In my first attempt I did this... but it failed to do anything more than confirm what the elements of $object look like.

print_r( $object );
$object->field_profile_courtesy_title = "Ms.";
node_save( $object );

Anybody here able to put me on the right path again? Thanks.

Comments

Summitt Dweller’s picture

Like any "Action" the "arbitrary PHP script" needs to act on the database directly in order to make changes. Even though $object is passed by reference (so it's writable...this really had me confused) there's apparently nothing going on downstream to save any changes you make to $object.

What I ended up coding was something simple like...

db_query( "UPDATE table SET field='%s' WHERE ( nid=%d AND vid=%d )", $new_value, $object->nid, $object->vid );

Worked nicely.

infojunkie’s picture

If the field_profile_courtesy_title is a CCK field, then you should probably set it as such:

$object->field_profile_courtesy_title[0]['value'] = "Ms.";

because that's the way CCK usually handles its fields.

traviss359’s picture

I was able to get this to work by loading my own node object using the node_load function and the $object->nid. Here is the code I placed in the PHP script to update the file description for a CCK filefield.

$new_node = node_load($object->nid);
$new_node->field_form_file[0]['data']['description'] = $object->title;
node_save($new_node);

Thanks for asking the question in the first place though and posting your own solution. It certainly helped me find this solution.

sgriffin’s picture

$new_node = node_load($object->nid);
$new_node->field_form_file[0]['data']['description'] = $object->title;
node_save($new_node);

$object is the node

you only need to do

$object->field_form_file[0]['data']['description'] = $object->title;
node_save($object);
krak’s picture

This code not worked in Drupal 7 with additional fields

infojunkie’s picture

In D7, you need to use the Field API.

krak’s picture

Can you write simple example?

jim_snydergrant’s picture

some working D7 examples here: http://drupal.org/node/1000200

tomvolek’s picture

I needed to do the same thing, i.e. execute PHP snippet in VBO. I finally figured it out, thanks to this thread and it is working just fine.

For beginners, install VBO module, then go to your view, build it. Under basic setting set the Style to "Bulk Operations" Then you can choose selected operations you want the VBO to do for you.

Now my question: is there a way to make the php snippet to stick so I dont have to enter it every time.
I eventually like to hide the php form box so my admin user wont see it.

Thanks
Tom

infojunkie’s picture

To make your script sticky on D6:
* go to admin/settings/actions/manage
* find the "Make a new advanced action available" downdown
* select the "Execute arbitrary PHP script..." action
* click "Create"
* In the new page, rename the action to something specific and write your script
* click "Save"
* go back to your VBO admin page
* select the new action you just created

When you execute this action, the PHP form will no longer appear because it's using the script you entered.

Hope this helps!

tomvolek’s picture

Thanks very much infojunkie . Your comment was god sent. Spent a trying to figure the last part.

Thanks
Tom

stefank’s picture

Is the process in drupal 7 similar?not sure how to add the action to vbo. Any suggestions?
Many Thanks

balagan’s picture

I would try to use the Rules module

functions’s picture

You can add the action in the settings for the VBO field.

functions’s picture

Thank you!