Overriding Form Fieldsets and CCK groups (drupal 6.x)

May
2011
19

Drupal is very flexible when it comes to custom content types with custom form fields , categories and subcategories ( vocabularies and taxonomy terms in the drupal world actually :D ). Using the CCK module and the "core" taxonomy module you can build any form you like.This is a standard procedure for a developer ( building the form with custom cck fields and groups ) but with a huge drawback when you want to 'group' similar cck fields with vocabularies. Why's that ? Well this is basically a problem of the default-core procedure that a vocabulary set is build .

The taxonomy module is a different module and the cck module is another one.So every module 'adds' her own groups or fieldsets inside the form . Although there are some modules that can take care of this issue ( vertical tabs , taxonomy fields etc ) they dont provide an all time classic solution for regrouping the fields inside the form.The only way i've found that serves my needs is by creating a new 'homemade' module and regrouping the fields ( cck and vocabularies ) by adding new groups inside the form calling the hook_alter_form function inside the module. This is my post in the drupal community forum where i'm explaining how i've solved it :D Hi. i'm trying for some days now to figure out how can i 'break' the default 'group' fieldset of vocabularies and move them where i want to. After reading a lot of threads and building my custom module i found out that this is possible the best way to do it but i dont know if this the best practice so here is my solution ( if u know better please live some feedback ) In order to 'break' the vocs fieldset we must build a custom module e.g validator validator.info ( 1 file .info : let drupal knows that this module exists :) . . . validator.module ( 1 file .module for our custom php code ) for validator.info

; $Id$
name = Custom Form Validation and Overriding
description = Adding some custom form overrides for validation etc.
core = 6.x
for validator.module PLEASE NOTE THIS IS MY CUSTOM MODULE you can do better than that of course
function validator_form_alter(&$form, $form_state, $form_id) {
//drupal here comes a drupal ninja :P
if ($form_id == 'MYCONTENTTYPE_node_form') {
unset($form['taxonomy']['#type']);
unset($form['#content_extra_fields']['taxonomy']['weight']);
$form['fieldtest'] = array(
'#type' => 'fieldset',
'#title' => t('Choose a name'),
'#collapsible' => FALSE,
'#tree' => TRUE,
);

$form['fieldtest']['FIELDNAMEA'] = $form['taxonomy'][VOCNUMBER]; // could be $form['taxonomy'][1];
$form['fieldtest']['FIELDNAMEB']= $form['taxonomy']['tags'][VOCNUMBER];// please note that this voc has autotag that's why is different
unset($form['taxonomy'][VOCNUMBER]);
unset($form['taxonomy']['tags'][VOCNUMBER]);
$form['#submit'][] = 'validator_NODETYPE_submit';
}
return $form;
}
function  validator_NODETYPE_submit($form, &$form_state) {
  $form_state['values']['taxonomy'][1] = $form_state['values']['fieldtest']['FIELDNAMEA'];
  $form_state['values']['taxonomy']['tags'][9] = $form_state['values']['fieldtest']['FIELDNAMEB'];
}

I dont know if validator_NODETYPE_submit($form, &$form_state) is totally correct from a drupal approach i mean but it works :) So after enabling the module and going to node/add/nodetype page this should work !!! p.s : i'm still searching how can i move the vocs inside into other allready fieldsets created from the cck module at the manage fields panel.

this is an arrow pointing back to the top of the page