You are here

function taxonomy_defaults_form_alter in Taxonomy Defaults 7

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

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

File

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

Code

function taxonomy_defaults_form_alter(&$form, &$form_state, $form_id) {

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

    // Retreive the core forum vocabulary id so it can be skipped later
    $forum_vid = variable_get('forum_nav_vocabulary', '');

    // Add the default 'pre-selected' terms to $node->taxonomy
    foreach (taxonomy_get_vocabularies($node->type) as $vid => $vocab) {

      // Ignore this vocabulary if it is the core forum vocabulary
      if ($vid == $forum_vid) {
        continue;
      }
      $default_tids = variable_get("taxdef_{$node->type}_{$vid}", array());
      $visible = variable_get("taxdef_{$node->type}_{$vid}_visible", TRUE);
      if ($vocab->tags) {

        // for freetag vocabs, build an array of terms and convert to a string
        $terms = array();
        foreach ($default_tids as $tid) {
          $terms[] = taxonomy_term_load($tid);
        }
        $typed_string = taxonomy_implode_tags($terms, $vid);
        if ($visible) {

          // Do not preselect terms on nodes that already have been edited
          if (!isset($node->nid)) {
            $form['taxonomy']['tags'][$vid]['#default_value'] = $typed_string;
          }
        }
        else {

          // For hidden vocabs, we just assign the defaults without checking for existing terms.
          // It's a bit brutal to assume this is OK, but so is the alternative, and this keeps the forms clean.
          $form['taxonomy']['tags'][$vid] = array(
            '#type' => 'value',
            '#value' => $typed_string,
          );
        }
      }
      else {
        if ($visible) {

          // Do not preselect terms on nodes that already have been edited
          if (!isset($node->nid)) {
            $form['taxonomy'][$vid]['#default_value'] = $default_tids;
          }
        }
        else {

          // For hidden vocabs, we just assign the defaults without checking for existing terms.
          // It's a bit brutal to assume this is OK, but so is the alternative, and this keeps the forms clean.
          $form['taxonomy'][$vid] = array(
            '#type' => 'value',
            '#value' => $default_tids,
          );
        }
      }
    }
  }
}