node/add form: taxonomy

ppmax - October 13, 2009 - 01:26

Hi--

I have a custom module that does a bunch of hook_form_alter stuff. One thing Im trying to do is to preselect a taxonomy term based upon the value of a variable passed in the url. Basically the reason for this is:
A user visits the site and goes to the "wanted" section where they see a list of items that the site owner is looking for. The user clicks a link (something like /submit?manufacturer=foo&category=bar which takes them to the node/add form...which is basically the node/add form for a custom content type. Based upon the vars passed in the URI I want to pre-select a taxonomy term from the taxonomy drop down.

My hook_form_alter code looks like this:

<?php
//drupal_set_message('<pre>'. var_export($form,TRUE) .'</pre>');  //for debug
//prepopulate form fields if coming from "Wanted"
//get querystring
if ($_GET['manufacturer']) {
 
$manufacturer = $_GET['manufacturer'];
}

if(
$manufacturer) {
 
$form['title']['#default_value'] = $manufacturer;
 
$form['taxonomy'][9]['#default_value'] = array(52 => '$manufacturer');
 
// $form['taxonomy']['9']['#pre_render'] = 'taxonomy_prerender';   
}
?>

As you can see above I've tried setting the taxonomy vid 9 #default_value element...but no dice on that one. I also tried calling a #pre_render method into a custom func but couldnt get that to work either.

Any tips?

thx
PP

Change custom module weight I

abdu - October 13, 2009 - 17:54

Change custom module weight

I too experienced the same problem in Drupal 6.

Try changing the weight of module where you have written the above form alter code to execute later.

You can change the module weight in system table and assign 1000

Thanks for the reply and

ppmax - October 13, 2009 - 23:56

Thanks for the reply and suggestion. I changed the weight per your instructions but it had no effect. Do I need to reload or re-initialize my custom module?

Another thought I had: is this a case where I need to engage #pre_render? I've not used that method before so if you have any tips about how to do so I'd be hugely appreciative.

thx
PP

Incidentally, here is the

ppmax - October 13, 2009 - 23:58

Incidentally, here is the output of the relevant section of the $form array Im trying to modify:

<?php
'taxonomy' =>
  array (
   
9 =>
    array (
     
'#type' => 'select',
     
'#title' => 'Manufacturers',
     
'#default_value' =>
      array (
      ),
     
'#options' =>
      array (
       
'' => '- Please choose -',
       
0 =>
       
stdClass::__set_state(array(
          
'option' =>
          array (
           
104 => 'Allu',
          ),
        )),
...
?>

Clear the Cache

abdu - October 14, 2009 - 05:38

Clear the cache by visiting admin/buil/modules page

I've cleared the cache, and

ppmax - October 14, 2009 - 18:26

I've cleared the cache, and disabled/enabled my custom module but no dice. The form that my module modifies is the standard "stock" node/add form and the taxonomy term default value Im trying to set is the standard "stock" node taxonomy list associated with this node type.

One thing that might be happening is that the default value may want a taxonomy number instead of an array with termid and term name (string). I'll try that tonight.

thx!
PP

Got it sorted out

ppmax - October 15, 2009 - 01:38

I sorted it out--basically I was passing an array with a number and a string but the element only wanted a tid for the #default_value. Also, like an idiot, I was trying to return the node taxonomy but Im actually not setting taxonomy terms on these nodes...im using a lookup field created for CCK.

Here's the code I ended up using in the custom.module form_alter func:

<?php
if($_GET['nid']) {
 
$nid = $_GET['nid'];
 
$node = node_load($nid);
 
$title = $node->title;
 
$manufacturer = $node->field_manufacturer[0]['value'];
 
$category = $node->field_category[0]['value'];
 
$form['title']['#default_value'] = $title;
 
$form['taxonomy'][9]['#default_value'] = $manufacturer;
 
$form['taxonomy'][4]['#default_value'] = $category;
}
?>

Thanks for posting--you made me think about this in a new light.

pp

 
 

Drupal is a registered trademark of Dries Buytaert.