Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Drupal 6: Updating nodes when changing CCK field allowed value list using a submit handler
In this article I’ll address an issue that has surfaced on every recent project I’ve been working on: how to update nodes when a CCK field’s allowed values list changes. For my projects, I implemented a variety of batch processes and drush scripts to solve this problem. But for this article, I decided to implement a form submit handler solution to automatically update nodes when the CCK field settings page is submitted. Please be careful when implementing code like this, it has potential to update a lot of nodes, and hit memory limitation issues depending on how many nodes need to be updated. As I mentioned before, I implemented a combination of batch APIs and drush scripts to process production environments; I implemented this code in a development environment. Anyway..
I created a new content type “band”, and added a CCK text/select field (genre) with an allowed values list:
I used Devel’s auto-generate content functionality to create 20 nodes (/admin/generate/content), and then added a simple view to show the node’s title and genre:
Now that I had some working test data, I added some code to a custom module to modify the CCK field property settings page, and add a submit handler update the nodes:
To test this code, I went back to the CCK field settings page (example: /admin/content/node-type/band/fields/field_genre), updated the allowed value list, and saved.
The form submit handler is triggered and the nodes are updated. Refreshing my view shows the updated genre fields on my test nodes: