taxonomy_test.module 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @file
  4. * Test module for Taxonomy hooks and functions not used in core.
  5. */
  6. /**
  7. * Implements hook_taxonomy_term_load().
  8. */
  9. function taxonomy_test_taxonomy_term_load($terms) {
  10. foreach ($terms as $term) {
  11. $antonym = taxonomy_test_get_antonym($term->tid);
  12. if ($antonym) {
  13. $term->antonym = $antonym;
  14. }
  15. }
  16. }
  17. /**
  18. * Implements hook_taxonomy_term_insert().
  19. */
  20. function taxonomy_test_taxonomy_term_insert($term) {
  21. if (!empty($term->antonym)) {
  22. db_insert('taxonomy_term_antonym')
  23. ->fields(array(
  24. 'tid' => $term->tid,
  25. 'name' => trim($term->antonym)
  26. ))
  27. ->execute();
  28. }
  29. }
  30. /**
  31. * Implements hook_taxonomy_term_update().
  32. */
  33. function taxonomy_test_taxonomy_term_update($term) {
  34. if (!empty($term->antonym)) {
  35. db_merge('taxonomy_term_antonym')
  36. ->key(array('tid' => $term->tid))
  37. ->fields(array(
  38. 'name' => trim($term->antonym)
  39. ))
  40. ->execute();
  41. }
  42. }
  43. /**
  44. * Implements hook_taxonomy_term_delete().
  45. */
  46. function taxonomy_test_taxonomy_term_delete($term) {
  47. db_delete('taxonomy_term_antonym')
  48. ->condition('tid', $term->tid)
  49. ->execute();
  50. }
  51. /**
  52. * Implements hook_form_alter().
  53. */
  54. function taxonomy_test_form_alter(&$form, $form_state, $form_id) {
  55. if ($form_id == 'taxonomy_form_term') {
  56. $antonym = taxonomy_test_get_antonym($form['#term']['tid']);
  57. $form['advanced']['antonym'] = array(
  58. '#type' => 'textfield',
  59. '#title' => t('Antonym'),
  60. '#default_value' => !empty($antonym) ? $antonym : '',
  61. '#description' => t('Antonym of this term.')
  62. );
  63. }
  64. }
  65. /**
  66. * Return the antonym of the given term ID.
  67. */
  68. function taxonomy_test_get_antonym($tid) {
  69. return db_select('taxonomy_term_antonym', 'ta')
  70. ->fields('ta', array('name'))
  71. ->condition('tid', $tid)
  72. ->execute()
  73. ->fetchField();
  74. }