You are here

taxonomy_defaults.module in Taxonomy Defaults 6

Taxonomy defaults - allows assignment of default terms to node types, either

  • hidden, added after node submission. Change is not possible.
  • preselected in the category form for vocabularies assigned to the node type. Change is allowed.

File

taxonomy_defaults.module
View source
<?php

/**
 * @file
 * Taxonomy defaults - allows assignment of default terms to node types, either
 *  - hidden, added after node submission. Change is not possible.
 *  - preselected in the category form for vocabularies assigned to the node type. Change is allowed.
 */

/**
 * Implementation of hook_help().
 *
 */
function taxonomy_defaults_help($path, $arg) {
  switch ($path) {
    case 'admin/content/taxonomy/taxonomy_defaults':
      $output = '<p>' . t("Below you may select default terms for each content type. Checking the 'Hide' checkbox to hide the vocabulary on the node creation form while still applying the default terms.") . '</p>';
      return $output;
  }
}

/**
 * Implementation of hook_perm().
 *
 */
function taxonomy_defaults_perm() {
  return array(
    'administer taxonomy defaults',
  );
}

/**
 * Define a custom callback to assign default terms menu at a tab on admin/taxonomy.
 *
 * Implementation of hook_menu().
 */
function taxonomy_defaults_menu() {
  $items['admin/content/taxonomy/taxonomy_defaults'] = array(
    'type' => MENU_LOCAL_TASK,
    'title' => 'Default terms',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'taxonomy_defaults_form',
    ),
    'access arguments' => array(
      'administer taxonomy defaults',
    ),
    'file' => 'taxonomy_defaults.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 *
 * @return unknown
 */
function taxonomy_defaults_theme() {
  return array(
    'taxonomy_defaults_form' => array(
      'arguments' => array(
        'form',
      ),
      'file' => 'taxonomy_defaults.admin.inc',
    ),
  );
}

/**
 * Adds the defaults for active vocabularies as preselected terms to '$node->taxonomy'
 * This requires a weight higher than taxonomy.module.
 */
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'];

    // Add the default 'pre-selected' terms to $node->taxonomy
    foreach (taxonomy_get_vocabularies($node->type) as $vid => $vocab) {
      $default_tids = variable_get("taxdef_{$node->type}_{$vid}", array());
      $visible = variable_get("taxdef_{$node->type}_{$vid}_visible", array());
      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_get_term($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 {
          $form['taxonomy']['tags'][$vid]['#type'] = 'value';
          unset($form['taxonomy'][$vid]['#theme']);

          // Do not preselect terms on nodes that already have been edited
          if (!isset($node->nid)) {
            $form['taxonomy']['tags'][$vid]['#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 {
          $form['taxonomy'][$vid]['#type'] = 'value';
          unset($form['taxonomy'][$vid]['#theme']);

          // Do not preselect terms on nodes that already have been edited
          if (!isset($node->nid)) {
            $form['taxonomy'][$vid]['#value'] = $default_tids;
          }
        }
      }
    }
  }
}
function _taxonomy_defaults_clear_settings($type, $vid) {
  variable_del("taxdef_{$type}_{$vid}_visible");
  variable_del("taxdef_{$type}_{$vid}");
}

/**
 * Implementation of hook_taxonomy().
 *
 * Clean up taxonomy default settings on deletion of a taxonomy vocabulary
 */
function taxonomy_defaults_taxonomy($op, $type, $args = NULL) {
  if ($op == 'delete') {
    if ($type == 'vocabulary') {
      $vid = $args['vid'];
      foreach (node_get_types() as $type => $name) {
        _taxonomy_defaults_clear_settings($type, $vid);
      }
    }
    elseif ($type == 'term') {

      //todo - search all taxdef settings and delete this term. maybe not worth the effort?
    }
  }
}

/**
 * Implementation of hook_node_type().
 *
 * Clean up taxonomy default settings on deletion of a content type
 */
function taxonomy_defaults_node_type($op, $args = NULL) {
  if ($op == 'delete') {
    foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
      _taxonomy_defaults_clear_settings($args->type, $vid);
    }
  }
}

Functions

Namesort descending Description
taxonomy_defaults_form_alter Adds the defaults for active vocabularies as preselected terms to '$node->taxonomy' This requires a weight higher than taxonomy.module.
taxonomy_defaults_help Implementation of hook_help().
taxonomy_defaults_menu Define a custom callback to assign default terms menu at a tab on admin/taxonomy.
taxonomy_defaults_node_type Implementation of hook_node_type().
taxonomy_defaults_perm Implementation of hook_perm().
taxonomy_defaults_taxonomy Implementation of hook_taxonomy().
taxonomy_defaults_theme Implementation of hook_theme().
_taxonomy_defaults_clear_settings