You are here

taxonomy_container.module in Taxonomy container 6

Same filename and directory in other branches
  1. 7 taxonomy_container.module

Allows users to organize containers using taxonomy terms.

File

taxonomy_container.module
View source
<?php

/**
 * @file
 *   Allows users to organize containers using taxonomy terms.
 */

/**
 * Implements hook_help().
 */
function taxonomy_container_help($path, $arg) {
  if ($path == 'admin/help#taxonomy_container') {
    $output = '<p>' . t('The taxonomy container module allows you to organize containers using taxonomy terms.') . '</p>';
    $output .= '<p>' . t('This module just alter the node add/edit form, so users do not have opportunity to add node to the root taxonomy terms.') . '</p>';
    $output .= '<p>' . t('You can enable this feature on the vocabulary settings page.') . '</p>';
    return $output;
  }
}

/**
 * Implements hook_form_alter().
 */
function taxonomy_container_form_alter(&$form, $form_state, $form_id) {

  // Alter node Add/Edit form.
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $node = $form['#node'];
    if (!isset($node->taxonomy)) {
      $terms = empty($node->nid) ? array() : taxonomy_node_get_terms($node);
    }
    else {

      // After a preview or form reload, the terms must be converted to objects.
      reset($node->taxonomy);
      if (!is_object(current($node->taxonomy))) {
        $node->taxonomy = taxonomy_preview_terms($node);
      }
      $terms = $node->taxonomy;
    }
    $result = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $node->type);
    while ($vocabulary = db_fetch_object($result)) {
      if ($vocabulary->tags) {
        if (isset($form_state['node_preview'])) {

          // Typed string can be changed by the user before preview,
          // so we just insert the tags directly as provided in the form.
          $typed_string = $node->taxonomy['tags'][$vocabulary->vid];
        }
        else {
          $typed_string = taxonomy_implode_tags($terms, $vocabulary->vid) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);
        }
        if ($vocabulary->help) {
          $help = filter_xss_admin($vocabulary->help);
        }
        else {
          $help = t('A comma-separated list of terms describing this content. Example: funny, bungee jumping, "Company, Inc.".');
        }
        $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' => 1024,
        );
      }
      else {

        // Extract terms belonging to the vocabulary in question.
        $default_terms = array();
        foreach ($terms as $term) {

          // Free tagging has no default terms and also no vid after preview.
          if (isset($term->vid) && $term->vid == $vocabulary->vid) {
            $default_terms[$term->tid] = $term;
          }
        }
        $form['taxonomy'][$vocabulary->vid] = taxonomy_container_form($vocabulary->vid, array_keys($default_terms), filter_xss_admin($vocabulary->help));
        $form['taxonomy'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
        $form['taxonomy'][$vocabulary->vid]['#required'] = $vocabulary->required;
      }
    }
    if (!empty($form['taxonomy']) && is_array($form['taxonomy'])) {
      if (count($form['taxonomy']) > 1) {

        // Add fieldset only if form has more than 1 element.
        $form['taxonomy'] += array(
          '#type' => 'fieldset',
          '#title' => t('Vocabularies'),
          '#collapsible' => TRUE,
          '#collapsed' => FALSE,
        );
      }
      $form['taxonomy']['#weight'] = -3;
      $form['taxonomy']['#tree'] = TRUE;
    }
  }
}

/**
 * Generate a form element for selecting terms from a vocabulary.
 *
 * @param $vid
 *   The vocabulary ID to generate a form element for.
 * @param $value
 *   The existing value of the term(s) in this vocabulary to use by default.
 * @param $help
 *   Optional help text to use for the form element. If specified, this value
 *   MUST be properly sanitized and filtered (e.g. with filter_xss_admin() or
 *   check_plain() if it is user-supplied) to prevent XSS vulnerabilities. If
 *   omitted, the help text stored with the vocaulary (if any) will be used.
 * @return
 *   An array describing a form element to select terms for a vocabulary.
 *
 * @see _taxonomy_container_term_select()
 * @see _taxonomy_term_select()
 * @see filter_xss_admin()
 */
function taxonomy_container_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') {
  $vocabulary = taxonomy_vocabulary_load($vid);
  $help = $help ? $help : filter_xss_admin($vocabulary->help);
  if (!$vocabulary->multiple) {
    $blank = $vocabulary->required ? t('- Please choose -') : t('- None selected -');
  }
  else {
    $blank = $vocabulary->required ? 0 : t('- None -');
  }
  $vocs = variable_get('taxonomy_container_vids', array());
  $select_func = empty($vocs[$vid]) ? '_taxonomy_term_select' : '_taxonomy_container_term_select';
  return call_user_func($select_func, check_plain($vocabulary->name), $name, $value, $vid, $help, intval($vocabulary->multiple), $blank);
}

/**
 * Create a select form element for a given taxonomy vocabulary.
 *
 * NOTE: This function expects input that has already been sanitized and is
 * safe for display. Callers must properly sanitize the $title and
 * $description arguments to prevent XSS vulnerabilities.
 *
 * @param $title
 *   The title of the vocabulary. This MUST be sanitized by the caller.
 * @param $name
 *   Ignored.
 * @param $value
 *   The currently selected terms from this vocabulary, if any.
 * @param $vocabulary_id
 *   The vocabulary ID to build the form element for.
 * @param $description
 *   Help text for the form element. This MUST be sanitized by the caller.
 * @param $multiple
 *   Boolean to control if the form should use a single or multiple select.
 * @param $blank
 *   Optional form choice to use when no value has been selected.
 * @param $exclude
 *   Optional array of term ids to exclude in the selector.
 * @return
 *   A FAPI form array to select terms from the given vocabulary.
 *
 * @see taxonomy_form()
 * @see taxonomy_form_term()
 */
function _taxonomy_container_term_select($title, $name, $value, $vocabulary_id, $description, $multiple, $blank, $exclude = array()) {
  $tree = taxonomy_get_tree($vocabulary_id);
  $options = array();
  if ($blank) {
    $options[''] = $blank;
  }
  if ($tree) {
    foreach ($tree as $term) {
      if (!in_array($term->tid, $exclude)) {
        if ($term->parents[0] == 0) {
          $parent_name = $term->name;
        }
        else {
          $choice = new stdClass();
          $choice->option = array(
            $term->tid => str_repeat('-', $term->depth) . ' ' . $term->name,
          );
          $options[$parent_name][$term->tid] = $choice;
        }
      }
    }
  }
  $select = array(
    '#type' => 'select',
    '#title' => $title,
    '#default_value' => $value,
    '#options' => $options,
    '#description' => $description,
    '#multiple' => $multiple,
    '#size' => $multiple ? min(9, count($options)) : 0,
    '#weight' => -15,
    '#theme' => 'taxonomy_term_select',
  );
  return $select;
}

/**
 * Alter vocabulary settings form.
 */
function taxonomy_container_form_taxonomy_form_vocabulary_alter(&$form, $form_state) {
  $vids = variable_get('taxonomy_container_vids', array());
  $vid = $form['vid']['#value'];
  $form['#submit'][] = 'taxonomy_container_settings_submit';
  $form['settings']['taxonomy_container_' . $vid] = array(
    '#type' => 'checkbox',
    '#title' => t('Apply containers for root terms'),
    '#default_value' => !empty($vids[$form['vid']['#value']]),
    '#description' => t('Force users to select only child terms.'),
  );
}

/**
 * Process vocabulary settings form submissions.
 */
function taxonomy_container_settings_submit(&$form, $form_state) {
  $vid = $form['vid']['#value'];
  $vids = variable_get('taxonomy_container_vids', array());
  $vids[$vid] = $form_state['values']['taxonomy_container_' . $vid];
  variable_set('taxonomy_container_vids', $vids);
}

Functions

Namesort descending Description
taxonomy_container_form Generate a form element for selecting terms from a vocabulary.
taxonomy_container_form_alter Implements hook_form_alter().
taxonomy_container_form_taxonomy_form_vocabulary_alter Alter vocabulary settings form.
taxonomy_container_help Implements hook_help().
taxonomy_container_settings_submit Process vocabulary settings form submissions.
_taxonomy_container_term_select Create a select form element for a given taxonomy vocabulary.