taxonomy_test.module 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Test module for Taxonomy hooks and functions not used in core.
  5. *
  6. * @see TaxonomyHooksTestCase::testTaxonomyTermHooks()
  7. */
  8. /**
  9. * Implements hook_taxonomy_term_load().
  10. */
  11. function taxonomy_test_taxonomy_term_load($terms) {
  12. foreach ($terms as $term) {
  13. $antonym = taxonomy_test_get_antonym($term->tid);
  14. if ($antonym) {
  15. $term->antonym = $antonym;
  16. }
  17. }
  18. }
  19. /**
  20. * Implements hook_taxonomy_term_insert().
  21. */
  22. function taxonomy_test_taxonomy_term_insert($term) {
  23. if (!empty($term->antonym)) {
  24. db_insert('taxonomy_term_antonym')
  25. ->fields(array(
  26. 'tid' => $term->tid,
  27. 'name' => trim($term->antonym)
  28. ))
  29. ->execute();
  30. }
  31. }
  32. /**
  33. * Implements hook_taxonomy_term_update().
  34. */
  35. function taxonomy_test_taxonomy_term_update($term) {
  36. if (!empty($term->antonym)) {
  37. db_merge('taxonomy_term_antonym')
  38. ->key(array('tid' => $term->tid))
  39. ->fields(array(
  40. 'name' => trim($term->antonym)
  41. ))
  42. ->execute();
  43. }
  44. }
  45. /**
  46. * Implements hook_taxonomy_term_delete().
  47. */
  48. function taxonomy_test_taxonomy_term_delete($term) {
  49. db_delete('taxonomy_term_antonym')
  50. ->condition('tid', $term->tid)
  51. ->execute();
  52. }
  53. /**
  54. * Implements hook_taxonomy_term_view().
  55. */
  56. function taxonomy_test_taxonomy_term_view($term, $view_mode, $langcode) {
  57. if ($view_mode == 'full') {
  58. $term->content['taxonomy_test_term_view_check'] = array(
  59. '#prefix' => '<div>',
  60. '#markup' => t('The antonym is %antonym', array('%antonym' => $term->antonym)),
  61. '#suffix' => '</div>',
  62. '#weight' => 10,
  63. );
  64. }
  65. }
  66. /**
  67. * Implements hook_entity_view().
  68. */
  69. function taxonomy_test_entity_view($entity, $type, $view_mode, $langcode) {
  70. if ($type == 'taxonomy_term' && $view_mode == 'full') {
  71. $entity->content['taxonomy_test_entity_view_check'] = array(
  72. '#prefix' => '<div>',
  73. '#markup' => t('The antonym is %antonym', array('%antonym' => $entity->antonym)),
  74. '#suffix' => '</div>',
  75. '#weight' => 20,
  76. );
  77. }
  78. }
  79. /**
  80. * Implements hook_form_alter().
  81. */
  82. function taxonomy_test_form_alter(&$form, $form_state, $form_id) {
  83. if ($form_id == 'taxonomy_form_term') {
  84. $antonym = taxonomy_test_get_antonym($form['#term']['tid']);
  85. $form['advanced']['antonym'] = array(
  86. '#type' => 'textfield',
  87. '#title' => t('Antonym'),
  88. '#default_value' => !empty($antonym) ? $antonym : '',
  89. '#description' => t('Antonym of this term.')
  90. );
  91. }
  92. }
  93. /**
  94. * Return the antonym of the given term ID.
  95. */
  96. function taxonomy_test_get_antonym($tid) {
  97. return db_select('taxonomy_term_antonym', 'ta')
  98. ->fields('ta', array('name'))
  99. ->condition('tid', $tid)
  100. ->execute()
  101. ->fetchField();
  102. }
  103. /**
  104. * Implements hook_query_alter().
  105. */
  106. function taxonomy_test_query_alter(QueryAlterableInterface $query) {
  107. $value = variable_get(__FUNCTION__);
  108. if (isset($value)) {
  109. variable_set(__FUNCTION__, ++$value);
  110. }
  111. }
  112. /**
  113. * Implements hook_query_TAG_alter().
  114. */
  115. function taxonomy_test_query_term_access_alter(QueryAlterableInterface $query) {
  116. $value = variable_get(__FUNCTION__);
  117. if (isset($value)) {
  118. variable_set(__FUNCTION__, ++$value);
  119. }
  120. }
  121. /**
  122. * Implements hook_query_TAG_alter().
  123. */
  124. function taxonomy_test_query_taxonomy_term_access_alter(QueryAlterableInterface $query) {
  125. $value = variable_get(__FUNCTION__);
  126. if (isset($value)) {
  127. variable_set(__FUNCTION__, ++$value);
  128. }
  129. }