[development] How to move taxonomy fields from the categoriesfieldset?

Nedjo Rogers nedjo at islandnet.com
Thu Jun 29 15:58:07 UTC 2006


> I see. So I could copy the fields to normal $form['something'] and  unset 
> the fields in the categories fieldset in hook_form_alter. Then,  in my 
> submit callback sort of "rebuild" the output so that it looks  like how 
> taxonomy module expects.

Depending on your needs, you might be able to get away with fewer changes by 
doing this in your form_alter:

// Unset all but the #tree property, so that taxonomy is no longer a 
fieldset.
foreach (array_keys(element_properties($form['taxonomy'])) as $property) {
  if ($property != '#tree'') {
    unset($form['taxonomy'][$property]);
  }
}

// Give the taxonomy an explicity weight to position it in the form.
// Of course, you may need to give other elements weights too....
$form['taxonomy']['weight'] = 3;

It would be nice to be able to move the taxonomy element without setting 
explicit weights, but e.g. array_splice() doesn't work with string keys. I 
suppose maybe you could iterate through an array and rebuild it accordingly. 
Here's a very hacky idea:

$newform = array();
foreach ($form as $key => $value) {
  $add = FALSE;
  if (!in_array($key, array('taxonomy', 'onebefore'))) {
    $newform[$key] = $value;
  }
  else if ($key == 'onebefore')) {
    $add= TRUE;
  }
  if ($add) {
    $newform['taxonomy'] = $value;
    $add = FALSE;
  }
}
$form = $newform;



More information about the development mailing list