summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/taxonomy_test.module
blob: d524fb87c8d84898ff38261a483a8d0021cddb81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
<?php
// $Id$

/**
 * @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();
}