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