| 1 |
<?php
|
| 2 |
// $Id: taxonomy.api.php,v 1.6 2009/01/14 21:16:20 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Hooks provided by the Taxonomy module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* @addtogroup hooks
|
| 11 |
* @{
|
| 12 |
*/
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Act on taxonomy vocabularies when loaded.
|
| 16 |
*
|
| 17 |
* Modules implementing this hook can act on the vocabulary object returned by
|
| 18 |
* taxonomy_vocabulary_load().
|
| 19 |
*
|
| 20 |
* @param $vocabulary
|
| 21 |
* A taxonomy vocabulary object.
|
| 22 |
*/
|
| 23 |
function hook_taxonomy_vocabulary_load($vocabularies) {
|
| 24 |
foreach ($vocabularies as $vocabulary) {
|
| 25 |
$vocabulary->synonyms = variable_get('taxonomy_' . $vocabulary->vid . '_synonyms', FALSE);
|
| 26 |
}
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Act on taxonomy vocabularies when inserted.
|
| 31 |
*
|
| 32 |
* Modules implementing this hook can act on the vocabulary object when saved
|
| 33 |
* to the database.
|
| 34 |
*
|
| 35 |
* @param $vocabulary
|
| 36 |
* A taxonomy vocabulary object.
|
| 37 |
*/
|
| 38 |
function hook_taxonomy_vocabulary_insert($vocabulary) {
|
| 39 |
if ($vocabulary->synonyms) {
|
| 40 |
variable_set('taxonomy_' . $vocabulary->vid . '_synonyms', TRUE);
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Act on taxonomy vocabularies when updated.
|
| 46 |
*
|
| 47 |
* Modules implementing this hook can act on the term object when updated.
|
| 48 |
*
|
| 49 |
* @param $term
|
| 50 |
* A taxonomy term object, passed by reference.
|
| 51 |
*/
|
| 52 |
function hook_taxonomy_vocabulary_update($term) {
|
| 53 |
$status = $vocabulary->synonyms ? TRUE : FALSE;
|
| 54 |
if ($vocabulary->synonyms) {
|
| 55 |
variable_set('taxonomy_' . $vocabulary->vid . '_synonyms', $status);
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
* Respond to the deletion of taxonomy vocabularies.
|
| 61 |
*
|
| 62 |
* Modules implementing this hook can respond to the deletion of taxonomy
|
| 63 |
* vocabularies from the database.
|
| 64 |
*
|
| 65 |
* @param $vocabulary
|
| 66 |
* A taxonomy vocabulary object.
|
| 67 |
*/
|
| 68 |
function hook_taxonomy_vocabulary_delete($vocabulary) {
|
| 69 |
if (variable_get('taxonomy_' . $vocabulary->vid . '_synonyms', FALSE)) {
|
| 70 |
variable_del('taxonomy_' . $vocabulary->vid . '_synonyms');
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
/**
|
| 75 |
* Act on taxonomy terms when loaded.
|
| 76 |
*
|
| 77 |
* Modules implementing this hook can act on the term object returned by
|
| 78 |
* taxonomy_term_load().
|
| 79 |
* For performance reasons, information to be added to term objects should be
|
| 80 |
* loaded in a single query for all terms where possible.
|
| 81 |
*
|
| 82 |
* Since terms are stored and retrieved from cache during a page request, avoid
|
| 83 |
* altering properties provided by the {taxonomy_term_data} table, since this may
|
| 84 |
* affect the way results are loaded from cache in subsequent calls.
|
| 85 |
*
|
| 86 |
* @param $terms
|
| 87 |
* An array of term objects, indexed by tid.
|
| 88 |
*/
|
| 89 |
function hook_taxonomy_term_load($terms) {
|
| 90 |
$result = db_query('SELECT tid, foo FROM {mytable} WHERE tid IN (:tids)', array(':tids' => array_keys($terms)));
|
| 91 |
foreach ($result as $record) {
|
| 92 |
$terms[$record->tid]->foo = $record->foo;
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Act on taxonomy terms when inserted.
|
| 98 |
*
|
| 99 |
* Modules implementing this hook can act on the term object when saved to
|
| 100 |
* the database.
|
| 101 |
*
|
| 102 |
* @param $term
|
| 103 |
* A taxonomy term object.
|
| 104 |
*/
|
| 105 |
function hook_taxonomy_term_insert($term) {
|
| 106 |
if (!empty($term->synonyms)) {
|
| 107 |
foreach (explode ("\n", str_replace("\r", '', $term->synonyms)) as $synonym) {
|
| 108 |
if ($synonym) {
|
| 109 |
db_insert('taxonomy_term_synonym')
|
| 110 |
->fields(array(
|
| 111 |
'tid' => $term->tid,
|
| 112 |
'name' => rtrim($synonym),
|
| 113 |
))
|
| 114 |
->execute();
|
| 115 |
}
|
| 116 |
}
|
| 117 |
}
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* Act on taxonomy terms when updated.
|
| 122 |
*
|
| 123 |
* Modules implementing this hook can act on the term object when updated.
|
| 124 |
*
|
| 125 |
* @param $term
|
| 126 |
* A taxonomy term object.
|
| 127 |
*/
|
| 128 |
function hook_taxonomy_term_update($term) {
|
| 129 |
hook_taxonomy_term_delete($term);
|
| 130 |
if (!empty($term->synonyms)) {
|
| 131 |
foreach (explode ("\n", str_replace("\r", '', $term->synonyms)) as $synonym) {
|
| 132 |
if ($synonym) {
|
| 133 |
db_insert('taxonomy_term_synonym')
|
| 134 |
->fields(array(
|
| 135 |
'tid' => $term->tid,
|
| 136 |
'name' => rtrim($synonym),
|
| 137 |
))
|
| 138 |
->execute();
|
| 139 |
}
|
| 140 |
}
|
| 141 |
}
|
| 142 |
}
|
| 143 |
|
| 144 |
/**
|
| 145 |
* Respond to the deletion of taxonomy terms.
|
| 146 |
*
|
| 147 |
* Modules implementing this hook can respond to the deletion of taxonomy
|
| 148 |
* terms from the database.
|
| 149 |
*
|
| 150 |
* @param $term
|
| 151 |
* A taxonomy term object.
|
| 152 |
*/
|
| 153 |
function hook_taxonomy_term_delete($term) {
|
| 154 |
db_delete('term_synoynm')->condition('tid', $term->tid)->execute();
|
| 155 |
}
|
| 156 |
|
| 157 |
/**
|
| 158 |
* @} End of "addtogroup hooks".
|
| 159 |
*/
|