home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (taxonomy_test.module)

This example Drupal source code file (taxonomy_test.module) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

antonym, empty, execute, foreach, form, form_id, function, if, php, taxonomy_test_get_antonym, term, terms, tid, trim

The taxonomy_test.module Drupal example source code

<?php
// $Id: taxonomy_test.module,v 1.8 2009/12/04 16:49:47 dries Exp $

/**
 * @file
 * Test module for Taxonomy hooks and functions not used in core.
 */

/**
 * Implements hook_taxonomy_term_load().
 */
function taxonomy_test_taxonomy_term_load($terms) {
  foreach ($terms as $term) {
    $antonym = taxonomy_test_get_antonym($term->tid);
    if ($antonym) {
      $term->antonym = $antonym;
    }
  }
}

/**
 * Implements hook_taxonomy_term_insert().
 */
function taxonomy_test_taxonomy_term_insert($term) {
  if (!empty($term->antonym)) {
    db_insert('taxonomy_term_antonym')
      ->fields(array(
        'tid' => $term->tid,
        'name' => trim($term->antonym)
      ))
      ->execute();
  }
}

/**
 * Implements hook_taxonomy_term_update().
 */
function taxonomy_test_taxonomy_term_update($term) {
  if (!empty($term->antonym)) {
    db_merge('taxonomy_term_antonym')
      ->key(array('tid' => $term->tid))
      ->fields(array(
        'name' => trim($term->antonym)
      ))
      ->execute();
  }
}

/**
 * Implements hook_taxonomy_term_delete().
 */
function taxonomy_test_taxonomy_term_delete($term) {
  db_delete('taxonomy_term_antonym')
    ->condition('tid', $term->tid)
    ->execute();
}

/**
 * Implements hook_form_alter().
 */
function taxonomy_test_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'taxonomy_form_term') {
    $antonym = taxonomy_test_get_antonym($form['#term']['tid']);
    $form['advanced']['antonym'] = array(
      '#type' => 'textfield',
      '#title' => t('Antonym'),
      '#default_value' => !empty($antonym) ? $antonym : '',
      '#description' => t('Antonym of this term.')
    );
  }
}

/**
 * Return the antonym of the given term ID.
 */
function taxonomy_test_get_antonym($tid) {
  return db_select('taxonomy_term_antonym', 'ta')
    ->fields('ta', array('name'))
    ->condition('tid', $tid)
    ->execute()
    ->fetchField();
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal taxonomy_test.module source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.