How can I set a default taxonomy term in the add node form of mycontenttype?

In the themes/garland/template.php function phptemplate_mycontenttype_node_form($form) I tried eg:

$form['taxonomy']['6']['#default_value'] = 17; // not working

But term 17 of vocabulary 6 won't get pre-selected. Do I need to set the tid wihin an array maybe?

I'm glad I found out how to have the category fieldset collapsed by default and the file attachments fieldset open:

$form['taxonomy']['#collapsed'] = true;
$form['attachments']['#collapsed'] = false;

Cheers!

Comments

graysadler’s picture

are you sure that's the right array: $form['taxonomy']['6']['#default_value'] and that 17 is a valid term? Is the garland theme enabled and selected?

You may consider using the form_alter hook instead.

Lead Developer and Founder of StreamRiot.com

brevity’s picture

Thanks for your replies, the taxonomy_defaults module sets defaults by

         foreach ($default_tids as $default_tid) {
            $term = taxonomy_get_term($default_tid);
            $form['#node']->taxonomy[$default_tid] = $term;
         }

Can I do this in template.php as well? Which part of the array do I need to set, and how?

Do I need to set the #options array maybe, or something like value=selected?

My $form array looks as follows:

    [taxonomy] => Array
        (
            [6] => Array
                (
                    [#type] => select
                    [#title] => Type of work
                    [#default_value] => Array
                        (
                        )

                    [#options] => Array
                        (
                            [] => - Please choose -
                            [0] => stdClass Object
                                (
                                    [option] => Array
                                        (
                                            [17] => MyCategoryTerm
                                        )

                                )
graysadler’s picture

$form['taxonomy'][6]['#default_value'] = array(tid => tid); where tid is the term id.

You probably wouldn't want to do that in template.php because that's for theme functions, unless there is a particular theme function for the page you're looking at. Try the fix above first...that should hopefully work.

Lead Developer and Founder of StreamRiot.com

brevity’s picture

Okay, now I think I have it... http://api.drupal.org/api/function/form_select_options/5 does not have #default_value

Here's what I added to template.php -- where would such stuff sit better? I often do not know where to start hacking...

function phptemplate_xxx_node_form($form) {
  $form['taxonomy'][6]['#value'] = 17;
  return drupal_render($form);
}

Cheers!

MGParisi’s picture

I am assumeing you are using a single select like me, but your code does not produce the same results, though its got to be close! Its just $value is said to be an array in print_r! I also have #default_value in my print_r, but it does not set the value either.

is #value the taxonomy id or the default selection?

EDIT #value is the term_ID and I got it to work so thanks

roald’s picture

McCool’s picture

That's just what I need!
Thanks.

kenorb’s picture

Thanks:)

danny_joris’s picture

Great module.

However, installing this module showed me how to change it manually. Make a custom module and add this:

<?php
function custom_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'xxx_node_form') {
  	$form['taxonomy'][1]['#default_value'] = array(0 => 1);
  	
        return dsm($form); // pretty print with devel module.
  }
}
?>

The important part is "array(0 => 1)". "custom" is the module name and xxx_node_form is the form ID.

Hope this helps.
Cheers,
Danny