1

On a Drupal 7 site, I have a content type that is populated with existing nodes. Later, I added a new boolean field (that is required). The existing nodes now have a blank value for this field.

How do I programmatically populate these nodes with a default value?

  • 1
    "programmatically".. do you like SQL, "raw" PHP or snippets of PHP + contrib modules better? – David Meister Aug 9 '12 at 14:57
  • I'm fairly new to drupal, so SQL or PHP + contrib modules might be better than "raw" PHP. – node9 Aug 9 '12 at 15:01
  • Ah, sorry, I g2g now. I might be able to give you something workable tomorrow. – David Meister Aug 9 '12 at 15:10
1

You may use module Views Bulk Operation. Then create views page like table and field title, your field, add field Content: Bulk operations (Bulk operations) with settings VIEWS BULK OPERATIONS -> SELECTED OPERATIONS Execute arbitrary PHP script (views_bulk_operations_script_action). Save, Open the page, select one node, choose Execute arbitrary PHP script

enter next code and execute:

//change entity title or what you want to do
$entity -> title = $entity->title . ' test';

//save entity
entity_save('node', $entity);
  • in your case $entity -> title = $entity->title . ' test'; must be like this $entity -> field_my_boolean['und'][0]['value'] = 1; for exampple – svetlio Aug 10 '12 at 6:56
0

Just in case anyone runs into this in the future: install Field Defaults.

Field Defaults allows batch updating of default field values to existing content. When adding a new field to an entity you can update all existing content with the default value or when editing an existing field you can choose to also update all existing content with the new value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.