Issue #1831540 by orb, sergeypavlenko, Berdir, andypost, podarok, cam8001, Gaelan: Rewrite examples for taxonomy hooks in taxonomy.api.php

merge-requests/26/head
Jennifer Hodgdon 2013-06-20 07:20:12 -07:00
parent 0a14ec9418
commit c23de5265f
1 changed files with 32 additions and 39 deletions

View File

@ -20,12 +20,15 @@
* An array of taxonomy vocabulary objects.
*/
function hook_taxonomy_vocabulary_load($vocabularies) {
foreach ($vocabularies as $vocabulary) {
$vocabulary->synonyms = variable_get('taxonomy_' . $vocabulary->vid . '_synonyms', FALSE);
$result = db_select('mytable', 'm')
->fields('m', array('vid', 'foo'))
->condition('m.vid', array_keys($vocabularies), 'IN')
->execute();
foreach ($result as $record) {
$vocabularies[$record->vid]->foo = $record->foo;
}
}
/**
* Act on taxonomy vocabularies before they are saved.
*
@ -49,8 +52,8 @@ function hook_taxonomy_vocabulary_presave($vocabulary) {
* A taxonomy vocabulary object.
*/
function hook_taxonomy_vocabulary_insert($vocabulary) {
if ($vocabulary->synonyms) {
variable_set('taxonomy_' . $vocabulary->vid . '_synonyms', TRUE);
if ($vocabulary->machine_name == 'my_vocabulary') {
$vocabulary->weight = 100;
}
}
@ -63,10 +66,10 @@ function hook_taxonomy_vocabulary_insert($vocabulary) {
* A taxonomy vocabulary object.
*/
function hook_taxonomy_vocabulary_update($vocabulary) {
$status = $vocabulary->synonyms ? TRUE : FALSE;
if ($vocabulary->synonyms) {
variable_set('taxonomy_' . $vocabulary->vid . '_synonyms', $status);
}
db_update('mytable')
->fields(array('foo' => $vocabulary->foo))
->condition('vid', $vocabulary->vid)
->execute();
}
/**
@ -79,9 +82,9 @@ function hook_taxonomy_vocabulary_update($vocabulary) {
* A taxonomy vocabulary object.
*/
function hook_taxonomy_vocabulary_delete($vocabulary) {
if (variable_get('taxonomy_' . $vocabulary->vid . '_synonyms', FALSE)) {
variable_del('taxonomy_' . $vocabulary->vid . '_synonyms');
}
db_delete('mytable')
->condition('vid', $vocabulary->vid)
->execute();
}
/**
@ -101,7 +104,10 @@ function hook_taxonomy_vocabulary_delete($vocabulary) {
* An array of term objects, indexed by tid.
*/
function hook_taxonomy_term_load($terms) {
$result = db_query('SELECT tid, foo FROM {mytable} WHERE tid IN (:tids)', array(':tids' => array_keys($terms)));
$result = db_select('mytable', 'm')
->fields('m', array('tid', 'foo'))
->condition('m.tid', array_keys($terms), 'IN')
->execute();
foreach ($result as $record) {
$terms[$record->tid]->foo = $record->foo;
}
@ -130,19 +136,13 @@ function hook_taxonomy_term_presave($term) {
* A taxonomy term object.
*/
function hook_taxonomy_term_insert($term) {
if (!empty($term->synonyms)) {
foreach (explode ("\n", str_replace("\r", '', $term->synonyms)) as $synonym) {
if ($synonym) {
db_insert('taxonomy_term_synonym')
db_insert('mytable')
->fields(array(
'tid' => $term->tid,
'name' => rtrim($synonym),
'foo' => $term->foo,
))
->execute();
}
}
}
}
/**
* Act on taxonomy terms when updated.
@ -153,20 +153,11 @@ function hook_taxonomy_term_insert($term) {
* A taxonomy term object.
*/
function hook_taxonomy_term_update($term) {
hook_taxonomy_term_delete($term);
if (!empty($term->synonyms)) {
foreach (explode ("\n", str_replace("\r", '', $term->synonyms)) as $synonym) {
if ($synonym) {
db_insert('taxonomy_term_synonym')
->fields(array(
'tid' => $term->tid,
'name' => rtrim($synonym),
))
db_update('mytable')
->fields(array('foo' => $term->foo))
->condition('tid', $term->tid)
->execute();
}
}
}
}
/**
* Respond to the deletion of taxonomy terms.
@ -178,7 +169,9 @@ function hook_taxonomy_term_update($term) {
* A taxonomy term object.
*/
function hook_taxonomy_term_delete($term) {
db_delete('term_synoynm')->condition('tid', $term->tid)->execute();
db_delete('mytable')
->condition('tid', $term->tid)
->execute();
}
/**