taxonomy_test.module 2.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php
// $Id$

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

/**
 * Implementation of hook_taxonomy_term_load().
 */
12 13 14 15
function taxonomy_test_taxonomy_term_load(&$terms) {
  foreach ($terms as $term) {
    $term->antonyms = taxonomy_test_get_antonyms($term->tid);
  }
16 17 18
}

/**
19
 * Implementation of hook_taxonomy_term_insert().
20
 */
21 22 23 24 25 26 27 28 29 30 31 32 33 34
function taxonomy_test_taxonomy_term_insert($term) {
  if (!empty($term->antonyms)) {
    foreach (explode ("\n", str_replace("\r", '', $term->antonyms)) as $antonym) {
      if ($antonym) {
        db_insert('term_antonym')->fields(array('tid' => $term->tid, 'name' => rtrim($antonym)))->execute();
      }
    }
  }
}

/**
 * Implementation of hook_taxonomy_term_update().
 */
function taxonomy_test_taxonomy_term_update($term) {
35 36 37 38
  taxonomy_test_taxonomy_term_delete($term);
  if (!empty($term->antonyms)) {
    foreach (explode ("\n", str_replace("\r", '', $term->antonyms)) as $antonym) {
      if ($antonym) {
39
        db_insert('term_antonym')->fields(array('tid' => $term->tid, 'name' => rtrim($antonym)))->execute();
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      }
    }
  }
}

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

/**
 * Implementation of hook_form_alter().
 */
function taxonomy_test_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'taxonomy_form_term') {
    $antonyms = taxonomy_test_get_antonyms($form['#term']['tid']);
    $form['advanced']['antonyms'] = array(
      '#type' => 'textarea',
      '#title' => t('Antonyms'),
      '#default_value' => !empty($antonyms) ? implode("\n", $antonyms) : NULL,
      '#description' => t('Antonyms of this term, one antonym per line.')
    );
  }
}

/**
 * Return an array of antonyms of the given term ID.
 */
function taxonomy_test_get_antonyms($tid) {
  if ($tid) {
    $antonyms = array();
    $result = db_query('SELECT name FROM {term_antonym} WHERE tid = :tid', array(':tid' => $tid));
    foreach($result as $antonym) {
      $antonyms[] = $antonym->name;
    }
    return $antonyms;
  }
  else {
    return FALSE;
  }
}