You are here

function taxonomy_defaults_form_alter in Taxonomy Defaults 5

Same name and namespace in other branches
  1. 6.2 taxonomy_defaults.module \taxonomy_defaults_form_alter()
  2. 6 taxonomy_defaults.module \taxonomy_defaults_form_alter()
  3. 7 taxonomy_defaults.module \taxonomy_defaults_form_alter()

Adds the defaults for active vocabularies as preselected terms to '$node->taxonomy' This requires a weight lower than taxonomy.module.

File

./taxonomy_defaults.module, line 29
Taxonomy defaults - allows assignment of default terms to node types, either

Code

function taxonomy_defaults_form_alter($form_id, &$form) {

  // Only alter node forms
  if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $node = $form['#node'];

    // Do not preselect terms on nodes that already have been edited
    if (!isset($node->nid)) {

      // Add the default 'pre-selected' terms to $node->taxonomy
      foreach (taxonomy_get_vocabularies($node->type) as $vid => $vocab) {
        if (variable_get("taxdef_{$node->type}_{$vid}_active", FALSE)) {
          $default_tids = variable_get("taxdef_{$node->type}_{$vid}", array());
          foreach ($default_tids as $default_tid) {
            $term = taxonomy_get_term($default_tid);
            $form['#node']->taxonomy[$default_tid] = $term;
          }
        }
      }
    }
  }
}