Projet

Général

Profil

Paste
Télécharger (2,65 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / taxonomy_test.module @ db2d93dd

1
<?php
2

    
3
/**
4
 * @file
5
 * Test module for Taxonomy hooks and functions not used in core.
6
 *
7
 * @see TaxonomyHooksTestCase::testTaxonomyTermHooks()
8
 */
9

    
10
/**
11
 * Implements hook_taxonomy_term_load().
12
 */
13
function taxonomy_test_taxonomy_term_load($terms) {
14
  foreach ($terms as $term) {
15
    $antonym = taxonomy_test_get_antonym($term->tid);
16
    if ($antonym) {
17
      $term->antonym = $antonym;
18
    }
19
  }
20
}
21

    
22
/**
23
 * Implements hook_taxonomy_term_insert().
24
 */
25
function taxonomy_test_taxonomy_term_insert($term) {
26
  if (!empty($term->antonym)) {
27
    db_insert('taxonomy_term_antonym')
28
      ->fields(array(
29
        'tid' => $term->tid,
30
        'name' => trim($term->antonym)
31
      ))
32
      ->execute();
33
  }
34
}
35

    
36
/**
37
 * Implements hook_taxonomy_term_update().
38
 */
39
function taxonomy_test_taxonomy_term_update($term) {
40
  if (!empty($term->antonym)) {
41
    db_merge('taxonomy_term_antonym')
42
      ->key(array('tid' => $term->tid))
43
      ->fields(array(
44
        'name' => trim($term->antonym)
45
      ))
46
      ->execute();
47
  }
48
}
49

    
50
/**
51
 * Implements hook_taxonomy_term_delete().
52
 */
53
function taxonomy_test_taxonomy_term_delete($term) {
54
  db_delete('taxonomy_term_antonym')
55
    ->condition('tid', $term->tid)
56
    ->execute();
57
}
58

    
59
/**
60
 * Implements hook_taxonomy_term_view().
61
 */
62
function taxonomy_test_taxonomy_term_view($term, $view_mode, $langcode) {
63
  if ($view_mode == 'full') {
64
    $term->content['taxonomy_test_term_view_check'] = array(
65
      '#prefix' => '<div>',
66
      '#markup' => t('The antonym is %antonym', array('%antonym' => $term->antonym)),
67
      '#suffix' => '</div>',
68
      '#weight' => 10,
69
    );
70
  }
71
}
72

    
73
/**
74
 * Implements hook_entity_view().
75
 */
76
function taxonomy_test_entity_view($entity, $type, $view_mode, $langcode) {
77
  if ($type == 'taxonomy_term' && $view_mode == 'full') {
78
    $entity->content['taxonomy_test_entity_view_check'] = array(
79
      '#prefix' => '<div>',
80
      '#markup' => t('The antonym is %antonym', array('%antonym' => $entity->antonym)),
81
      '#suffix' => '</div>',
82
      '#weight' => 20,
83
    );
84
  }
85
}
86

    
87
/**
88
 * Implements hook_form_alter().
89
 */
90
function taxonomy_test_form_alter(&$form, $form_state, $form_id) {
91
  if ($form_id == 'taxonomy_form_term') {
92
    $antonym = taxonomy_test_get_antonym($form['#term']['tid']);
93
    $form['advanced']['antonym'] = array(
94
      '#type' => 'textfield',
95
      '#title' => t('Antonym'),
96
      '#default_value' => !empty($antonym) ? $antonym : '',
97
      '#description' => t('Antonym of this term.')
98
    );
99
  }
100
}
101

    
102
/**
103
 * Return the antonym of the given term ID.
104
 */
105
function taxonomy_test_get_antonym($tid) {
106
  return db_select('taxonomy_term_antonym', 'ta')
107
    ->fields('ta', array('name'))
108
    ->condition('tid', $tid)
109
    ->execute()
110
    ->fetchField();
111
}