taxonomy.api.php

Hooks provided by the Taxonomy module.

File

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