taxonomy.api.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Taxonomy module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Act on taxonomy vocabularies when loaded.
  12. *
  13. * Modules implementing this hook can act on the vocabulary objects before they
  14. * are returned by taxonomy_vocabulary_load_multiple().
  15. *
  16. * @param $vocabulary
  17. * An array of taxonomy vocabulary objects.
  18. */
  19. function hook_taxonomy_vocabulary_load($vocabularies) {
  20. foreach ($vocabularies as $vocabulary) {
  21. $vocabulary->synonyms = variable_get('taxonomy_' . $vocabulary->vid . '_synonyms', FALSE);
  22. }
  23. }
  24. /**
  25. * Act on taxonomy vocabularies before they are saved.
  26. *
  27. * Modules implementing this hook can act on the vocabulary object before it is
  28. * inserted or updated.
  29. *
  30. * @param $vocabulary
  31. * A taxonomy vocabulary object.
  32. */
  33. function hook_taxonomy_vocabulary_presave($vocabulary) {
  34. $vocabulary->foo = 'bar';
  35. }
  36. /**
  37. * Act on taxonomy vocabularies when inserted.
  38. *
  39. * Modules implementing this hook can act on the vocabulary object when saved
  40. * to the database.
  41. *
  42. * @param $vocabulary
  43. * A taxonomy vocabulary object.
  44. */
  45. function hook_taxonomy_vocabulary_insert($vocabulary) {
  46. if ($vocabulary->synonyms) {
  47. variable_set('taxonomy_' . $vocabulary->vid . '_synonyms', TRUE);
  48. }
  49. }
  50. /**
  51. * Act on taxonomy vocabularies when updated.
  52. *
  53. * Modules implementing this hook can act on the vocabulary object when updated.
  54. *
  55. * @param $vocabulary
  56. * A taxonomy vocabulary object.
  57. */
  58. function hook_taxonomy_vocabulary_update($vocabulary) {
  59. $status = $vocabulary->synonyms ? TRUE : FALSE;
  60. if ($vocabulary->synonyms) {
  61. variable_set('taxonomy_' . $vocabulary->vid . '_synonyms', $status);
  62. }
  63. }
  64. /**
  65. * Respond to the deletion of taxonomy vocabularies.
  66. *
  67. * Modules implementing this hook can respond to the deletion of taxonomy
  68. * vocabularies from the database.
  69. *
  70. * @param $vocabulary
  71. * A taxonomy vocabulary object.
  72. */
  73. function hook_taxonomy_vocabulary_delete($vocabulary) {
  74. if (variable_get('taxonomy_' . $vocabulary->vid . '_synonyms', FALSE)) {
  75. variable_del('taxonomy_' . $vocabulary->vid . '_synonyms');
  76. }
  77. }
  78. /**
  79. * Act on taxonomy terms when loaded.
  80. *
  81. * Modules implementing this hook can act on the term objects returned by
  82. * taxonomy_term_load_multiple().
  83. *
  84. * For performance reasons, information to be added to term objects should be
  85. * loaded in a single query for all terms where possible.
  86. *
  87. * Since terms are stored and retrieved from cache during a page request, avoid
  88. * altering properties provided by the {taxonomy_term_data} table, since this
  89. * may affect the way results are loaded from cache in subsequent calls.
  90. *
  91. * @param $terms
  92. * An array of term objects, indexed by tid.
  93. */
  94. function hook_taxonomy_term_load($terms) {
  95. $result = db_query('SELECT tid, foo FROM {mytable} WHERE tid IN (:tids)', array(':tids' => array_keys($terms)));
  96. foreach ($result as $record) {
  97. $terms[$record->tid]->foo = $record->foo;
  98. }
  99. }
  100. /**
  101. * Act on taxonomy terms before they are saved.
  102. *
  103. * Modules implementing this hook can act on the term object before it is
  104. * inserted or updated.
  105. *
  106. * @param $term
  107. * A term object.
  108. */
  109. function hook_taxonomy_term_presave($term) {
  110. $term->foo = 'bar';
  111. }
  112. /**
  113. * Act on taxonomy terms when inserted.
  114. *
  115. * Modules implementing this hook can act on the term object when saved to
  116. * the database.
  117. *
  118. * @param $term
  119. * A taxonomy term object.
  120. */
  121. function hook_taxonomy_term_insert($term) {
  122. if (!empty($term->synonyms)) {
  123. foreach (explode ("\n", str_replace("\r", '', $term->synonyms)) as $synonym) {
  124. if ($synonym) {
  125. db_insert('taxonomy_term_synonym')
  126. ->fields(array(
  127. 'tid' => $term->tid,
  128. 'name' => rtrim($synonym),
  129. ))
  130. ->execute();
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Act on taxonomy terms when updated.
  137. *
  138. * Modules implementing this hook can act on the term object when updated.
  139. *
  140. * @param $term
  141. * A taxonomy term object.
  142. */
  143. function hook_taxonomy_term_update($term) {
  144. hook_taxonomy_term_delete($term);
  145. if (!empty($term->synonyms)) {
  146. foreach (explode ("\n", str_replace("\r", '', $term->synonyms)) as $synonym) {
  147. if ($synonym) {
  148. db_insert('taxonomy_term_synonym')
  149. ->fields(array(
  150. 'tid' => $term->tid,
  151. 'name' => rtrim($synonym),
  152. ))
  153. ->execute();
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. * Respond to the deletion of taxonomy terms.
  160. *
  161. * Modules implementing this hook can respond to the deletion of taxonomy
  162. * terms from the database.
  163. *
  164. * @param $term
  165. * A taxonomy term object.
  166. */
  167. function hook_taxonomy_term_delete($term) {
  168. db_delete('term_synoynm')->condition('tid', $term->tid)->execute();
  169. }
  170. /**
  171. * Act on a taxonomy term that is being assembled before rendering.
  172. *
  173. * The module may add elements to $term->content prior to rendering. The
  174. * structure of $term->content is a renderable array as expected by
  175. * drupal_render().
  176. *
  177. * @param $term
  178. * The term that is being assembled for rendering.
  179. * @param $view_mode
  180. * The $view_mode parameter from taxonomy_term_view().
  181. * @param $langcode
  182. * The language code used for rendering.
  183. *
  184. * @see hook_entity_view()
  185. */
  186. function hook_taxonomy_term_view($term, $view_mode, $langcode) {
  187. $term->content['my_additional_field'] = array(
  188. '#markup' => $additional_field,
  189. '#weight' => 10,
  190. '#theme' => 'mymodule_my_additional_field',
  191. );
  192. }
  193. /**
  194. * Alter the results of taxonomy_term_view().
  195. *
  196. * This hook is called after the content has been assembled in a structured
  197. * array and may be used for doing processing which requires that the complete
  198. * taxonomy term content structure has been built.
  199. *
  200. * If the module wishes to act on the rendered HTML of the term rather than the
  201. * structured content array, it may use this hook to add a #post_render
  202. * callback. Alternatively, it could also implement
  203. * hook_preprocess_taxonomy_term(). See drupal_render() and theme()
  204. * documentation respectively for details.
  205. *
  206. * @param $build
  207. * A renderable array representing the node content.
  208. *
  209. * @see hook_entity_view_alter()
  210. */
  211. function hook_taxonomy_term_view_alter(&$build) {
  212. if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
  213. // Change its weight.
  214. $build['an_additional_field']['#weight'] = -10;
  215. }
  216. // Add a #post_render callback to act on the rendered HTML of the term.
  217. $build['#post_render'][] = 'my_module_node_post_render';
  218. }
  219. /**
  220. * @} End of "addtogroup hooks".
  221. */