From bd22265b458032ad71f22bb471ed69995874d37c Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Sun, 2 Nov 2008 14:42:45 +0000 Subject: - Patch #306224 by catch et al: improving the taxonomy hook system. --- modules/simpletest/tests/taxonomy_test.module | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 modules/simpletest/tests/taxonomy_test.module (limited to 'modules/simpletest/tests/taxonomy_test.module') diff --git a/modules/simpletest/tests/taxonomy_test.module b/modules/simpletest/tests/taxonomy_test.module new file mode 100644 index 000000000..7f5b6f4df --- /dev/null +++ b/modules/simpletest/tests/taxonomy_test.module @@ -0,0 +1,72 @@ +antonyms = taxonomy_test_get_antonyms($term->tid); +} + +/** + * Implementation of hook_taxonomy_term_save(). + */ +function taxonomy_test_taxonomy_term_save($term) { + taxonomy_test_taxonomy_term_delete($term); + if (!empty($term->antonyms)) { + foreach (explode ("\n", str_replace("\r", '', $term->antonyms)) as $antonym) { + if ($antonym) { + db_insert('term_antonym') + ->fields(array( + 'tid' => $term->tid, + 'name' => rtrim($antonym), + )) + ->execute(); + } + } + } +} + +/** + * Implementation of hook_taxonomy_term_delete(). + */ +function taxonomy_test_taxonomy_term_delete($term) { + db_delete('term_antonym')->condition('tid', $term->tid)->execute(); +} + +/** + * Implementation of hook_form_alter(). + */ +function taxonomy_test_form_alter(&$form, $form_state, $form_id) { + if ($form_id == 'taxonomy_form_term') { + $antonyms = taxonomy_test_get_antonyms($form['#term']['tid']); + $form['advanced']['antonyms'] = array( + '#type' => 'textarea', + '#title' => t('Antonyms'), + '#default_value' => !empty($antonyms) ? implode("\n", $antonyms) : NULL, + '#description' => t('Antonyms of this term, one antonym per line.') + ); + } +} + +/** + * Return an array of antonyms of the given term ID. + */ +function taxonomy_test_get_antonyms($tid) { + if ($tid) { + $antonyms = array(); + $result = db_query('SELECT name FROM {term_antonym} WHERE tid = :tid', array(':tid' => $tid)); + foreach($result as $antonym) { + $antonyms[] = $antonym->name; + } + return $antonyms; + } + else { + return FALSE; + } +} -- cgit v1.2.3