How to move taxonomy fields from the categories fieldset?
I found an old thread where this was asked about but it didn't seem to have a resolution: http://lists.drupal.org/archives/development/ 2006-03/msg00511.html Basically, I'd like the ability to take the stuff in the "categories" fieldset (specifically, free-tagging vocabs, but any and all is good), assign it some arbitrarily weight, and place it elsewhere in the form. Anyone have any pearls of wisdom? I hereby volunteer to write up a nice little tutorial about it for the handbook if someone can throw me some sample code. Thanks a lot, -Angie
I had to do this and ended up doing it as follows: First, add the fields to the form. I generated the fields as follows, but you might have equal success by using hook_form_alter to move taxonomy fields to a new place. // This code in hook_form: // to add fields to this form, see _ocp_project_talent_fields method if ($fields = _ocp_project_talent_fields($i)) { $w = 0; foreach($fields as $field => $data) { $value = $_REQUEST['edit']['node_data'][$field] or $value = $node->node_data[$field]; // XXX should not have to do this, but we do for previews. hopefully will be fixed in drupal $form[$f_field]['node_data'][$field] = _taxonomy_term_select(t($data['label']), "node_data][$field", $value, $data['vid'], '', false, 'None'); $form[$f_field]['node_data'][$field]['#weight'] = $w++; } } That may look more complicated than it really is. The $fields data structure looks like: return array( 'agency' => array('label' => 'Agency', 'vid' => OCP_VID_AGENCY), 'director' => array('label' => 'Director', 'vid' => OCP_VID_TALENT), 'vfx_supervisor' => array('label' => 'VFX Supervisor', 'vid' => OCP_VID_TALENT), 'production_co' => array('label' => 'Production Company', 'vid' => OCP_VID_PRODUCTION_CO), ); Where each of the OCP_VID_... constants is a vocabulary id. So that's part 1, just getting the form fields in the place I want them. Again, simply moving the fields in form_alter may suffice. Next, handle the form_submit... function ocp_project_node_form_submit($form_id, $form_values) { // add our terms to node's taxonomy foreach (_ocp_project_talent_fields(1) as $label => $data) { if ($tid = $form_values['node_data'][$label]) { if (!in_array($tid, $form_values['taxonomy'])) { $form_values['taxonomy'][] = $tid; } } } // now let the default callback handle it... return node_form_submit($form_id, $form_values); } Basically, that submit callback puts the values where taxonomy.module expects them, and calls node_form_submit. Of course all this uses some knowledge of taxonomy methods and data structures that third-party module have no business messing with. So it may not work in future versions of Drupal. I hope that helps, -Dave On Tuesday 27 June 2006 19:49, Angela Byron wrote:
I found an old thread where this was asked about but it didn't seem to have a resolution: http://lists.drupal.org/archives/development/ 2006-03/msg00511.html
Basically, I'd like the ability to take the stuff in the "categories" fieldset (specifically, free-tagging vocabs, but any and all is good), assign it some arbitrarily weight, and place it elsewhere in the form.
Anyone have any pearls of wisdom? I hereby volunteer to write up a nice little tutorial about it for the handbook if someone can throw me some sample code.
Thanks a lot, -Angie
On 28-Jun-06, at 12:21 AM, Dave Cohen wrote:
Basically, that submit callback puts the values where taxonomy.module expects them, and calls node_form_submit.
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. Interesting approach. I'll give it a shot. Thank you! -Angie
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;
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.
please review jjeff's patch at http://drupal.org/node/66183. it adds this function to drupal core.
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]); } }
Nedjo, you are awesome! :D That little snippet was a life-saver today when trying to hide the forum selection field.
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.
Jeff has a patch for this in the queue - http://drupal.org/node/66183 - although, I couldn't get it to work as advertised. I might try Frankensteining yours and his together and see what I come up with because I agree it would be useful. Thanks so much, again! Handbook page will be forthcoming; hopefully I can get some good examples done tomorrow. :) -Angie
participants (5)
-
Angela Byron -
Angie Byron -
Dave Cohen -
Moshe Weitzman -
Nedjo Rogers