File: taxonomy_test.module

package info (click to toggle)
drupal7 7.52-2%2Bdeb9u11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,756 kB
  • sloc: php: 44,022; pascal: 42,649; javascript: 31,405; sh: 1,685; xml: 466; makefile: 21; sql: 1
file content (141 lines) | stat: -rw-r--r-- 3,420 bytes parent folder | download | duplicates (3)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?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();
}

/**
 * Implements hook_query_alter().
 */
function taxonomy_test_query_alter(QueryAlterableInterface $query) {
  $value = variable_get(__FUNCTION__);
  if (isset($value)) {
    variable_set(__FUNCTION__, ++$value);
  }
}

/**
 * Implements hook_query_TAG_alter().
 */
function taxonomy_test_query_term_access_alter(QueryAlterableInterface $query) {
  $value = variable_get(__FUNCTION__);
  if (isset($value)) {
    variable_set(__FUNCTION__, ++$value);
  }
}

/**
 * Implements hook_query_TAG_alter().
 */
function taxonomy_test_query_taxonomy_term_access_alter(QueryAlterableInterface $query) {
  $value = variable_get(__FUNCTION__);
  if (isset($value)) {
    variable_set(__FUNCTION__, ++$value);
  }
}