Updating new Drupal fields with default values
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.
- If I'm performing a large batch operation on the database I'll always backup the database before I begin.
- Download, install and enable admin_views, views_bulk_operations and dependencies entity and ctools.
- Check that the http://example.com/admin/content listing has been overridden by admin_views
- Edit the view generated by admin_views at http://example.com/admin/structure/views/view/admin_views_node/edit
- Add any new Fields you might need in the View for it to make sense. Adding Availability is not necessary but may be helpful
- Add a new Filter criteria on Availability
- Expose the new filter so we can alter it in the content listing
- Also expose the operator so we can select 'None of' in the content listing
- And allow multiple so we can select nodes that are none of available and not available
- Edit the Bulk Operations (Content) field at the top of the View field list
- Make sure that Modify Entity Values is available and checked
- If not make sure that the Entity module is enabled
- I also alter the pagination so that too is exposed. And I set some high upper limits (50, 250 and 1000 nodes) for convenience.
- 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.
- Select all the rows in the filtered list by checking the top left checkbox. Optionally press `Select all rows in this view`.
- From the Operations select list choose `Change value` and press Execute
- 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.
- 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).