Solution to CCK Field Groups not appearing in hook_form_alter()
One issue that I recently came across with a module I was editing was that the fieldsets / field groups from a CCK type weren't in the $form
array passed to hook_form_alter()
.
According to Andy Chase, In CCK for D5 & D6, the 'fieldgroup' module supplied with CCK is given a weight of '9', which means that it's hooks will be run after most other modules.
Because of this, fieldgroup's hook_form_alter()
function which adds the groups to the $form
array is added long after your custom module's hook_form_alter()
is run.
The reason for this (according to #292338: Fieldgroup module weight) is that putting them in groups makes it difficult for users to find / work with the fields. Personally, I prefer working with the groups, and, in this particular case, I needed the ability to modify the fieldsets within hook_form_alter()
.
The solution which Andy gave work perfectly. Create the following snippet in your mymodule.install:
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #FF8000">/**<br>* Fix module weight to be heavier than CCK field group module,<br>* so hook_form_alter() has access to field groups<br>*/<br></span><span style="color: #007700">function </span><span style="color: #0000BB">my_module_update_1</span><span style="color: #007700">() {<br> </span><span style="color: #0000BB">$items </span><span style="color: #007700">= array();<br> </span><span style="color: #0000BB">$items</span><span style="color: #007700">[] = </span><span style="color: #0000BB">update_sql</span><span style="color: #007700">(<br> </span><span style="color: #DD0000">"UPDATE {system} <br> SET weight = 10 <br> WHERE `name` = 'my_module';"<br> </span><span style="color: #007700">);<br> </span><span style="color: #0000BB">drupal_set_message</span><span style="color: #007700">(</span><span style="color: #DD0000">'Updated custom module weight.'</span><span style="color: #007700">);<br> return </span><span style="color: #0000BB">$items</span><span style="color: #007700">;<br>} <br></span><span style="color: #0000BB">?></span></span>