2002-04-14 20:46:41 +00:00
<?php
2002-05-02 21:41:29 +00:00
// $Id$
2002-04-20 11:52:50 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
* Enables the organization of content into categories.
*/
2004-06-18 15:04:37 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_perm().
2004-06-18 15:04:37 +00:00
*/
2002-05-02 21:41:29 +00:00
function taxonomy_perm() {
2008-02-20 13:46:43 +00:00
return array(
2008-10-09 15:15:55 +00:00
'administer taxonomy' => array(
'title' => t('Administer taxonomy'),
'description' => t('Manage taxonomy vocabularies and terms.'),
),
2008-02-20 13:46:43 +00:00
);
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2007-04-06 13:27:23 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_theme().
2007-04-06 13:27:23 +00:00
*/
function taxonomy_theme() {
return array(
'taxonomy_term_select' => array(
'arguments' => array('element' => NULL),
2007-11-04 15:10:09 +00:00
),
2007-11-26 19:46:52 +00:00
'taxonomy_overview_vocabularies' => array(
'arguments' => array('form' => array()),
),
'taxonomy_overview_terms' => array(
'arguments' => array('form' => array()),
),
2007-04-06 13:27:23 +00:00
);
}
2004-04-21 13:56:38 +00:00
/**
2009-03-08 04:25:07 +00:00
* An implementation of hook_node_view().
2004-04-21 13:56:38 +00:00
*/
2009-03-08 04:25:07 +00:00
function taxonomy_node_view($node) {
2009-05-03 10:11:35 +00:00
if (empty($node->taxonomy)) {
return;
2008-12-16 22:05:51 +00:00
}
2009-05-03 10:11:35 +00:00
if ($node->build_mode == NODE_BUILD_RSS) {
// Provide category information for RSS feeds.
foreach ($node->taxonomy as $term) {
$node->rss_elements[] = array(
'key' => 'category',
'value' => $term->name,
'attributes' => array('domain' => url(taxonomy_term_path($term), array('absolute' => TRUE))),
);
}
}
else {
$links = array();
// If previewing, the terms must be converted to objects first.
if ($node->build_mode == NODE_BUILD_PREVIEW) {
$node->taxonomy = taxonomy_preview_terms($node);
}
2008-12-16 22:05:51 +00:00
foreach ($node->taxonomy as $term) {
// During preview the free tagging terms are in an array unlike the
// other terms which are objects. So we have to check if a $term
// is an object or not.
if (is_object($term)) {
$links['taxonomy_term_' . $term->tid] = array(
'title' => $term->name,
'href' => taxonomy_term_path($term),
'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description))
);
}
// Previewing free tagging terms; we don't link them because the
// term-page might not exist yet.
else {
foreach ($term as $free_typed) {
$typed_terms = drupal_explode_tags($free_typed);
foreach ($typed_terms as $typed_term) {
$links['taxonomy_preview_term_' . $typed_term] = array(
'title' => $typed_term,
);
2007-11-14 13:32:58 +00:00
}
2007-03-27 05:13:55 +00:00
}
2003-04-15 19:50:04 +00:00
}
2003-02-09 17:39:40 +00:00
}
2009-05-24 17:39:35 +00:00
2009-05-03 10:11:35 +00:00
$node->content['links']['terms'] = array(
'#type' => 'node_links',
'#value' => $links,
'#sorted' => TRUE,
);
}
2005-10-21 11:12:46 +00:00
}
2006-12-20 10:32:16 +00:00
/**
* For vocabularies not maintained by taxonomy.module, give the maintaining
* module a chance to provide a path for terms in that vocabulary.
*
* @param $term
* A term object.
* @return
* An internal Drupal path.
*/
2006-07-02 01:13:45 +00:00
function taxonomy_term_path($term) {
2007-02-11 09:30:51 +00:00
$vocabulary = taxonomy_vocabulary_load($term->vid);
2006-07-02 01:13:45 +00:00
if ($vocabulary->module != 'taxonomy' && $path = module_invoke($vocabulary->module, 'term_path', $term)) {
return $path;
}
2008-04-14 17:48:46 +00:00
return 'taxonomy/term/' . $term->tid;
2006-07-02 01:13:45 +00:00
}
2004-06-18 15:04:37 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_menu().
2004-06-18 15:04:37 +00:00
*/
2007-01-24 14:48:36 +00:00
function taxonomy_menu() {
$items['admin/content/taxonomy'] = array(
2007-11-19 18:26:11 +00:00
'title' => 'Taxonomy',
'description' => 'Manage tagging, categorization, and classification of your content.',
2007-11-26 19:46:52 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_overview_vocabularies'),
2007-01-24 14:48:36 +00:00
'access arguments' => array('administer taxonomy'),
);
$items['admin/content/taxonomy/list'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'List',
2007-01-24 14:48:36 +00:00
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
2008-09-19 20:25:03 +00:00
$items['admin/content/taxonomy/add'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Add vocabulary',
2007-01-24 14:48:36 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_form_vocabulary'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer taxonomy'),
2007-01-24 14:48:36 +00:00
'type' => MENU_LOCAL_TASK,
);
2008-09-19 20:25:03 +00:00
$items['taxonomy/term/%taxonomy_terms'] = array(
'title' => 'Taxonomy term',
'page callback' => 'taxonomy_term_page',
'page arguments' => array(2),
'access arguments' => array('access content'),
2007-01-24 14:48:36 +00:00
'type' => MENU_CALLBACK,
);
2008-09-19 20:25:03 +00:00
$items['taxonomy/term/%taxonomy_terms/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
2007-01-24 14:48:36 +00:00
);
2008-09-19 20:25:03 +00:00
$items['taxonomy/term/%taxonomy_term/edit'] = array(
'title' => 'Edit term',
'page callback' => 'taxonomy_term_edit',
2007-12-14 11:19:39 +00:00
'page arguments' => array(2),
2008-09-19 20:25:03 +00:00
'access arguments' => array('administer taxonomy'),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
2007-01-24 14:48:36 +00:00
);
$items['taxonomy/autocomplete'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Autocomplete taxonomy',
2007-01-24 14:48:36 +00:00
'page callback' => 'taxonomy_autocomplete',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
2008-09-19 20:25:03 +00:00
2007-02-11 09:30:51 +00:00
$items['admin/content/taxonomy/%taxonomy_vocabulary'] = array(
2008-09-19 20:25:03 +00:00
'title' => 'Vocabulary', // this is replaced by callback
2007-11-26 19:46:52 +00:00
'page callback' => 'drupal_get_form',
2008-09-19 20:25:03 +00:00
'page arguments' => array('taxonomy_form_vocabulary', 3),
'title callback' => 'taxonomy_admin_vocabulary_title_callback',
'title arguments' => array(3),
2007-01-24 14:48:36 +00:00
'access arguments' => array('administer taxonomy'),
'type' => MENU_CALLBACK,
);
2008-09-19 20:25:03 +00:00
$items['admin/content/taxonomy/%taxonomy_vocabulary/edit'] = array(
'title' => 'Edit vocabulary',
2007-01-24 14:48:36 +00:00
'type' => MENU_DEFAULT_LOCAL_TASK,
2008-09-19 20:25:03 +00:00
'weight' => -20,
);
$items['admin/content/taxonomy/%taxonomy_vocabulary/list'] = array(
'title' => 'List terms',
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_overview_terms', 3),
'access arguments' => array('administer taxonomy'),
'type' => MENU_LOCAL_TASK,
2007-01-24 14:48:36 +00:00
'weight' => -10,
);
2008-09-19 20:25:03 +00:00
$items['admin/content/taxonomy/%taxonomy_vocabulary/add'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Add term',
2008-09-19 20:25:03 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_form_term', 3),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer taxonomy'),
2007-01-24 14:48:36 +00:00
'type' => MENU_LOCAL_TASK,
);
2004-09-16 07:17:56 +00:00
2004-06-18 15:04:37 +00:00
return $items;
}
2002-04-14 20:46:41 +00:00
2008-09-19 20:25:03 +00:00
/**
* Return the vocabulary name given the vocabulary object.
*/
function taxonomy_admin_vocabulary_title_callback($vocabulary) {
return check_plain($vocabulary->name);
}
/**
2008-11-07 18:52:18 +00:00
* Save a vocabulary given a vocabulary object.
2008-09-19 20:25:03 +00:00
*/
2008-11-05 12:47:23 +00:00
function taxonomy_vocabulary_save($vocabulary) {
if (empty($vocabulary->nodes)) {
$vocabulary->nodes = array();
}
2008-11-11 16:49:38 +00:00
2008-11-07 18:52:18 +00:00
if (!empty($vocabulary->name)) {
// Prevent leading and trailing spaces in vocabulary names.
$vocabulary->name = trim($vocabulary->name);
}
2003-05-10 14:56:23 +00:00
2008-11-05 12:47:23 +00:00
if (!isset($vocabulary->module)) {
$vocabulary->module = 'taxonomy';
2007-10-02 16:15:56 +00:00
}
2008-11-05 12:47:23 +00:00
if (!empty($vocabulary->vid) && !empty($vocabulary->name)) {
2009-01-14 21:16:21 +00:00
$status = drupal_write_record('taxonomy_vocabulary', $vocabulary, 'vid');
2009-04-15 14:12:55 +00:00
db_delete('taxonomy_vocabulary_node_type')
->condition('vid', $vocabulary->vid)
->execute();
if (!empty($vocabulary->nodes)) {
$query = db_insert('taxonomy_vocabulary_node_type')
->fields(array('vid', 'type'));
foreach ($vocabulary->nodes as $type => $selected) {
$query->values(array(
'vid' => $vocabulary->vid,
'type' => $type,
));
}
$query->execute();
2005-01-19 16:22:52 +00:00
}
2008-11-05 12:47:23 +00:00
module_invoke_all('taxonomy_vocabulary_update', $vocabulary);
2002-05-02 21:41:29 +00:00
}
2008-11-05 12:47:23 +00:00
elseif (empty($vocabulary->vid)) {
2009-01-14 21:16:21 +00:00
$status = drupal_write_record('taxonomy_vocabulary', $vocabulary);
2009-04-15 14:12:55 +00:00
if (!empty($vocabulary->nodes)) {
$query = db_insert('taxonomy_vocabulary_node_type')
->fields(array('vid', 'type'));
foreach ($vocabulary->nodes as $type => $selected) {
$query->values(array(
'vid' => $vocabulary->vid,
'type' => $type,
));
}
$query->execute();
2005-01-19 16:22:52 +00:00
}
2008-11-05 12:47:23 +00:00
module_invoke_all('taxonomy_vocabulary_insert', $vocabulary);
2002-05-02 21:41:29 +00:00
}
2002-12-12 18:54:17 +00:00
cache_clear_all();
2009-04-02 20:39:45 +00:00
drupal_static_reset('taxonomy_vocabulary_load_multiple');
2002-12-30 12:03:53 +00:00
2005-08-28 15:58:52 +00:00
return $status;
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2006-12-20 10:32:16 +00:00
/**
* Delete a vocabulary.
*
* @param $vid
* A vocabulary ID.
* @return
* Constant indicating items were deleted.
*/
2008-11-05 12:47:23 +00:00
function taxonomy_vocabulary_delete($vid) {
2007-02-11 09:30:51 +00:00
$vocabulary = (array) taxonomy_vocabulary_load($vid);
2002-12-02 19:14:41 +00:00
2009-04-15 14:12:55 +00:00
db_delete('taxonomy_vocabulary')
->condition('vid', $vid)
->execute();
db_delete('taxonomy_vocabulary_node_type')
->condition('vid', $vid)
->execute();
$result = db_query('SELECT tid FROM {taxonomy_term_data} WHERE vid = :vid', array(':vid' => $vid))->fetchCol();
foreach ($result as $tid) {
taxonomy_term_delete($tid);
2002-04-14 20:46:41 +00:00
}
2002-12-02 19:14:41 +00:00
2004-06-18 15:04:37 +00:00
module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
2002-12-30 12:03:53 +00:00
2002-12-12 18:54:17 +00:00
cache_clear_all();
2009-04-02 20:39:45 +00:00
drupal_static_reset('taxonomy_vocabulary_load_multiple');
2002-12-12 18:54:17 +00:00
2005-05-07 01:48:06 +00:00
return SAVED_DELETED;
2002-12-02 19:14:41 +00:00
}
2007-11-26 19:46:52 +00:00
/**
2008-12-30 16:43:20 +00:00
* Dynamically check and update the hierarchy flag of a vocabulary.
2007-11-28 10:29:21 +00:00
*
2007-11-26 19:46:52 +00:00
* Checks the current parents of all terms in a vocabulary and updates the
* vocabularies hierarchy setting to the lowest possible level. A hierarchy with
* no parents in any of its terms will be given a hierarchy of 0. If terms
* contain at most a single parent, the vocabulary will be given a hierarchy of
* 1. If any term contain multiple parents, the vocabulary will be given a
2008-12-30 16:43:20 +00:00
* hierarchy of 2.
2007-11-28 10:29:21 +00:00
*
2007-11-26 19:46:52 +00:00
* @param $vocabulary
2008-11-05 12:47:23 +00:00
* A vocabulary object.
2007-11-26 19:46:52 +00:00
* @param $changed_term
* An array of the term structure that was updated.
*/
function taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term) {
2008-11-05 12:47:23 +00:00
$tree = taxonomy_get_tree($vocabulary->vid);
2007-11-26 19:46:52 +00:00
$hierarchy = 0;
foreach ($tree as $term) {
2008-12-30 16:43:20 +00:00
// Update the changed term with the new parent value before comparison.
2007-11-26 19:46:52 +00:00
if ($term->tid == $changed_term['tid']) {
$term = (object)$changed_term;
$term->parents = $term->parent;
}
// Check this term's parent count.
if (count($term->parents) > 1) {
$hierarchy = 2;
break;
}
elseif (count($term->parents) == 1 && 0 !== array_shift($term->parents)) {
$hierarchy = 1;
}
}
2008-11-05 12:47:23 +00:00
if ($hierarchy != $vocabulary->hierarchy) {
$vocabulary->hierarchy = $hierarchy;
taxonomy_vocabulary_save($vocabulary);
2007-11-26 19:46:52 +00:00
}
return $hierarchy;
}
2006-12-20 10:32:16 +00:00
/**
2008-11-05 14:08:11 +00:00
* Save a term object to the database.
2006-12-20 10:32:16 +00:00
*
2008-11-05 14:08:11 +00:00
* @param $term
* A term object.
2006-12-20 10:32:16 +00:00
* @return
* Status constant indicating if term was inserted or updated.
*/
2008-11-05 14:08:11 +00:00
function taxonomy_term_save($term) {
2008-11-07 18:52:18 +00:00
if ($term->name) {
// Prevent leading and trailing spaces in term names.
$term->name = trim($term->name);
}
2007-04-05 03:08:48 +00:00
2008-11-05 14:08:11 +00:00
if (!empty($term->tid) && $term->name) {
2009-01-14 21:16:21 +00:00
$status = drupal_write_record('taxonomy_term_data', $term, 'tid');
2008-11-02 17:46:47 +00:00
module_invoke_all('taxonomy_term_insert', $term);
2002-05-02 21:41:29 +00:00
}
else {
2009-01-14 21:16:21 +00:00
$status = drupal_write_record('taxonomy_term_data', $term);
2008-11-02 17:46:47 +00:00
module_invoke_all('taxonomy_term_update', $term);
2002-05-02 21:41:29 +00:00
}
2009-05-24 17:39:35 +00:00
2009-04-15 14:12:55 +00:00
db_delete('taxonomy_term_relation')
->condition(db_or()
->condition('tid1', $term->tid)
->condition('tid2', $term->tid)
)
->execute();
2002-04-14 20:46:41 +00:00
2008-11-05 14:08:11 +00:00
if (!empty($term->relations)) {
foreach ($term->relations as $related_id) {
2002-05-02 21:41:29 +00:00
if ($related_id != 0) {
2009-04-15 14:12:55 +00:00
db_insert('taxonomy_term_relation')
->fields(array(
'tid1' => $term->tid,
'tid2' => $related_id
))
->execute();
2002-04-14 20:46:41 +00:00
}
2002-04-22 09:05:36 +00:00
}
2002-05-02 21:41:29 +00:00
}
2009-04-15 14:12:55 +00:00
db_delete('taxonomy_term_hierarchy')
->condition('tid', $term->tid)
->execute();
2002-04-14 20:46:41 +00:00
2008-11-05 14:08:11 +00:00
if (!isset($term->parent) || empty($term->parent)) {
$term->parent = array(0);
2002-05-02 21:41:29 +00:00
}
2009-04-15 14:12:55 +00:00
$query = db_insert('taxonomy_term_hierarchy')
->fields(array('tid', 'parent'));
2008-11-05 14:08:11 +00:00
if (is_array($term->parent)) {
foreach ($term->parent as $parent) {
2005-03-03 20:38:18 +00:00
if (is_array($parent)) {
foreach ($parent as $tid) {
2009-04-15 14:12:55 +00:00
$query->values(array(
'tid' => $term->tid,
'parent' => $tid
));
2005-03-03 20:38:18 +00:00
}
}
else {
2009-04-15 14:12:55 +00:00
$query->values(array(
'tid' => $term->tid,
'parent' => $parent
));
2005-03-03 20:38:18 +00:00
}
2002-04-14 20:46:41 +00:00
}
2002-05-02 21:41:29 +00:00
}
2005-11-01 10:17:34 +00:00
else {
2009-05-24 17:39:35 +00:00
$query->values(array(
'tid' => $term->tid,
'parent' => $parent
));
2005-11-01 10:17:34 +00:00
}
2009-04-15 14:12:55 +00:00
$query->execute();
2002-04-14 20:46:41 +00:00
2009-04-15 14:12:55 +00:00
db_delete('taxonomy_term_synonym')
->condition('tid', $term->tid)
->execute();
2008-11-05 14:08:11 +00:00
if (!empty($term->synonyms)) {
2009-04-15 14:12:55 +00:00
$query = db_insert('taxonomy_term_synonym')
->fields(array('tid', 'name'));
2008-11-05 14:08:11 +00:00
foreach (explode ("\n", str_replace("\r", '', $term->synonyms)) as $synonym) {
2002-12-11 20:22:59 +00:00
if ($synonym) {
2009-04-15 14:12:55 +00:00
$query->values(array(
'tid' => $term->tid,
'name' => rtrim($synonym)
));
2002-12-11 20:22:59 +00:00
}
2002-05-02 21:41:29 +00:00
}
2009-04-15 14:12:55 +00:00
$query->execute();
2002-04-14 20:46:41 +00:00
}
2002-12-02 19:14:41 +00:00
2002-12-12 18:54:17 +00:00
cache_clear_all();
2009-04-02 20:39:45 +00:00
taxonomy_terms_static_reset();
2002-12-12 18:54:17 +00:00
2005-08-28 15:58:52 +00:00
return $status;
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2006-12-20 10:32:16 +00:00
/**
* Delete a term.
*
* @param $tid
* The term ID.
* @return
* Status constant indicating deletion.
*/
2008-11-05 14:08:11 +00:00
function taxonomy_term_delete($tid) {
2004-10-13 18:38:33 +00:00
$tids = array($tid);
while ($tids) {
$children_tids = $orphans = array();
foreach ($tids as $tid) {
// See if any of the term's children are about to be become orphans:
if ($children = taxonomy_get_children($tid)) {
foreach ($children as $child) {
// If the term has multiple parents, we don't delete it.
$parents = taxonomy_get_parents($child->tid);
if (count($parents) == 1) {
$orphans[] = $child->tid;
}
}
}
2002-12-02 19:14:41 +00:00
2008-11-02 14:42:45 +00:00
$term = taxonomy_term_load($tid);
2004-10-15 05:10:35 +00:00
2009-04-15 14:12:55 +00:00
db_delete('taxonomy_term_data')
->condition('tid', $tid)
->execute();
db_delete('taxonomy_term_hierarchy')
->condition('tid', $tid)
->execute();
db_delete('taxonomy_term_relation')
->condition(db_or()
->condition('tid1', $tid)
->condition('tid2', $tid)
)
->execute();
db_delete('taxonomy_term_synonym')
->condition('tid', $tid)
->execute();
db_delete('taxonomy_term_node')
->condition('tid', $tid)
->execute();
2004-10-15 05:10:35 +00:00
2008-11-02 14:42:45 +00:00
module_invoke_all('taxonomy_term_delete', $term);
2004-10-13 18:38:33 +00:00
}
2004-10-15 05:10:35 +00:00
2004-10-13 18:38:33 +00:00
$tids = $orphans;
}
2003-06-20 17:43:03 +00:00
2002-12-12 18:54:17 +00:00
cache_clear_all();
2009-04-02 20:39:45 +00:00
taxonomy_terms_static_reset();
2009-05-24 17:39:35 +00:00
2006-03-23 21:46:35 +00:00
return SAVED_DELETED;
2002-12-02 19:14:41 +00:00
}
2009-04-02 20:39:45 +00:00
/**
* Clear all static cache variables for terms..
*/
function taxonomy_terms_static_reset() {
drupal_static_reset('taxonomy_term_count_nodes');
drupal_static_reset('taxonomy_get_tree');
drupal_static_reset('taxonomy_get_synonym_root');
drupal_static_reset('taxonomy_term_load_multiple');
drupal_static_reset('taxonomy_get_term_data');
}
2004-06-18 15:04:37 +00:00
/**
* Generate a form element for selecting terms from a vocabulary.
2009-05-19 19:01:51 +00:00
*
* @param $vid
* The vocabulary ID to generate a form element for
* @param $value
* The existing value of the term(s) in this vocabulary to use by default.
* @param $help
* Optional help text to use for the form element. If specified, this value
* MUST be properly sanitized and filtered (e.g. with filter_xss_admin() or
* check_plain() if it is user-supplied) to prevent XSS vulnerabilities. If
* omitted, the help text stored with the vocaulary (if any) will be used.
* @return
* An array describing a form element to select terms for a vocabulary.
*
* @see _taxonomy_term_select()
* @see filter_xss_admin()
2004-06-18 15:04:37 +00:00
*/
2009-04-15 13:48:03 +00:00
function taxonomy_form($vid, $value = 0, $help = NULL) {
2007-02-11 09:30:51 +00:00
$vocabulary = taxonomy_vocabulary_load($vid);
2009-05-19 19:01:51 +00:00
$help = ($help) ? $help : filter_xss_admin($vocabulary->help);
2007-11-11 06:56:44 +00:00
2007-11-06 10:01:52 +00:00
if (!$vocabulary->multiple) {
$blank = ($vocabulary->required) ? t('- Please choose -') : t('- None selected -');
2002-05-02 21:41:29 +00:00
}
2007-12-10 21:54:10 +00:00
else {
$blank = ($vocabulary->required) ? 0 : t('- None -');
}
2002-05-31 20:29:30 +00:00
2009-04-15 13:48:03 +00:00
return _taxonomy_term_select(check_plain($vocabulary->name), $value, $vid, $help, intval($vocabulary->multiple), $blank);
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2006-05-16 06:50:21 +00:00
/**
2006-12-20 10:32:16 +00:00
* Generate a set of options for selecting a term from all vocabularies.
2006-05-16 06:50:21 +00:00
*/
function taxonomy_form_all($free_tags = 0) {
$vocabularies = taxonomy_get_vocabularies();
$options = array();
foreach ($vocabularies as $vid => $vocabulary) {
2008-03-02 05:58:40 +00:00
if ($vocabulary->tags && !$free_tags) {
continue;
}
2006-05-16 06:50:21 +00:00
$tree = taxonomy_get_tree($vid);
2007-07-14 15:30:40 +00:00
if ($tree && (count($tree) > 0)) {
2006-09-06 07:22:41 +00:00
$options[$vocabulary->name] = array();
2006-05-16 06:50:21 +00:00
foreach ($tree as $term) {
2006-08-24 06:29:50 +00:00
$options[$vocabulary->name][$term->tid] = str_repeat('-', $term->depth) . $term->name;
2006-05-16 06:50:21 +00:00
}
}
}
return $options;
}
2004-06-18 15:04:37 +00:00
/**
* Return an array of all vocabulary objects.
*
* @param $type
* If set, return only those vocabularies associated with this node type.
*/
2009-04-02 20:39:45 +00:00
function taxonomy_get_vocabularies($type = NULL) {
2009-03-30 05:18:49 +00:00
$conditions = !empty($type) ? array('type' => $type) : NULL;
2009-04-02 20:39:45 +00:00
return taxonomy_vocabulary_load_multiple(array(), $conditions);
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2004-06-18 15:04:37 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_form_alter().
2004-06-18 15:04:37 +00:00
* Generate a form for selecting terms to associate with a node.
2007-11-23 13:34:55 +00:00
* We check for taxonomy_override_selector before loading the full
2007-11-23 12:32:21 +00:00
* vocabulary, so contrib modules can intercept before hook_form_alter
* and provide scalable alternatives.
2004-06-18 15:04:37 +00:00
*/
2007-05-28 06:08:47 +00:00
function taxonomy_form_alter(&$form, $form_state, $form_id) {
2008-09-27 20:37:01 +00:00
if (!variable_get('taxonomy_override_selector', FALSE) && !empty($form['#node_edit_form'])) {
2005-12-05 09:11:33 +00:00
$node = $form['#node'];
2005-12-06 12:20:24 +00:00
if (!isset($node->taxonomy)) {
2007-02-12 17:47:08 +00:00
$terms = empty($node->nid) ? array() : taxonomy_node_get_terms($node);
2002-04-22 09:05:36 +00:00
}
else {
2007-11-14 13:32:58 +00:00
// After preview the terms must be converted to objects.
if (isset($form_state['node_preview'])) {
$node->taxonomy = taxonomy_preview_terms($node);
}
2005-12-05 09:11:33 +00:00
$terms = $node->taxonomy;
2002-04-14 20:46:41 +00:00
}
2009-04-15 14:12:55 +00:00
$query = db_select('taxonomy_vocabulary', 'v');
$query->join('taxonomy_vocabulary_node_type', 'n', 'v.vid = n.vid');
$query->addTag('term_access');
2002-04-14 20:46:41 +00:00
2009-04-15 14:12:55 +00:00
$result = $query
->fields('v')
->condition('n.type', $node->type)
->orderBy('v.weight')
->orderBy('v.name')
->execute();
2005-04-08 14:59:14 +00:00
2009-04-15 14:12:55 +00:00
foreach ($result as $vocabulary) {
2005-12-05 09:11:33 +00:00
if ($vocabulary->tags) {
2007-11-14 13:32:58 +00:00
if (isset($form_state['node_preview'])) {
2007-11-17 14:25:23 +00:00
// Typed string can be changed by the user before preview,
2007-11-14 13:39:58 +00:00
// so we just insert the tags directly as provided in the form.
2007-11-14 13:32:58 +00:00
$typed_string = $node->taxonomy['tags'][$vocabulary->vid];
}
else {
$typed_string = taxonomy_implode_tags($terms, $vocabulary->vid) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);
}
2006-01-10 19:56:19 +00:00
if ($vocabulary->help) {
2009-05-19 19:01:51 +00:00
$help = filter_xss_admin($vocabulary->help);
2006-01-10 19:56:19 +00:00
}
else {
2009-02-06 16:25:09 +00:00
$help = t('A comma-separated list of terms describing this content. Example: funny, bungee jumping, "Company, Inc."');
2006-01-10 19:56:19 +00:00
}
2006-02-09 08:28:28 +00:00
$form['taxonomy']['tags'][$vocabulary->vid] = array('#type' => 'textfield',
'#title' => $vocabulary->name,
'#description' => $help,
'#required' => $vocabulary->required,
'#default_value' => $typed_string,
2008-04-14 17:48:46 +00:00
'#autocomplete_path' => 'taxonomy/autocomplete/' . $vocabulary->vid,
2006-02-09 08:28:28 +00:00
'#weight' => $vocabulary->weight,
2009-02-11 03:53:36 +00:00
'#maxlength' => 1024,
2006-02-09 08:28:28 +00:00
);
2005-12-05 09:11:33 +00:00
}
else {
2006-03-11 13:47:51 +00:00
// Extract terms belonging to the vocabulary in question.
$default_terms = array();
2007-11-17 14:25:23 +00:00
foreach ($terms as $term) {
2007-11-14 13:32:58 +00:00
// Free tagging has no default terms and also no vid after preview.
if (isset($term->vid) && $term->vid == $vocabulary->vid) {
2006-03-11 13:47:51 +00:00
$default_terms[$term->tid] = $term;
}
}
2009-05-19 19:01:51 +00:00
$form['taxonomy'][$vocabulary->vid] = taxonomy_form($vocabulary->vid, array_keys($default_terms), filter_xss_admin($vocabulary->help));
2005-12-16 06:23:46 +00:00
$form['taxonomy'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
2006-01-26 08:33:20 +00:00
$form['taxonomy'][$vocabulary->vid]['#required'] = $vocabulary->required;
2005-12-05 09:11:33 +00:00
}
2005-04-08 14:59:14 +00:00
}
2007-01-31 15:49:26 +00:00
if (!empty($form['taxonomy']) && is_array($form['taxonomy'])) {
2007-11-17 14:25:23 +00:00
if (count($form['taxonomy']) > 1) {
2007-11-14 13:39:58 +00:00
// Add fieldset only if form has more than 1 element.
2006-09-11 08:18:24 +00:00
$form['taxonomy'] += array(
'#type' => 'fieldset',
2007-11-19 18:26:11 +00:00
'#title' => t('Vocabularies'),
2006-09-11 08:18:24 +00:00
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
}
2006-09-21 15:31:24 +00:00
$form['taxonomy']['#weight'] = -3;
$form['taxonomy']['#tree'] = TRUE;
2007-11-17 14:25:23 +00:00
}
2007-11-14 13:32:58 +00:00
}
}
/**
2007-11-20 15:31:34 +00:00
* Helper function to convert terms after a preview.
2007-11-14 13:32:58 +00:00
*
* After preview the tags are an array instead of proper objects. This function
* converts them back to objects with the exception of 'free tagging' terms,
2007-11-17 14:25:23 +00:00
* because new tags can be added by the user before preview and those do not
* yet exist in the database. We therefore save those tags as a string so
2007-11-14 13:32:58 +00:00
* we can fill the form again after the preview.
*/
function taxonomy_preview_terms($node) {
$taxonomy = array();
2007-11-22 13:18:01 +00:00
if (isset($node->taxonomy)) {
foreach ($node->taxonomy as $key => $term) {
unset($node->taxonomy[$key]);
// A 'Multiple select' and a 'Free tagging' field returns an array.
if (is_array($term)) {
foreach ($term as $tid) {
if ($key == 'tags') {
// Free tagging; the values will be saved for later as strings
// instead of objects to fill the form again.
$taxonomy['tags'] = $term;
}
else {
2008-09-19 20:25:03 +00:00
$taxonomy[$tid] = taxonomy_term_load($tid);
2007-11-22 13:18:01 +00:00
}
2007-11-14 13:32:58 +00:00
}
}
2007-11-22 13:18:01 +00:00
// A 'Single select' field returns the term id.
elseif ($term) {
2008-09-19 20:25:03 +00:00
$taxonomy[$term] = taxonomy_term_load($term);
2007-11-22 13:18:01 +00:00
}
2005-04-08 14:59:14 +00:00
}
2002-04-14 20:46:41 +00:00
}
2007-11-14 13:32:58 +00:00
return $taxonomy;
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2004-06-18 15:04:37 +00:00
/**
2006-12-20 10:32:16 +00:00
* Find all terms associated with the given node, within one vocabulary.
2004-06-18 15:04:37 +00:00
*/
2007-02-12 17:47:08 +00:00
function taxonomy_node_get_terms_by_vocabulary($node, $vid, $key = 'tid') {
2009-04-15 14:12:55 +00:00
$query = db_select('taxonomy_term_data', 't');
$query->join('taxonomy_term_node', 'r', 'r.tid = t.tid');
$query->addTag('term_access');
$result = $query
->fields('t')
->condition('t.vid', $vid)
->condition('r.vid', $node->vid)
->orderBy('weight')
->execute();
2002-05-02 21:41:29 +00:00
$terms = array();
2009-04-15 14:12:55 +00:00
foreach ($result as $term) {
2002-05-02 21:41:29 +00:00
$terms[$term->$key] = $term;
2002-04-14 20:46:41 +00:00
}
2002-05-02 21:41:29 +00:00
return $terms;
}
2008-12-05 22:18:46 +00:00
/**
* Find all term IDs associated with a set of nodes.
*
* @param $nodes
* An array of node objects.
*
* @return
* An array of term and node IDs ordered by vocabulary and term weight.
*/
function taxonomy_get_tids_from_nodes($nodes) {
$node_vids = array();
foreach ($nodes as $node) {
$node_vids[] = $node->vid;
}
2009-01-14 21:16:21 +00:00
$query = db_select('taxonomy_term_node', 'r');
$query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
$query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
2008-12-05 22:18:46 +00:00
$query->addTag('term_access');
2009-04-15 14:12:55 +00:00
return $query
->fields('r', array('tid', 'nid', 'vid'))
->condition('r.vid', $node_vids, 'IN')
->orderBy('v.weight')
->orderBy('t.weight')
->orderBy('t.name')
->execute()
->fetchAll();
2008-12-05 22:18:46 +00:00
}
2004-06-18 15:04:37 +00:00
/**
2006-12-20 10:32:16 +00:00
* Find all terms associated with the given node, ordered by vocabulary and term weight.
2004-06-18 15:04:37 +00:00
*/
2007-02-12 17:47:08 +00:00
function taxonomy_node_get_terms($node, $key = 'tid') {
2009-04-02 20:39:45 +00:00
$terms = &drupal_static(__FUNCTION__);
2002-04-14 20:46:41 +00:00
2007-05-04 08:38:34 +00:00
if (!isset($terms[$node->vid][$key])) {
2009-04-15 14:12:55 +00:00
$query = db_select('taxonomy_term_node', 'r');
$query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
$query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
$query->addTag('term_access');
$result = $query
->fields('r', array('tid', 'nid', 'vid'))
->condition('r.vid', $node->vid)
->orderBy('v.weight')
->orderBy('t.weight')
->orderBy('t.name')
->execute();
2007-05-04 08:38:34 +00:00
$terms[$node->vid][$key] = array();
2009-04-15 14:12:55 +00:00
foreach ($result as $term) {
2007-05-04 08:38:34 +00:00
$terms[$node->vid][$key][$term->$key] = $term;
2002-04-14 20:46:41 +00:00
}
}
2007-05-04 08:38:34 +00:00
return $terms[$node->vid][$key];
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2004-06-18 15:04:37 +00:00
/**
* Save term associations for a given node.
*/
2007-02-12 17:47:08 +00:00
function taxonomy_node_save($node, $terms) {
2009-03-08 04:25:07 +00:00
taxonomy_node_delete_revision($node);
2005-04-08 14:59:14 +00:00
// Free tagging vocabularies do not send their tids in the form,
// so we'll detect them here and process them independently.
2006-08-31 21:58:36 +00:00
if (isset($terms['tags'])) {
$typed_input = $terms['tags'];
unset($terms['tags']);
2005-04-08 14:59:14 +00:00
foreach ($typed_input as $vid => $vid_value) {
2007-06-29 18:06:51 +00:00
$typed_terms = drupal_explode_tags($vid_value);
2005-04-08 14:59:14 +00:00
2006-05-23 02:21:26 +00:00
$inserted = array();
2005-04-08 14:59:14 +00:00
foreach ($typed_terms as $typed_term) {
// See if the term exists in the chosen vocabulary
2006-12-20 10:32:16 +00:00
// and return the tid; otherwise, add a new record.
2005-04-08 14:59:14 +00:00
$possibilities = taxonomy_get_term_by_name($typed_term);
2006-12-20 10:32:16 +00:00
$typed_term_tid = NULL; // tid match, if any.
2005-04-08 14:59:14 +00:00
foreach ($possibilities as $possibility) {
if ($possibility->vid == $vid) {
$typed_term_tid = $possibility->tid;
}
}
if (!$typed_term_tid) {
2005-08-28 15:58:52 +00:00
$edit = array('vid' => $vid, 'name' => $typed_term);
2008-11-09 00:58:03 +00:00
$term = (object)$edit;
$status = taxonomy_term_save($term);
$typed_term_tid = $term->tid;
2005-04-08 14:59:14 +00:00
}
2006-12-20 10:32:16 +00:00
// Defend against duplicate, differently cased tags
2006-05-23 02:21:26 +00:00
if (!isset($inserted[$typed_term_tid])) {
2009-04-15 14:12:55 +00:00
db_insert('taxonomy_term_node')
->fields(array(
'nid' => $node->nid,
'vid' => $node->vid,
'tid' => $typed_term_tid
))
->execute();
2006-05-23 02:21:26 +00:00
$inserted[$typed_term_tid] = TRUE;
}
2005-04-08 14:59:14 +00:00
}
}
}
2002-04-14 20:46:41 +00:00
2009-04-15 14:12:55 +00:00
if (is_array($terms) && !empty($terms)) {
$query = db_insert('taxonomy_term_node')
->fields(array('nid', 'vid', 'tid'));
2009-05-24 17:39:35 +00:00
2006-08-31 21:58:36 +00:00
foreach ($terms as $term) {
2005-03-03 20:38:18 +00:00
if (is_array($term)) {
foreach ($term as $tid) {
if ($tid) {
2009-04-15 14:12:55 +00:00
$query->values(array(
'nid' => $node->nid,
'vid' => $node->vid,
'tid' => $tid,
));
2005-03-03 20:38:18 +00:00
}
}
}
2008-10-12 04:30:09 +00:00
elseif (is_object($term)) {
2009-04-15 14:12:55 +00:00
$query->values(array(
'nid' => $node->nid,
'vid' => $node->vid,
'tid' => $term->tid,
));
2006-04-11 17:57:37 +00:00
}
2008-10-12 04:30:09 +00:00
elseif ($term) {
2009-04-15 14:12:55 +00:00
$query->values(array(
'nid' => $node->nid,
'vid' => $node->vid,
'tid' => $term,
));
2004-06-15 18:41:25 +00:00
}
2002-04-14 20:46:41 +00:00
}
2009-04-15 14:12:55 +00:00
$query->execute();
2002-04-14 20:46:41 +00:00
}
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2006-10-31 17:01:04 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_type().
2006-10-31 17:01:04 +00:00
*/
function taxonomy_node_type($op, $info) {
if ($op == 'update' && !empty($info->old_type) && $info->type != $info->old_type) {
2009-04-15 14:12:55 +00:00
db_update('taxonomy_vocabulary_node_type')
->fields(array(
'type' => $info->type,
))
->condition('type', $info->old_type)
->execute();
2006-11-10 19:40:23 +00:00
}
elseif ($op == 'delete') {
2009-04-15 14:12:55 +00:00
db_delete('taxonomy_vocabulary_node_type')
->condition('type', $info->type)
->execute();
2006-10-31 17:01:04 +00:00
}
2009-04-02 20:39:45 +00:00
drupal_static_reset('taxonomy_term_count_nodes');
2006-10-31 17:01:04 +00:00
}
2004-06-18 15:04:37 +00:00
/**
* Find all term objects related to a given term ID.
*/
function taxonomy_get_related($tid, $key = 'tid') {
2002-05-02 21:41:29 +00:00
if ($tid) {
2009-05-03 10:44:04 +00:00
$result = db_query('SELECT t.*, tid1, tid2 FROM {taxonomy_term_relation}, {taxonomy_term_data} t WHERE (t.tid = tid1 OR t.tid = tid2) AND (tid1 = :tid1 OR tid2 = :tid2) AND t.tid != :tid ORDER BY weight, name', array(':tid' => $tid, ':tid1' => $tid, ':tid2' => $tid));
2002-05-02 21:41:29 +00:00
$related = array();
2009-04-15 14:12:55 +00:00
foreach ($result as $term) {
2002-05-02 21:41:29 +00:00
$related[$term->$key] = $term;
2002-04-14 20:46:41 +00:00
}
2002-05-02 21:41:29 +00:00
return $related;
2002-04-14 20:46:41 +00:00
}
2002-05-02 21:41:29 +00:00
else {
return array();
2002-04-14 20:46:41 +00:00
}
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2004-06-18 15:04:37 +00:00
/**
* Find all parents of a given term ID.
*/
function taxonomy_get_parents($tid, $key = 'tid') {
2002-05-02 21:41:29 +00:00
if ($tid) {
2009-04-15 14:12:55 +00:00
$query = db_select('taxonomy_term_data', 't');
$query->join('taxonomy_term_hierarchy', 'h', 'h.parent = t.tid');
$query->addTag('term_access');
$result = $query
->fields('t')
->condition('h.tid', $tid)
->orderBy('weight')
->orderBy('name')
->execute();
2002-05-02 21:41:29 +00:00
$parents = array();
2009-04-15 14:12:55 +00:00
foreach ($result as $parent) {
2002-05-02 21:41:29 +00:00
$parents[$parent->$key] = $parent;
2002-04-28 09:08:02 +00:00
}
2002-05-02 21:41:29 +00:00
return $parents;
2002-04-14 20:46:41 +00:00
}
2002-05-02 21:41:29 +00:00
else {
return array();
}
}
2002-04-14 20:46:41 +00:00
2004-06-18 15:04:37 +00:00
/**
* Find all ancestors of a given term ID.
*/
function taxonomy_get_parents_all($tid) {
2003-12-24 15:09:43 +00:00
$parents = array();
2009-01-28 01:09:58 +00:00
if ($term = taxonomy_term_load($tid)) {
$parents[] = $term;
2003-12-24 15:09:43 +00:00
$n = 0;
while ($parent = taxonomy_get_parents($parents[$n]->tid)) {
$parents = array_merge($parents, $parent);
$n++;
}
}
return $parents;
}
2004-06-18 15:04:37 +00:00
/**
* Find all children of a term ID.
*/
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
2009-04-15 14:12:55 +00:00
$query = db_select('taxonomy_term_data', 't');
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
$query->addTag('term_access');
$query
->fields('t')
->condition('parent', $tid)
->orderBy('weight')
->orderBy('name');
2002-05-02 21:41:29 +00:00
if ($vid) {
2009-04-15 14:12:55 +00:00
$query->condition('t.vid', $vid);
2002-05-02 21:41:29 +00:00
}
2009-04-15 14:12:55 +00:00
$result = $query->execute();
2002-05-02 21:41:29 +00:00
$children = array();
2009-04-15 14:12:55 +00:00
foreach ($result as $term) {
2002-05-02 21:41:29 +00:00
$children[$term->$key] = $term;
}
return $children;
}
2002-04-14 20:46:41 +00:00
2004-06-18 15:04:37 +00:00
/**
* Create a hierarchical representation of a vocabulary.
*
* @param $vid
* Which vocabulary to generate the tree for.
* @param $parent
* The term ID under which to generate the tree. If 0, generate the tree
* for the entire vocabulary.
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
* @param $max_depth
* The number of levels of the tree to return. Leave NULL to return all levels.
2008-12-08 21:54:33 +00:00
* @param $depth
* Internal use only.
*
2004-06-18 15:04:37 +00:00
* @return
* An array of all term objects in the tree. Each term object is extended
* to have "depth" and "parents" attributes in addition to its normal ones.
2006-10-18 11:15:51 +00:00
* Results are statically cached.
2004-06-18 15:04:37 +00:00
*/
2009-04-02 20:39:45 +00:00
function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $depth = -1) {
$children = &drupal_static(__FUNCTION__, array());
$parents = &drupal_static(__FUNCTION__ . 'parents', array());
$terms = &drupal_static(__FUNCTION__ . 'terms', array());
2009-01-28 01:14:39 +00:00
2002-05-02 21:41:29 +00:00
$depth++;
2002-12-02 19:14:41 +00:00
2004-06-18 15:04:37 +00:00
// We cache trees, so it's not CPU-intensive to call get_tree() on a term
// and its children, too.
if (!isset($children[$vid])) {
$children[$vid] = array();
2009-04-02 20:39:45 +00:00
$parents[$vid] = array();
$terms[$vid] = array();
2002-12-30 12:03:53 +00:00
2009-04-15 14:12:55 +00:00
$query = db_select('taxonomy_term_data', 't');
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
$query->addTag('term_access');
$result = $query
->fields('t')
->fields('h', array('parent'))
->condition('t.vid', $vid)
->orderBy('weight')
->orderBy('name')
->execute();
foreach ($result as $term) {
2004-06-18 15:04:37 +00:00
$children[$vid][$term->parent][] = $term->tid;
$parents[$vid][$term->tid][] = $term->parent;
$terms[$vid][$term->tid] = $term;
2002-04-14 20:46:41 +00:00
}
}
2002-12-02 19:14:41 +00:00
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
$max_depth = (is_null($max_depth)) ? count($children[$vid]) : $max_depth;
2007-01-31 15:49:26 +00:00
$tree = array();
2009-04-17 19:59:51 +00:00
if ($max_depth > $depth && !empty($children[$vid][$parent])) {
2005-04-07 20:09:36 +00:00
foreach ($children[$vid][$parent] as $child) {
2009-04-17 19:59:51 +00:00
$term = clone $terms[$vid][$child];
$term->depth = $depth;
// The "parent" attribute is not useful, as it would show one parent only.
unset($term->parent);
$term->parents = $parents[$vid][$child];
$tree[] = $term;
if (!empty($children[$vid][$child])) {
$tree = array_merge($tree, taxonomy_get_tree($vid, $child, $max_depth, $depth));
2004-05-31 19:14:43 +00:00
}
2002-12-02 19:14:41 +00:00
}
2002-05-02 21:41:29 +00:00
}
2002-12-02 19:14:41 +00:00
2007-01-31 15:49:26 +00:00
return $tree;
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2004-06-18 15:04:37 +00:00
/**
* Return an array of synonyms of the given term ID.
*/
2002-05-02 21:41:29 +00:00
function taxonomy_get_synonyms($tid) {
if ($tid) {
2007-07-11 15:27:57 +00:00
$synonyms = array();
2009-04-15 14:12:55 +00:00
return db_query('SELECT name FROM {taxonomy_term_synonym} WHERE tid = :tid', array(':tid' => $tid))->fetchCol();
2002-04-14 20:46:41 +00:00
}
2002-05-02 21:41:29 +00:00
else {
return array();
2002-04-20 11:52:50 +00:00
}
2002-05-02 21:41:29 +00:00
}
2002-04-20 11:52:50 +00:00
2004-06-18 15:04:37 +00:00
/**
* Return the term object that has the given string as a synonym.
2008-10-11 16:04:29 +00:00
*
* @param $synonym
* The string to compare against.
* @return
* A term object, or FALSE if no matching term is found.
2004-06-18 15:04:37 +00:00
*/
2009-04-02 20:39:45 +00:00
function taxonomy_get_synonym_root($synonym) {
$synonyms = &drupal_static(__FUNCTION__, array());
2008-10-11 16:04:29 +00:00
if (!isset($synonyms[$synonym])) {
2009-01-14 21:16:21 +00:00
$synonyms[$synonym] = db_query("SELECT * FROM {taxonomy_term_synonym} s, {taxonomy_term_data} t WHERE t.tid = s.tid AND s.name = :name", array(':name' => $synonym))->fetch();
2008-10-11 16:04:29 +00:00
}
return $synonyms[$synonym];
2002-05-02 21:41:29 +00:00
}
2002-04-20 11:52:50 +00:00
2004-06-18 15:04:37 +00:00
/**
2006-10-18 11:15:51 +00:00
* Count the number of published nodes classified by a term.
*
* @param $tid
2009-01-28 01:14:39 +00:00
* The term ID
2006-10-18 11:15:51 +00:00
* @param $type
2009-01-28 01:14:39 +00:00
* (Optional) The $node->type. If given, taxonomy_term_count_nodes only counts
2006-10-18 11:15:51 +00:00
* nodes of $type that are classified with the term $tid.
*
2009-01-28 01:14:39 +00:00
* @return
2006-10-18 11:15:51 +00:00
* An integer representing a number of nodes.
* Results are statically cached.
2004-06-18 15:04:37 +00:00
*/
2009-04-02 20:39:45 +00:00
function taxonomy_term_count_nodes($tid, $type = NULL) {
$count = &drupal_static(__FUNCTION__, array());
// Reset the taxonomy tree when first called (or if reset).
if (empty($count)) {
drupal_static_reset('taxonomy_get_tree');
2002-05-02 21:41:29 +00:00
}
2009-01-28 01:14:39 +00:00
// If $type is NULL, change it to 0 to allow it to be used as an array key
// for the static cache.
$type = empty($type) ? 0 : $type;
2002-04-20 11:52:50 +00:00
2009-01-28 01:14:39 +00:00
if (!isset($count[$type][$tid])) {
$term = taxonomy_term_load($tid);
2009-04-02 20:39:45 +00:00
$tree = taxonomy_get_tree($term->vid, $tid, NULL);
2009-01-28 01:14:39 +00:00
$tids = array($tid);
foreach ($tree as $descendent) {
$tids[] = $descendent->tid;
}
$query = db_select('taxonomy_term_node', 't');
$query->addExpression('COUNT(DISTINCT(n.nid))', 'nid_count');
$query->join('node', 'n', 't.vid = n.vid');
$query->condition('t.tid', $tids, 'IN');
$query->condition('n.status', 1);
if (!is_numeric($type)) {
$query->condition('n.type', $type);
2002-04-20 11:52:50 +00:00
}
2009-01-28 01:14:39 +00:00
$query->addTag('term_access');
$count[$type][$tid] = $query->execute()->fetchField();
2002-04-14 20:46:41 +00:00
}
2009-01-28 01:14:39 +00:00
return $count[$type][$tid];
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2002-12-12 18:54:17 +00:00
/**
2004-06-18 15:04:37 +00:00
* Try to map a string to an existing term, as for glossary use.
2002-12-12 18:54:17 +00:00
*
2004-06-18 15:04:37 +00:00
* Provides a case-insensitive and trimmed mapping, to maximize the
* likelihood of a successful match.
*
* @param name
* Name of the term to search for.
*
* @return
* An array of matching term objects.
2002-12-12 18:54:17 +00:00
*/
function taxonomy_get_term_by_name($name) {
2009-04-15 14:12:55 +00:00
$query = db_select('taxonomy_term_data', 't');
$query->addTag('term_access');
2002-12-12 18:54:17 +00:00
2009-04-15 14:12:55 +00:00
return $query
->fields('t')
->where("LOWER(t.name) = LOWER(:name)", array(':name' => trim($name)))
->execute()->fetchAll();
2002-12-12 18:54:17 +00:00
}
2004-06-18 15:04:37 +00:00
/**
2009-03-30 05:18:49 +00:00
* Load multiple taxonomy vocabularies based on certain conditions.
2006-10-18 11:15:51 +00:00
*
2009-03-30 05:18:49 +00:00
* This function should be used whenever you need to load more than one
* vocabulary from the database. Terms are loaded into memory and will not
* require database access if loaded again during the same page request.
2008-09-24 18:42:00 +00:00
*
2009-03-30 05:18:49 +00:00
* @param $vids
* An array of taxonomy vocabulary IDs.
* @param $conditions
* An array of conditions to add to the query.
2006-10-18 11:15:51 +00:00
*
2007-12-31 16:58:34 +00:00
* @return
2009-03-30 05:18:49 +00:00
* An array of vocabulary objects, indexed by vid.
2004-06-18 15:04:37 +00:00
*/
2009-04-02 20:39:45 +00:00
function taxonomy_vocabulary_load_multiple($vids = array(), $conditions = array()) {
$vocabulary_cache = &drupal_static(__FUNCTION__, array());
2009-03-30 05:18:49 +00:00
// Node type associations are not stored in the vocabulary table, so remove
// this from conditions into it's own variable.
if (isset($conditions['type'])) {
$type = $conditions['type'];
unset($conditions['type']);
}
2005-10-21 11:12:46 +00:00
2009-03-30 05:18:49 +00:00
$vocabularies = array();
// Create a new variable which is either a prepared version of the $vids
// array for later comparison with the term cache, or FALSE if no $vids were
// passed. The $vids array is reduced as items are loaded from cache, and we
// need to know if it's empty for this reason to avoid querying the database
// when all requested items are loaded from cache.
$passed_vids = !empty($vids) ? array_flip($vids) : FALSE;
// Load any available items from the internal cache.
if ($vocabulary_cache) {
if ($vids) {
$vocabularies += array_intersect_key($vocabulary_cache, $passed_vids);
// If any items were loaded, remove them from the $vids still to load.
$vids = array_keys(array_diff_key($passed_vids, $vocabularies));
}
// If only conditions is passed, load all items from the cache. Items
// which don't match conditions will be removed later.
elseif ($conditions) {
$vocabularies = $vocabulary_cache;
}
}
// Remove any loaded terms from the array if they don't match $conditions.
if ($conditions || isset($type)) {
foreach ($vocabularies as $vocabulary) {
$vocabulary_values = (array) $vocabulary;
if (array_diff_assoc($conditions, $vocabulary_values)) {
unset($vocabularies[$vocabulary->vid]);
}
if (isset($type) && !in_array($type, $vocabulary->nodes)) {
unset($vocabularies[$vocabulary->vid]);
}
}
2008-09-24 18:42:00 +00:00
}
2009-03-30 05:18:49 +00:00
// Load any remaining vocabularies from the database, this is necessary if
// we have $vids still to load, or if no $vids were passed.
if ($vids || !$passed_vids) {
$query = db_select('taxonomy_vocabulary', 'v');
$query->addField('n', 'type');
2009-04-15 14:12:55 +00:00
$query
->fields('v')
->orderBy('v.weight')
->orderBy('v.name');
2009-03-30 05:18:49 +00:00
if (!empty($type)) {
2009-04-13 18:52:38 +00:00
$query->leftJoin('taxonomy_vocabulary_node_type', 'n', 'v.vid = n.vid AND n.type = :type', array(':type' => $type));
2009-03-30 05:18:49 +00:00
}
else {
$query->leftJoin('taxonomy_vocabulary_node_type', 'n', 'v.vid = n.vid');
}
// If the $vids array is populated, add those to the query.
if ($vids) {
$query->condition('v.vid', $vids, 'IN');
}
// If the conditions array is populated, add those to the query.
if ($conditions) {
foreach ($conditions as $field => $value) {
$query->condition('v.' . $field, $value);
}
}
$result = $query->execute();
$queried_vocabularies = array();
2005-10-21 11:12:46 +00:00
$node_types = array();
2009-03-30 05:18:49 +00:00
foreach ($result as $record) {
// If no node types are associated with a vocabulary, the LEFT JOIN will
// return a NULL value for type.
if (isset($record->type)) {
$node_types[$record->vid][$record->type] = $record->type;
unset($record->type);
$record->nodes = $node_types[$record->vid];
2007-04-10 10:16:29 +00:00
}
2009-03-30 05:18:49 +00:00
elseif (!isset($record->nodes)) {
$record->nodes = array();
}
$queried_vocabularies[$record->vid] = $record;
}
// Invoke hook_taxonomy_vocabulary_load() on the vocabularies loaded from
// the database and add them to the static cache.
if (!empty($queried_vocabularies)) {
foreach (module_implements('taxonomy_vocabulary_load') as $module) {
$function = $module . '_taxonomy_vocabulary_load';
$function($queried_vocabularies);
}
$vocabularies += $queried_vocabularies;
$vocabulary_cache += $queried_vocabularies;
2005-10-21 11:12:46 +00:00
}
2005-01-19 16:22:52 +00:00
}
2008-01-15 08:00:24 +00:00
2009-03-30 05:18:49 +00:00
// Ensure that the returned array is ordered the same as the original $vids
// array if this was passed in and remove any invalid vids.
if ($passed_vids) {
// Remove any invalid vids from the array.
$passed_vids = array_intersect_key($passed_vids, $vocabularies);
foreach ($vocabularies as $vocabulary) {
$passed_vids[$vocabulary->vid] = $vocabulary;
}
$vocabularies = $passed_vids;
}
return $vocabularies;
}
/**
* Return the vocabulary object matching a vocabulary ID.
*
* @param $vid
* The vocabulary's ID.
*
* @return
* The vocabulary object with all of its metadata, if exists, FALSE otherwise.
* Results are statically cached.
*/
2009-04-02 20:39:45 +00:00
function taxonomy_vocabulary_load($vid) {
return reset(taxonomy_vocabulary_load_multiple(array($vid), array()));
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2008-09-19 20:25:03 +00:00
/**
* Return array of tids and join operator.
*
* This is a wrapper function for taxonomy_terms_parse_string which is called
* by the menu system when loading a path with taxonomy terms.
*/
function taxonomy_terms_load($str_tids) {
$terms = taxonomy_terms_parse_string($str_tids);
return $terms;
}
2008-12-05 22:18:46 +00:00
/**
* Load multiple taxonomy terms based on certain conditions.
*
* This function should be used whenever you need to load more than one term
* from the database. Terms are loaded into memory and will not require
* database access if loaded again during the same page request.
*
* @param $tids
* An array of taxonomy term IDs.
* @param $conditions
* An array of conditions to add to the query.
*
* @return
* An array of term objects, indexed by tid.
*/
2009-04-02 20:39:45 +00:00
function taxonomy_term_load_multiple($tids = array(), $conditions = array()) {
$term_cache = &drupal_static(__FUNCTION__, array());
2008-12-05 22:18:46 +00:00
$terms = array();
// Create a new variable which is either a prepared version of the $tids
// array for later comparison with the term cache, or FALSE if no $tids were
// passed. The $tids array is reduced as items are loaded from cache, and we
// need to know if it's empty for this reason to avoid querying the database
// when all requested terms are loaded from cache.
$passed_tids = !empty($tids) ? array_flip($tids) : FALSE;
// Load any available terms from the internal cache.
if ($term_cache) {
if ($tids) {
$terms += array_intersect_key($term_cache, $passed_tids);
// If any terms were loaded, remove them from the $tids still to load.
$tids = array_keys(array_diff_key($passed_tids, $terms));
}
// If only conditions is passed, load all terms from the cache. Terms
// which don't match conditions will be removed later.
elseif ($conditions) {
$terms = $term_cache;
}
}
// Remove any loaded terms from the array if they don't match $conditions.
if ($conditions) {
foreach ($terms as $term) {
$term_values = (array) $term;
if (array_diff_assoc($conditions, $term_values)) {
unset($terms[$term->tid]);
}
}
}
// Load any remaining terms from the database, this is necessary if we have
// $tids still to load, or if $conditions was passed without $tids.
if ($tids || ($conditions && !$passed_tids)) {
2009-01-14 21:16:21 +00:00
$query = db_select('taxonomy_term_data', 't');
$taxonomy_term_data = drupal_schema_fields_sql('taxonomy_term_data');
$query->fields('t', $taxonomy_term_data);
2008-12-05 22:18:46 +00:00
// If the $tids array is populated, add those to the query.
if ($tids) {
$query->condition('t.tid', $tids, 'IN');
}
// If the conditions array is populated, add those to the query.
if ($conditions) {
foreach ($conditions as $field => $value) {
2008-12-19 15:34:27 +00:00
$query->condition('t.' . $field, $value);
2008-12-05 22:18:46 +00:00
}
}
$queried_terms = $query->execute()->fetchAllAssoc('tid');
// Invoke hook_taxonomy_term_load() on the terms loaded from the database
// and add them to the static cache.
if (!empty($queried_terms)) {
foreach (module_implements('taxonomy_term_load') as $module) {
$function = $module . '_taxonomy_term_load';
$function($queried_terms);
}
$terms += $queried_terms;
$term_cache += $queried_terms;
}
}
// Ensure that the returned array is ordered the same as the original $tids
// array if this was passed in and remove any invalid tids.
if ($passed_tids) {
// Remove any invalid tids from the array.
$passed_tids = array_intersect_key($passed_tids, $terms);
foreach ($terms as $term) {
$passed_tids[$term->tid] = $term;
}
$terms = $passed_tids;
}
return $terms;
}
2004-06-18 15:04:37 +00:00
/**
* Return the term object matching a term ID.
2006-10-18 11:15:51 +00:00
*
* @param $tid
* A term's ID
*
2008-12-05 22:18:46 +00:00
* @return
2006-10-18 11:15:51 +00:00
* A term object. Results are statically cached.
2004-06-18 15:04:37 +00:00
*/
2009-04-02 20:39:45 +00:00
function taxonomy_term_load($tid) {
2008-09-19 20:25:03 +00:00
if (!is_numeric($tid)) {
return FALSE;
}
2009-04-02 20:39:45 +00:00
$term = taxonomy_term_load_multiple(array($tid), array());
2008-12-05 22:18:46 +00:00
return $term ? $term[$tid] : FALSE;
2008-11-02 14:42:45 +00:00
}
/**
2009-01-14 21:16:21 +00:00
* Return a term object from the taxonomy_term_data table.
2008-11-02 14:42:45 +00:00
* @param $tid
* A term's ID
* @return Object
* A term object. Results are statically cached.
*/
2009-04-02 20:39:45 +00:00
function taxonomy_get_term_data($tid) {
2009-05-24 17:39:35 +00:00
$terms = &drupal_static(__FUNCTION__, array());
2008-11-02 14:42:45 +00:00
2009-04-02 20:39:45 +00:00
if (!isset($terms[$tid])) {
2009-04-15 14:12:55 +00:00
$terms[$tid] = db_query('SELECT * FROM {taxonomy_term_data} WHERE tid = :tid', array(':tid' => $tid))->fetch();
2006-10-18 11:15:51 +00:00
}
return $terms[$tid];
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2009-05-19 19:01:51 +00:00
/**
* Create a select form element for a given taxonomy vocabulary.
*
* NOTE: This function expects input that has already been sanitized and is
* safe for display. Callers must properly sanitize the $title and
* $description arguments to prevent XSS vulnerabilities.
*
* @param $title
* The title of the vocabulary. This MUST be sanitized by the caller.
* @param $value
* The currently selected terms from this vocabulary, if any.
* @param $vocabulary_id
* The vocabulary ID to build the form element for.
* @param $description
* Help text for the form element. This MUST be sanitized by the caller.
* @param $multiple
* Boolean to control if the form should use a single or multiple select.
* @param $blank
* Optional form choice to use when no value has been selected.
* @param $exclude
* Optional array of term ids to exclude in the selector.
* @return
* A FAPI form array to select terms from the given vocabulary.
*
* @see taxonomy_form()
* @see taxonomy_form_term()
*/
2009-04-15 13:48:03 +00:00
function _taxonomy_term_select($title, $value, $vocabulary_id, $description, $multiple, $blank, $exclude = array()) {
2002-12-02 19:14:41 +00:00
$tree = taxonomy_get_tree($vocabulary_id);
2005-03-03 20:38:18 +00:00
$options = array();
2004-06-23 20:52:46 +00:00
if ($blank) {
2008-11-13 08:13:56 +00:00
$options[0] = $blank;
2004-06-23 20:52:46 +00:00
}
2002-05-02 21:41:29 +00:00
if ($tree) {
foreach ($tree as $term) {
if (!in_array($term->tid, $exclude)) {
2006-10-20 20:55:03 +00:00
$choice = new stdClass();
$choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
$options[] = $choice;
2002-04-14 20:46:41 +00:00
}
}
2002-05-02 21:41:29 +00:00
}
2006-03-11 13:47:51 +00:00
return array('#type' => 'select',
'#title' => $title,
'#default_value' => $value,
'#options' => $options,
'#description' => $description,
'#multiple' => $multiple,
'#size' => $multiple ? min(9, count($options)) : 0,
'#weight' => -15,
'#theme' => 'taxonomy_term_select',
);
2005-11-12 04:31:56 +00:00
}
2006-12-20 10:32:16 +00:00
/**
2007-12-06 09:58:34 +00:00
* Format the selection field for choosing terms
2008-12-30 16:43:20 +00:00
* (by default the default selection field is used).
2007-12-06 09:58:34 +00:00
*
* @ingroup themeable
2006-12-20 10:32:16 +00:00
*/
2005-11-12 04:31:56 +00:00
function theme_taxonomy_term_select($element) {
return theme('select', $element);
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2004-06-18 15:04:37 +00:00
/**
* Finds all nodes that match selected taxonomy conditions.
*
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
* @param $tids
* An array of term IDs to match.
* @param $operator
* How to interpret multiple IDs in the array. Can be "or" or "and".
* @param $depth
2008-12-30 16:43:20 +00:00
* How many levels deep to traverse the taxonomy tree. Can be a non-negative
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
* integer or "all".
2004-06-18 15:04:37 +00:00
* @param $pager
* Whether the nodes are to be used with a pager (the case on most Drupal
* pages) or not (in an XML feed, for example).
2005-05-25 04:04:59 +00:00
* @param $order
2009-05-24 17:39:35 +00:00
* The order clause for the query that retrieve the nodes.
2009-04-15 14:12:55 +00:00
* It is important to specifc the table alias (n).
*
* Example:
* array('table_alias.field_name' = 'DESC');
2004-06-18 15:04:37 +00:00
* @return
2009-01-27 00:22:27 +00:00
* An array of node IDs.
2004-06-18 15:04:37 +00:00
*/
2009-04-15 14:12:55 +00:00
function taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = array('n.sticky' => 'DESC', 'n.created' => 'DESC')) {
if (count($tids) <= 0) {
return array();
}
// For each term ID, generate an array of descendant term IDs to the right depth.
$descendant_tids = array();
if ($depth === 'all') {
$depth = NULL;
}
foreach ($tids as $index => $tid) {
$term = taxonomy_get_term_data($tid);
$tree = taxonomy_get_tree($term->vid, $tid, $depth);
$descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
}
$query = db_select('node', 'n');
$query->addTag('node_access');
$query->condition('n.status', 1);
2003-02-09 17:39:40 +00:00
2009-04-15 14:12:55 +00:00
if ($operator == 'or') {
2007-12-05 21:18:29 +00:00
$args = call_user_func_array('array_merge', $descendant_tids);
2009-04-15 14:12:55 +00:00
$query->join('taxonomy_term_node', 'tn', 'n.vid = tn.vid');
$query->condition('tn.tid', $args, 'IN');
}
else {
foreach ($descendant_tids as $tids) {
$alias = $query->join('taxonomy_term_node', 'tn', 'n.vid = tn.vid');
$query->condition($alias . '.tid', $tids, 'IN');
2003-06-27 17:48:20 +00:00
}
2003-02-09 17:39:40 +00:00
}
2009-05-24 17:39:35 +00:00
2009-04-15 14:12:55 +00:00
if ($pager) {
$count_query = clone $query;
$count_query->addExpression('COUNT(DISTINCT n.nid)');
$query = $query
->extend('PagerDefault')
->limit(variable_get('default_nodes_main', 10));
2009-05-24 17:39:35 +00:00
$query->setCountQuery($count_query);
2009-04-15 14:12:55 +00:00
}
else {
$query->range(0, variable_get('feed_default_items', 10));
}
$query->distinct(TRUE);
$query->addField('n', 'nid');
foreach ($order as $field => $direction) {
$query->orderBy($field, $direction);
// ORDER BY fields need to be loaded too, assume they are in the form table_alias.name
list($table_alias, $name) = explode('.', $field);
$query->addField($table_alias, $name);
}
2003-02-09 17:39:40 +00:00
2009-04-15 14:12:55 +00:00
return $query->execute()->fetchCol();
2003-02-09 17:39:40 +00:00
}
2004-06-18 15:04:37 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_load().
2004-06-18 15:04:37 +00:00
*/
2009-03-08 04:25:07 +00:00
function taxonomy_node_load($nodes) {
2008-12-05 22:18:46 +00:00
// Get an array of tid, vid associations ordered by vocabulary and term
// weight.
$tids = taxonomy_get_tids_from_nodes($nodes);
// Extract the tids only from this array.
$term_ids = array();
foreach ($tids as $term) {
$term_ids[$term->tid] = $term->tid;
}
// Load the full term objects for these tids.
$terms = taxonomy_term_load_multiple($term_ids);
foreach ($tids as $term) {
$nodes[$term->nid]->taxonomy[$term->tid] = $terms[$term->tid];
}
foreach ($nodes as $node) {
if (!isset($nodes[$node->nid]->taxonomy)) {
$node->taxonomy = array();
}
}
2008-10-06 12:55:56 +00:00
}
2007-02-12 17:47:08 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_insert().
2008-10-06 12:55:56 +00:00
*/
2009-03-08 04:25:07 +00:00
function taxonomy_node_insert($node) {
2008-10-06 12:55:56 +00:00
if (!empty($node->taxonomy)) {
taxonomy_node_save($node, $node->taxonomy);
}
}
2007-02-12 17:47:08 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_update().
2008-10-06 12:55:56 +00:00
*/
2009-03-08 04:25:07 +00:00
function taxonomy_node_update($node) {
2008-10-06 12:55:56 +00:00
if (!empty($node->taxonomy)) {
taxonomy_node_save($node, $node->taxonomy);
}
}
2007-02-12 17:47:08 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_delete().
2008-12-18 23:07:50 +00:00
*
* Remove associations of a node to its terms.
2008-10-06 12:55:56 +00:00
*/
2009-03-08 04:25:07 +00:00
function taxonomy_node_delete($node) {
2009-04-15 14:12:55 +00:00
db_delete('taxonomy_term_node')
->condition('nid', $node->nid)
->execute();
2009-04-02 20:39:45 +00:00
drupal_static_reset('taxonomy_term_count_nodes');
2008-10-06 12:55:56 +00:00
}
2007-02-12 17:47:08 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_delete_revision().
2008-12-18 23:07:50 +00:00
*
* Remove associations of a node to its terms.
2008-10-06 12:55:56 +00:00
*/
2009-03-08 04:25:07 +00:00
function taxonomy_node_delete_revision($node) {
2009-04-15 14:12:55 +00:00
db_delete('taxonomy_term_node')
->condition('nid', $node->vid)
->execute();
2009-04-02 20:39:45 +00:00
drupal_static_reset('taxonomy_term_count_nodes');
2008-10-06 12:55:56 +00:00
}
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_validate().
2008-12-18 23:07:50 +00:00
*
* Make sure incoming vids are free tagging enabled.
2008-10-06 12:55:56 +00:00
*/
2009-03-08 04:25:07 +00:00
function taxonomy_node_validate($node, $form) {
2008-12-18 23:07:50 +00:00
if (!empty($node->taxonomy)) {
$terms = $node->taxonomy;
if (!empty($terms['tags'])) {
foreach ($terms['tags'] as $vid => $vid_value) {
$vocabulary = taxonomy_vocabulary_load($vid);
if (empty($vocabulary->tags)) {
// see form_get_error $key = implode('][', $element['#parents']);
// on why this is the key
form_set_error("taxonomy][tags][$vid", t('The %name vocabulary can not be modified in this way.', array('%name' => $vocabulary->name)));
}
}
}
}
2008-10-06 12:55:56 +00:00
}
2007-02-12 17:47:08 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_node_update_index().
2008-10-06 12:55:56 +00:00
*/
2009-03-08 04:25:07 +00:00
function taxonomy_node_update_index($node) {
2006-04-27 22:20:51 +00:00
$output = array();
foreach ($node->taxonomy as $term) {
$output[] = $term->name;
}
if (count($output)) {
2008-04-14 17:48:46 +00:00
return '<strong>(' . implode(', ', $output) . ')</strong>';
2003-04-15 19:50:04 +00:00
}
}
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
/**
2006-12-20 10:32:16 +00:00
* Parses a comma or plus separated string of term IDs.
2006-05-07 03:33:03 +00:00
*
* @param $str_tids
2006-12-20 10:32:16 +00:00
* A string of term IDs, separated by plus or comma.
2006-05-07 03:33:03 +00:00
* comma (,) means AND
* plus (+) means OR
2006-05-08 15:12:25 +00:00
*
2006-12-20 10:32:16 +00:00
* @return an associative array with an operator key (either 'and'
* or 'or') and a tid key containing an array of the term ids.
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
*/
2006-05-07 03:33:03 +00:00
function taxonomy_terms_parse_string($str_tids) {
2007-11-04 14:46:11 +00:00
$terms = array('operator' => '', 'tids' => array());
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str_tids)) {
2006-05-07 03:33:03 +00:00
$terms['operator'] = 'or';
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
// The '+' character in a query string may be parsed as ' '.
2006-05-07 03:33:03 +00:00
$terms['tids'] = preg_split('/[+ ]/', $str_tids);
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
}
2008-10-12 04:30:09 +00:00
elseif (preg_match('/^([0-9]+,)*[0-9]+$/', $str_tids)) {
2006-05-07 03:33:03 +00:00
$terms['operator'] = 'and';
$terms['tids'] = explode(',', $str_tids);
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
}
2008-09-19 20:25:03 +00:00
$terms['str_tids'] = $str_tids;
2006-05-07 03:33:03 +00:00
return $terms;
}
2004-06-18 15:04:37 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_help().
2004-06-18 15:04:37 +00:00
*/
2007-06-30 19:46:58 +00:00
function taxonomy_help($path, $arg) {
switch ($path) {
2005-11-01 10:17:34 +00:00
case 'admin/help#taxonomy':
2008-04-14 17:48:46 +00:00
$output = '<p>' . t('The taxonomy module allows you to categorize content using various systems of classification. Free-tagging vocabularies are created by users on the fly when they submit posts (as commonly found in blogs and social bookmarking applications). Controlled vocabularies allow for administrator-defined short lists of terms as well as complex hierarchies with multiple relationships between different terms. These methods can be applied to different content types and combined together to create a powerful and flexible method of classifying and presenting your content.') . '</p>';
$output .= '<p>' . t('For example, when creating a recipe site, you might want to classify posts by both the type of meal and preparation time. A vocabulary for each allows you to categorize using each criteria independently instead of creating a tag for every possible combination.') . '</p>';
$output .= '<p>' . t('Type of Meal: <em>Appetizer, Main Course, Salad, Dessert</em>') . '</p>';
$output .= '<p>' . t('Preparation Time: <em>0-30mins, 30-60mins, 1-2 hrs, 2hrs+</em>') . '</p>';
$output .= '<p>' . t("Each taxonomy term (often called a 'category' or 'tag' in other systems) automatically provides lists of posts and a corresponding RSS feed. These taxonomy/term URLs can be manipulated to generate AND and OR lists of posts classified with terms. In our recipe site example, it then becomes easy to create pages displaying 'Main courses', '30 minute recipes', or '30 minute main courses and appetizers' by using terms on their own or in combination with others. There are a significant number of contributed modules which you to alter and extend the behavior of the core module for both display and organization of terms.") . '</p>';
$output .= '<p>' . t("Terms can also be organized in parent/child relationships from the admin interface. An example would be a vocabulary grouping countries under their parent geo-political regions. The taxonomy module also enables advanced implementations of hierarchy, for example placing Turkey in both the 'Middle East' and 'Europe'.") . '</p>';
$output .= '<p>' . t('The taxonomy module supports the use of both synonyms and related terms, but does not directly use this functionality. However, optional contributed or custom modules may make full use of these advanced features.') . '</p>';
$output .= '<p>' . t('For more information, see the online handbook entry for <a href="@taxonomy">Taxonomy module</a>.', array('@taxonomy' => 'http://drupal.org/handbook/modules/taxonomy/')) . '</p>';
2005-11-01 10:17:34 +00:00
return $output;
2006-07-31 11:25:55 +00:00
case 'admin/content/taxonomy':
2008-04-14 17:48:46 +00:00
$output = '<p>' . t("The taxonomy module allows you to categorize your content using both tags and administrator defined terms. It is a flexible tool for classifying content with many advanced features. To begin, create a 'Vocabulary' to hold one set of terms or tags. You can create one free-tagging vocabulary for everything, or separate controlled vocabularies to define the various properties of your content, for example 'Countries' or 'Colors'.") . '</p>';
$output .= '<p>' . t('Use the list below to configure and review the vocabularies defined on your site, or to list and manage the terms (tags) they contain. A vocabulary may (optionally) be tied to specific content types as shown in the <em>Type</em> column and, if so, will be displayed when creating or editing posts of that type. Multiple vocabularies tied to the same content type will be displayed in the order shown below. To change the order of a vocabulary, grab a drag-and-drop handle under the <em>Name</em> column and drag it to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Remember that your changes will not be saved until you click the <em>Save</em> button at the bottom of the page.') . '</p>';
2008-01-27 17:55:15 +00:00
return $output;
2008-09-19 20:25:03 +00:00
case 'admin/content/taxonomy/%/list':
2007-11-26 19:46:52 +00:00
$vocabulary = taxonomy_vocabulary_load($arg[3]);
if ($vocabulary->tags) {
2008-04-14 17:48:46 +00:00
return '<p>' . t('%capital_name is a free-tagging vocabulary. To change the name or description of a term, click the <em>edit</em> link next to the term.', array('%capital_name' => drupal_ucfirst($vocabulary->name))) . '</p>';
2007-11-26 19:46:52 +00:00
}
switch ($vocabulary->hierarchy) {
case 0:
2008-04-14 17:48:46 +00:00
return '<p>' . t('%capital_name is a flat vocabulary. You may organize the terms in the %name vocabulary by using the handles on the left side of the table. To change the name or description of a term, click the <em>edit</em> link next to the term.', array('%capital_name' => drupal_ucfirst($vocabulary->name), '%name' => $vocabulary->name)) . '</p>';
2007-11-26 19:46:52 +00:00
case 1:
2008-04-14 17:48:46 +00:00
return '<p>' . t('%capital_name is a single hierarchy vocabulary. You may organize the terms in the %name vocabulary by using the handles on the left side of the table. To change the name or description of a term, click the <em>edit</em> link next to the term.', array('%capital_name' => drupal_ucfirst($vocabulary->name), '%name' => $vocabulary->name)) . '</p>';
2007-11-26 19:46:52 +00:00
case 2:
2008-04-14 17:48:46 +00:00
return '<p>' . t('%capital_name is a multiple hierarchy vocabulary. To change the name or description of a term, click the <em>edit</em> link next to the term. Drag and drop of multiple hierarchies is not supported, but you can re-enable drag and drop support by editing each term to include only a single parent.', array('%capital_name' => drupal_ucfirst($vocabulary->name))) . '</p>';
2007-11-26 19:46:52 +00:00
}
2008-09-19 20:25:03 +00:00
case 'admin/content/taxonomy/add':
2008-04-14 17:48:46 +00:00
return '<p>' . t('Define how your vocabulary will be presented to administrators and users, and which content types to categorize with it. Tags allows users to create terms when submitting posts by typing a comma separated list. Otherwise terms are chosen from a select list and can only be created by users with the "administer taxonomy" permission.') . '</p>';
2003-08-21 18:02:37 +00:00
}
2002-05-02 21:41:29 +00:00
}
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
/**
* Helper function for array_map purposes.
*/
function _taxonomy_get_tid_from_term($term) {
return $term->tid;
}
2007-04-05 03:08:48 +00:00
/**
* Implode a list of tags of a certain vocabulary into a string.
*/
function taxonomy_implode_tags($tags, $vid = NULL) {
$typed_tags = array();
foreach ($tags as $tag) {
// Extract terms belonging to the vocabulary in question.
2007-04-11 09:17:53 +00:00
if (is_null($vid) || $tag->vid == $vid) {
2007-04-05 03:08:48 +00:00
2007-04-11 09:17:53 +00:00
// Commas and quotes in tag names are special cases, so encode 'em.
if (strpos($tag->name, ',') !== FALSE || strpos($tag->name, '"') !== FALSE) {
2008-04-14 17:48:46 +00:00
$tag->name = '"' . str_replace('"', '""', $tag->name) . '"';
2007-04-05 03:08:48 +00:00
}
2007-04-11 09:17:53 +00:00
$typed_tags[] = $tag->name;
2007-04-05 03:08:48 +00:00
}
}
2007-04-06 13:27:23 +00:00
return implode(', ', $typed_tags);
2007-04-05 03:08:48 +00:00
}
2007-06-29 18:06:51 +00:00
/**
2009-05-27 18:34:03 +00:00
* Implement hook_hook_info().
2007-06-29 18:06:51 +00:00
*/
function taxonomy_hook_info() {
return array(
'taxonomy' => array(
'taxonomy' => array(
'insert' => array(
2008-01-15 08:00:24 +00:00
'runs when' => t('After saving a new term to the database'),
2007-06-29 18:06:51 +00:00
),
'update' => array(
2008-01-15 08:00:24 +00:00
'runs when' => t('After saving an updated term to the database'),
2007-06-29 18:06:51 +00:00
),
'delete' => array(
2008-01-15 08:00:24 +00:00
'runs when' => t('After deleting a term')
2007-06-29 18:06:51 +00:00
),
),
),
);
2007-07-11 15:27:57 +00:00
}