Hello all,

My objective is to allow a user to add new nodes to a section of the site, and have those nodes automatically pre-populated with the appropriate taxonomy term.

For example, the user would click an 'add content here' button, which would point to /node/add/flexinode-1 ... and the node form would be displayed. I have set up taxonomy so the user can select the appropriate term from the drop-down list. However, I would like to be able to set the drop-down to a default based on the context of where they were when they clicked 'add content here'.

I assume that the URL for the 'add content here' button would include some sort of parameter (like term=something) so that the node form could be aware of which section the user was adding the content to.

Any ideas on the best way to approach this? Should I try to modify the _taxonomy_term_select() function in taxonomy.module? Not sure where to start... any direction would be helpful.

Thanks,
Chud

Comments

venkat-rk’s picture

There is a taxonomy_default module that does this, I think. Sorry for not giving the link but a quick search should pull it up for you.

chud’s picture

Thanks Ramdak. This is interesting, but I don't think it will help me to dynamically set the default based on the context of the site.

chud’s picture

After messing around a bit I came up with the following solution, although there must be a better cleaner way.

1. Created the following link to create a new node: /node/add/flexinode-1?default=mydefault

2. I then inserted the following code into taxonomy_form_alter() in taxonomy.module

        if ($typed_string == "") {
        	$typed_string = $_GET['default'];        	
        }

...just before this statement...

          $form['taxonomy']['tags'][$vocabulary->vid] = array('#type' => 'textfield',
          '#title' => $vocabulary->name,
          '#description' => $help,
          '#required' => $vocabulary->required,
          '#default_value' => $typed_string,
          '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid,
          '#weight' => $vocabulary->weight,
          '#maxlength' => 100,
        );

The result was that the Category field was pre-populated with the value of $_GET['default'].

Note that I am using 'free tagging'.

Can anyone provide me with direction on how to properly implement this?

eye’s picture

Thanks chud, you helped solve my problem!

avior’s picture

Hi There
I am using 4.6 and I want to do the same look here http://drupal.org/node/62965
I found the taxonomy default but it not suitable for 4.6
the code here is just what i need will this work in 4.6 ? , I guess not
Can you tell me what to change in order it to work in 4.6 ?

Thanks , Avior

Mark Celsor’s picture

If you are trying to do this with a selector. Put this line:

if ($_GET['default_id'] != "") {
   $default_terms[$_GET['default_id']] = $_GET['default_id'];   
}

Ahead of this block:

$form['taxonomy'][$vocabulary->vid] = taxonomy_form($vocabulary->vid, 
   array_keys($default_terms), $vocabulary->help);
$form['taxonomy'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
$form['taxonomy'][$vocabulary->vid]['#required'] = $vocabulary->required;

And use the category id in your link:
/node/add/flexinode-1?default_id=555

Drupal User-1’s picture

The solution was great. I applied it for the Image content type. But the link that I had to give to the add node was not http://../../../?q=node/add/image?default=termname... When I first used it, it went to the page where all the content types were listed. So instead I used ?q=node/add/image&default=termname. May be this is useful to someone. :)

FYI : I am using D6