taxonomy_test.module

Test module for Taxonomy hooks and functions not used in core.

See also

TaxonomyHooksTestCase::testTaxonomyTermHooks()

File

drupal-7.x/modules/simpletest/tests/taxonomy_test.module
View source
  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. }