taxonomy_test.module
2.65 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* @file
* Test module for Taxonomy hooks and functions not used in core.
*
* @see TaxonomyHooksTestCase::testTaxonomyTermHooks()
*/
/**
* 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_taxonomy_term_view().
*/
function taxonomy_test_taxonomy_term_view($term, $view_mode, $langcode) {
if ($view_mode == 'full') {
$term->content['taxonomy_test_term_view_check'] = array(
'#prefix' => '<div>',
'#markup' => t('The antonym is %antonym', array('%antonym' => $term->antonym)),
'#suffix' => '</div>',
'#weight' => 10,
);
}
}
/**
* Implements hook_entity_view().
*/
function taxonomy_test_entity_view($entity, $type, $view_mode, $langcode) {
if ($type == 'taxonomy_term' && $view_mode == 'full') {
$entity->content['taxonomy_test_entity_view_check'] = array(
'#prefix' => '<div>',
'#markup' => t('The antonym is %antonym', array('%antonym' => $entity->antonym)),
'#suffix' => '</div>',
'#weight' => 20,
);
}
}
/**
* 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();
}