[development] How to move taxonomy fields from the categories fieldset?

Dave Cohen drupal at dave-cohen.com
Wed Jun 28 04:21:59 UTC 2006


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


More information about the development mailing list