Updating new Drupal fields with default values

Tech Notes

I love Views Bulk Operations and in particular Admin Views. So many tedious tasks are simplied with this wonderful pair of Drupal modules.

Often in a Drupal build I need to add a new field late in the content filling piece. Sometimes I need to back fill all the exisitng nodes with default values. There are a bunch of ways to do this with scripts and SQL queries but as I often install Admin Views I can take advantage of that and its dependency Views Bulk Operations.

My set up.

I have a new options list field called Availability. Its a single on off checkbox. But all my existing nodes don't yet have a row in the database for it. I need to set all the missing ones to `unticked` (or zero) in the database.

Procedure

  1. If I'm performing a large batch operation on the database I'll always backup the database before I begin. 
  2. Download, install and enable admin_views, views_bulk_operations and dependencies entity and ctools.
  3. Check that the http://example.com/admin/content listing has been overridden by admin_views
  4. Edit the view generated by admin_views at http://example.com/admin/structure/views/view/admin_views_node/edit
  5. Add any new Fields you might need in the View for it to make sense. Adding Availability is not necessary but may be helpful
  6. Add a new Filter criteria on Availability
    1. Expose the new filter so we can alter it in the content listing
    2. Also expose the operator so we can select 'None of' in the content listing
    3. And allow multiple so we can select nodes that are none of available and not available
  7. Edit the Bulk Operations (Content) field at the top of the View field list
    1. Make sure that Modify Entity Values is available and checked
    2. If not make sure that the Entity module is enabled
  8. I also alter the pagination so that too is exposed. And I set some high upper limits (50, 250 and 1000 nodes) for convenience.
  9. Save your view and return to the content listing. Now you should be able to filter on node type and exclude nodes whose availability isn't yet set. Apply that filter.
  10. Select all the rows in the filtered list by checking the top left checkbox. Optionally press `Select all rows in this view`.
  11. From the Operations select list choose `Change value` and press Execute
  12. In the following form check the Availability box from the list of Fields. Select your default value and press Next. Note: if you just want an empty line in the database you don't need to select a value: simply selecting the field will populate the database table.
  13. That should perform the operation in batches and return you to the View. If you processed all the nodes your view is probably now empty (as the filters now match no records).