Today was spent working on creating a Drupal 6 version of a custom Drupal 5 module as part of a Drupal upgrade project. This module is similar to the image_attach module that is a contrib module package with the Image module. The difference is that the module allows you to designate categories for images by content_type and allows for the upload, selection, removal and ordering of images in each “image category”.
Now, one of the biggest challenges is that there are multiple instances of the “image_attach” sections on a single content edit form, one for each category. The Drupal 5 version handled this by naming these by appending the category name to the fields. This allows the submitted form value for “iids” to be an array of categories. Each category itself being an array of image iid values.
For the life of me, my Drupal 6 version of this functionality failed to maintain the category arrays. After some frustrated hair pulling (hmmm…. no wonder my hair is so short), I remembered to take a look at the FAPI docs.
http://api.drupal.org/api/drupal/developer–topics–forms_api.html/6
An important thing to note: notice that
$form['access']has a'#tree' => TRUEattribute. this setting retains the full tree structure for all elements under it when it is passed to$form_state['values']. you must explicitly declare this anywhere you wish to retain an array’s full hierarchy when it is passed.
So, Drupal 6 collapses form elements where possible by default. By adding the #tree attribute, the hierarchy of form elements is maintained in the form_state submitted. Eureka!
Here’s an example of what I was getting without the #tree attribute:
And now with the #tree attribute set to true, the form hierarchy is retain and not collapsed:
I’m sure most of you doing Drupal development are saying “Duh!”. Well, let’s just say I really didn’t know.
Thought I’d throw it on the blog so I don’t forget.
Blessings.
Popularity: 2% [?]

