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-07-05 18:00:11 +00:00
* Implement hook_permission().
2004-06-18 15:04:37 +00:00
*/
2009-07-05 18:00:11 +00:00
function taxonomy_permission() {
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
2009-06-12 13:59:56 +00:00
/**
2009-08-25 21:53:48 +00:00
* Implement hook_entity_info().
2009-06-12 13:59:56 +00:00
*/
2009-08-25 21:53:48 +00:00
function taxonomy_entity_info() {
2009-06-12 13:59:56 +00:00
$return = array(
'taxonomy_term' => array(
2009-07-10 05:58:13 +00:00
'label' => t('Taxonomy term'),
2009-08-25 21:53:48 +00:00
'controller class' => 'TaxonomyTermController',
'base table' => 'taxonomy_term_data',
'fieldable' => TRUE,
2009-07-10 05:58:13 +00:00
'object keys' => array(
'id' => 'tid',
'bundle' => 'vocabulary_machine_name',
),
'bundle keys' => array(
'bundle' => 'machine_name',
),
'bundles' => array(),
2009-06-12 13:59:56 +00:00
),
);
2009-08-25 21:53:48 +00:00
foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocabulary) {
$return['taxonomy_term']['bundles'][$machine_name] = array(
2009-07-10 05:58:13 +00:00
'label' => $vocabulary->name,
'admin' => array(
2009-07-20 18:51:36 +00:00
'path' => 'admin/structure/taxonomy/%taxonomy_vocabulary',
'real path' => 'admin/structure/taxonomy/' . $vocabulary->vid,
2009-07-10 05:58:13 +00:00
'bundle argument' => 3,
'access arguments' => array('administer taxonomy'),
),
);
}
2009-08-25 21:53:48 +00:00
$return['taxonomy_vocabulary'] = array(
'label' => t('Taxonomy vocabulary'),
'controller class' => 'TaxonomyVocabularyController',
'base table' => 'taxonomy_vocabulary',
'object keys' => array(
'id' => 'vid',
),
'fieldable' => FALSE,
);
2009-06-12 13:59:56 +00:00
return $return;
}
2009-10-08 07:58:47 +00:00
/**
* Return nodes attached to a term across all field instances.
*
* This function requires taxonomy module to be maintaining its own tables,
* and will return an empty array if it is not. If using other field storage
* methods alternatives methods for listing terms will need to be used.
*
* @param $term
* The term object.
* @param $pager
* Boolean to indicate whether a pager should be used.
* @order
* An array of fields and directions.
*
* @return
* An array of nids matching the query.
*/
function taxonomy_select_nodes($term, $pager = TRUE, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC')) {
if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
return array();
}
$query = db_select('taxonomy_index', 't');
$query->addTag('node_access');
if ($pager) {
$count_query = clone $query;
$count_query->addExpression('COUNT(t.nid)');
$query = $query
->extend('PagerDefault')
->limit(variable_get('default_nodes_main', 10));
$query->setCountQuery($count_query);
}
else {
$query->range(0, variable_get('feed_default_items', 10));
}
$query->condition('tid', $term->tid );
$query->addField('t', 'nid');
$query->addField('t', 'tid');
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);
}
return $query->execute()->fetchCol();
}
2009-06-12 13:59:56 +00:00
/**
* Implement hook_field_build_modes();
*
* @TODO: build mode for display as a field (when attached to nodes etc.).
*/
function taxonomy_field_build_modes($obj_type) {
$modes = array();
if ($obj_type == 'term') {
$modes = array(
'full' => t('Taxonomy term page'),
);
}
return $modes;
}
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()),
),
2009-08-18 06:01:07 +00:00
'taxonomy_autocomplete' => array(
'arguments' => array('element' => NULL),
),
2007-04-06 13:27:23 +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() {
2009-07-20 18:51:36 +00:00
$items['admin/structure/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'),
2009-08-24 00:14:23 +00:00
'file' => 'taxonomy.admin.inc',
2007-01-24 14:48:36 +00:00
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/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,
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/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'),
2009-08-23 01:05:12 +00:00
'type' => MENU_LOCAL_ACTION,
2009-08-24 00:14:23 +00:00
'file' => 'taxonomy.admin.inc',
2007-01-24 14:48:36 +00:00
);
2009-07-08 07:18:08 +00:00
$items['taxonomy/term/%taxonomy_term'] = array(
2008-09-19 20:25:03 +00:00
'title' => 'Taxonomy term',
2009-07-08 07:18:08 +00:00
'title callback' => 'taxonomy_term_title',
'title arguments' => array(2),
2008-09-19 20:25:03 +00:00
'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,
2009-08-24 00:14:23 +00:00
'file' => 'taxonomy.pages.inc',
2007-01-24 14:48:36 +00:00
);
2009-07-08 07:18:08 +00:00
$items['taxonomy/term/%taxonomy_term/view'] = array(
2008-09-19 20:25:03 +00:00
'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,
2009-08-24 00:14:23 +00:00
'file' => 'taxonomy.pages.inc',
2007-01-24 14:48:36 +00:00
);
2009-07-08 07:18:08 +00:00
$items['taxonomy/term/%taxonomy_term/feed'] = array(
'title' => 'Taxonomy term',
'title callback' => 'taxonomy_term_title',
'title arguments' => array(2),
'page callback' => 'taxonomy_term_feed',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
2009-08-24 00:14:23 +00:00
'file' => 'taxonomy.feeds.inc',
2009-07-08 07:18:08 +00:00
);
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,
2009-08-24 00:14:23 +00:00
'file' => 'taxonomy.pages.inc',
2007-01-24 14:48:36 +00:00
);
2008-09-19 20:25:03 +00:00
2009-07-20 18:51:36 +00:00
$items['admin/structure/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,
2009-08-24 00:14:23 +00:00
'file' => 'taxonomy.admin.inc',
2007-01-24 14:48:36 +00:00
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/taxonomy/%taxonomy_vocabulary/edit'] = array(
2008-09-19 20:25:03 +00:00
'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,
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/taxonomy/%taxonomy_vocabulary/list'] = array(
2008-09-19 20:25:03 +00:00
'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,
2009-08-24 00:14:23 +00:00
'file' => 'taxonomy.admin.inc',
2007-01-24 14:48:36 +00:00
);
2009-08-23 01:05:12 +00:00
$items['admin/structure/taxonomy/%taxonomy_vocabulary/list/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'),
2009-08-23 01:05:12 +00:00
'type' => MENU_LOCAL_ACTION,
2009-08-24 00:14:23 +00:00
'file' => 'taxonomy.admin.inc',
2007-01-24 14:48:36 +00:00
);
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) {
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');
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-06-12 13:59:56 +00:00
field_attach_create_bundle($vocabulary->machine_name);
2009-10-08 07:58:47 +00:00
taxonomy_vocabulary_create_field($vocabulary);
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-08-25 21:53:48 +00:00
entity_get_controller('taxonomy_vocabulary')->resetCache();
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();
$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
2009-06-12 13:59:56 +00:00
field_attach_delete_bundle($vocabulary['machine_name']);
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-08-25 21:53:48 +00:00
entity_get_controller('taxonomy_vocabulary')->resetCache();
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;
}
2009-10-08 07:58:47 +00:00
/**
* Create a default field when a vocabulary is created.
*
* @param $vocabulary
* A taxonomy vocabulary object.
*/
function taxonomy_vocabulary_create_field($vocabulary) {
$field = array(
'field_name' => 'taxonomy_' . $vocabulary->machine_name,
'type' => 'taxonomy_term',
// Set cardinality to unlimited so that select
// and autocomplete widgets behave as normal.
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vid' => $vocabulary->vid,
'parent' => 0,
),
),
),
);
field_create_field($field);
}
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);
}
2009-06-12 13:59:56 +00:00
if (!isset($term->vocabulary_machine_name)) {
$vocabulary = taxonomy_vocabulary_load($term->vid);
$term->vocabulary_machine_name = $vocabulary->machine_name;
}
field_attach_presave('taxonomy_term', $term);
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');
2009-06-12 13:59:56 +00:00
field_attach_update('taxonomy_term', $term);
2009-08-02 10:51:17 +00:00
module_invoke_all('taxonomy_term_update', $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);
2009-07-31 07:43:33 +00:00
_taxonomy_clean_field_cache($term);
2009-06-12 13:59:56 +00:00
field_attach_insert('taxonomy_term', $term);
2009-08-02 10:51:17 +00:00
module_invoke_all('taxonomy_term_insert', $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_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-10-08 07:58:47 +00:00
if (!is_array($term->parent)) {
$term->parent = array($term->parent);
}
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
}
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_synonym')
->condition('tid', $tid)
->execute();
2004-10-15 05:10:35 +00:00
2009-06-12 13:59:56 +00:00
field_attach_delete('taxonomy_term', $term);
2009-07-31 07:43:33 +00:00
_taxonomy_clean_field_cache($term);
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');
2009-08-25 21:53:48 +00:00
entity_get_controller('taxonomy_term')->resetCache();
2009-04-02 20:39:45 +00:00
}
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
2009-10-08 07:58:47 +00:00
$blank = t('- Please choose -');
2002-05-31 20:29:30 +00:00
2009-10-08 07:58:47 +00:00
return _taxonomy_term_select(check_plain($vocabulary->name), $value, $vid, $help, $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
*/
2009-10-08 07:58:47 +00:00
function taxonomy_form_all() {
2006-05-16 06:50:21 +00:00
$vocabularies = taxonomy_get_vocabularies();
$options = array();
foreach ($vocabularies as $vid => $vocabulary) {
$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-10-08 07:58:47 +00:00
function taxonomy_get_vocabularies() {
return taxonomy_vocabulary_load_multiple(FALSE, array());
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
2009-06-12 13:59:56 +00:00
/**
* Get names for all taxonomy vocabularies.
*
* @return
2009-08-25 21:53:48 +00:00
* An array of vocabulary ids, names, machine names, keyed by machine name.
2009-06-12 13:59:56 +00:00
*/
function taxonomy_vocabulary_get_names() {
2009-08-25 21:53:48 +00:00
$names = db_query('SELECT name, machine_name, vid FROM {taxonomy_vocabulary}')->fetchAllAssoc('machine_name');
2009-06-12 13:59:56 +00:00
return $names;
}
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
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-06-03 19:34:53 +00:00
return taxonomy_term_load_multiple(array(), array('name' => trim($name)));
2002-12-12 18:54:17 +00:00
}
2009-08-25 21:53:48 +00:00
/**
* Controller class for taxonomy terms.
*
* This extends the DrupalDefaultEntityController class. Only alteration is
* that we match the condition on term name case-independently.
*/
class TaxonomyTermController extends DrupalDefaultEntityController {
protected $type;
public function load($ids = array(), $conditions = array()) {
if (isset($conditions['type'])) {
$this->type = $conditions['type'];
unset($conditions['type']);
2009-03-30 05:18:49 +00:00
}
2009-08-25 21:53:48 +00:00
return parent::load($ids, $conditions);
2009-03-30 05:18:49 +00:00
}
2009-08-25 21:53:48 +00:00
protected function buildQuery() {
parent::buildQuery();
// When name is passed as a condition use LIKE.
if (isset($this->conditions['name'])) {
$conditions = &$this->query->conditions();
foreach ($conditions as $key => $condition) {
if ($condition['field'] == 'base.name') {
$conditions[$key]['operator'] = 'LIKE';
}
2009-03-30 05:18:49 +00:00
}
}
2009-08-25 21:53:48 +00:00
// Add the machine name field from the {taxonomy_vocabulary} table.
$this->query->innerJoin('taxonomy_vocabulary', 'v', 'base.vid = v.vid');
$this->query->addField('v', 'machine_name', 'vocabulary_machine_name');
}
protected function cacheGet($ids) {
$terms = parent::cacheGet($ids);
// Name matching is case insensitive, note that with some collations
// LOWER() and drupal_strtolower() may return different results.
foreach ($terms as $term) {
$term_values = (array) $term;
if (isset($this->conditions['name']) && drupal_strtolower($this->conditions['name'] != drupal_strtolower($term_values['name']))) {
unset($terms[$term->tid]);
}
2009-03-30 05:18:49 +00:00
}
2009-08-25 21:53:48 +00:00
return $terms;
}
}
2009-03-30 05:18:49 +00:00
2009-08-25 21:53:48 +00:00
/**
* Controller class for taxonomy vocabularies.
*
* This extends the DrupalDefaultEntityController class, adding required
* special handling for taxonomy vocabulary objects.
*/
class TaxonomyVocabularyController extends DrupalDefaultEntityController {
protected function buildQuery() {
parent::buildQuery();
$this->query->orderBy('base.weight');
$this->query->orderBy('base.name');
}
2009-03-30 05:18:49 +00:00
2009-08-25 21:53:48 +00:00
protected function attachLoad(&$records) {
foreach ($records as $record) {
2009-03-30 05:18:49 +00:00
// If no node types are associated with a vocabulary, the LEFT JOIN will
// return a NULL value for type.
$queried_vocabularies[$record->vid] = $record;
}
2009-08-25 21:53:48 +00:00
$records = $queried_vocabularies;
parent::attachLoad($records);
2005-01-19 16:22:52 +00:00
}
2002-05-02 21:41:29 +00:00
}
2002-04-14 20:46:41 +00:00
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.
*
2009-08-25 21:53:48 +00:00
* @see entity_load()
*
2008-12-05 22:18:46 +00:00
* @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()) {
2009-08-25 21:53:48 +00:00
return entity_load('taxonomy_term', $tids, $conditions);
}
2008-12-05 22:18:46 +00:00
2009-08-25 21:53:48 +00:00
/**
* Load multiple taxonomy vocabularies based on certain conditions.
*
* 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.
*
* @see entity_load()
*
* @param $vids
* An array of taxonomy vocabulary IDs, or FALSE to load all vocabularies.
* @param $conditions
* An array of conditions to add to the query.
*
* @return
* An array of vocabulary objects, indexed by vid.
*/
function taxonomy_vocabulary_load_multiple($vids = array(), $conditions = array()) {
return entity_load('taxonomy_vocabulary', $vids, $conditions);
}
2008-12-05 22:18:46 +00:00
2009-08-25 21:53:48 +00:00
/**
* 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.
*/
function taxonomy_vocabulary_load($vid) {
return reset(taxonomy_vocabulary_load_multiple(array($vid)));
2008-12-05 22:18:46 +00:00
}
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-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-10-08 07:58:47 +00:00
function _taxonomy_term_select($title, $value, $vocabulary_id, $description, $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,
'#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
*/
2009-10-09 01:00:08 +00:00
function theme_taxonomy_term_select($variables) {
return theme('select', $variables['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
/**
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;
2009-07-20 18:51:36 +00:00
case 'admin/structure/taxonomy':
2009-09-09 11:28:57 +00:00
$output = '<p>' . t('Configure the vocabularies and terms for your site.') . '</p>';
2008-01-27 17:55:15 +00:00
return $output;
2009-07-20 18:51:36 +00:00
case 'admin/structure/taxonomy/%/list':
2007-11-26 19:46:52 +00:00
$vocabulary = taxonomy_vocabulary_load($arg[3]);
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
}
2009-07-20 18:51:36 +00:00
case 'admin/structure/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-07-31 07:43:33 +00:00
/**
* Implement hook_field_info().
*
* Field settings:
* - allowed_values: a list array of one or more vocabulary trees:
* - vid: a vocabulary ID.
* - parent: a term ID of a term whose children are allowed. This should be
* '0' if all terms in a vocabulary are allowed. The allowed values do not
* include the parent term.
*
*/
function taxonomy_field_info() {
return array(
'taxonomy_term' => array(
'label' => t('Taxonomy term'),
'description' => t('This field stores a reference to a taxonomy term.'),
'default_widget' => 'options_select',
'default_formatter' => 'taxonomy_term_link',
'settings' => array(
'allowed_values' => array(
array(
'vid' => '0',
'parent' => '0',
),
),
),
),
);
}
2009-08-18 06:01:07 +00:00
/**
* Implement hook_field_widget_info().
*
* We need custom handling of multiple values because we need
* to combine them into a options list rather than display
* cardinality elements. We will use the field module's default
* handling for default values.
*
* Callbacks can be omitted if default handing is used.
* They're included here just so this module can be used
* as an example for custom modules that might do things
* differently.
*/
function taxonomy_field_widget_info() {
return array(
'taxonomy_autocomplete' => array(
'label' => t('Autocomplete term widget (tagging)'),
'field types' => array('taxonomy_term'),
'settings' => array(
'size' => 60,
'autocomplete_path' => 'taxonomy/autocomplete',
),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
),
),
);
}
2009-07-31 07:43:33 +00:00
/**
* Implement hook_field_widget_info_alter().
*/
function taxonomy_field_widget_info_alter(&$info) {
$info['options_select']['field types'][] = 'taxonomy_term';
$info['options_buttons']['field types'][] = 'taxonomy_term';
}
/**
* Implement hook_field_schema().
*/
function taxonomy_field_schema($field) {
return array(
'columns' => array(
'value' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
),
'indexes' => array(
'value' => array('value'),
),
);
}
/**
* Implement hook_field_validate().
*
* Possible error codes:
* - 'taxonomy_term_illegal_value': The value is not part of the list of allowed values.
*/
2009-08-22 00:58:55 +00:00
function taxonomy_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) {
2009-07-31 07:43:33 +00:00
$allowed_values = taxonomy_allowed_values($field);
2009-08-18 06:01:07 +00:00
$widget = field_info_widget_types($instance['widget']['type']);
2009-08-24 00:14:23 +00:00
2009-08-18 06:01:07 +00:00
// Check we don't exceed the allowed number of values for widgets with custom
// behavior for multiple values (taxonomy_autocomplete widget).
if ($widget['behaviors']['multiple values'] == FIELD_BEHAVIOR_CUSTOM && $field['cardinality'] >= 2) {
if (count($items) > $field['cardinality']) {
$errors[$field['field_name']][0][] = array(
'error' => 'taxonomy_term_illegal_value',
'message' => t('%name: this field cannot hold more that @count values.', array('%name' => t($instance['label']), '@count' => $field['cardinality'])),
);
}
}
2009-07-31 07:43:33 +00:00
foreach ($items as $delta => $item) {
if (!empty($item['value'])) {
if (!isset($allowed_values[$item['value']])) {
$errors[$field['field_name']][$delta][] = array(
'error' => 'taxonomy_term_illegal_value',
'message' => t('%name: illegal value.', array('%name' => t($instance['label']))),
);
}
}
}
}
/**
* Implement hook_field_is_empty().
*/
function taxonomy_field_is_empty($item, $field) {
2009-10-08 07:58:47 +00:00
if (!is_array($item) || (empty($item['value']) && (string) $item['value'] !== '0')) {
2009-07-31 07:43:33 +00:00
return TRUE;
}
return FALSE;
}
/**
* Implement hook_field_formatter_info().
*/
function taxonomy_field_formatter_info() {
return array(
'taxonomy_term_link' => array(
'label' => t('Link'),
'field types' => array('taxonomy_term'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
),
'taxonomy_term_plain' => array(
'label' => t('Plain text'),
'field types' => array('taxonomy_term'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
),
);
}
/**
* Theme function for 'link' term field formatter.
*/
2009-10-09 01:00:08 +00:00
function theme_field_formatter_taxonomy_term_link($variables) {
$term = $variables['element']['#item']['taxonomy_term'];
2009-07-31 07:43:33 +00:00
return l($term->name, taxonomy_term_path($term));
}
/**
* Theme function for 'plain' term field formatter.
*/
2009-10-09 01:00:08 +00:00
function theme_field_formatter_taxonomy_term_plain($variables) {
$term = $variables['element']['#item']['taxonomy_term'];
2009-07-31 07:43:33 +00:00
return $term->name;
}
/**
* Create an array of the allowed values for this field.
*
* Call the field's allowed_values function to retrieve the allowed
* values array.
*
* @see _taxonomy_term_select()
*/
function taxonomy_allowed_values($field) {
$options = array();
foreach ($field['settings']['allowed_values'] as $tree) {
$terms = taxonomy_get_tree($tree['vid'], $tree['parent']);
if ($terms) {
foreach ($terms as $term) {
$options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
}
}
}
return $options;
}
/**
* Implement hook_field_load().
*
* This preloads all taxonomy terms for multiple loaded objects at once and
* unsets values for invalid terms that do not exist.
*/
2009-08-22 00:58:55 +00:00
function taxonomy_field_load($obj_type, $objects, $field, $instances, $langcode, &$items, $age) {
2009-07-31 07:43:33 +00:00
$tids = array();
// Collect every possible term attached to any of the fieldable entities.
foreach ($objects as $id => $object) {
foreach ($items[$id] as $delta => $item) {
// Force the array key to prevent duplicates.
$tids[$item['value']] = $item['value'];
}
}
if ($tids) {
$terms = array();
// Avoid calling taxonomy_term_load_multiple because it could lead to
// circular references.
$query = db_select('taxonomy_term_data', 't');
$query->fields('t');
$query->condition('t.tid', $tids, 'IN');
$query->addTag('term_access');
$terms = $query->execute()->fetchAllAssoc('tid');
// Iterate through the fieldable entities again to attach the loaded term data.
foreach ($objects as $id => $object) {
foreach ($items[$id] as $delta => $item) {
// Check whether the taxonomy term field instance value could be loaded.
if (isset($terms[$item['value']])) {
// Replace the instance value with the term data.
$items[$id][$delta]['taxonomy_term'] = $terms[$item['value']];
}
// Otherwise, unset the instance value, since the term does not exist.
else {
unset($items[$id][$delta]);
}
}
}
}
}
/**
* Helper function that clears field cache when terms are updated or deleted
*/
function _taxonomy_clean_field_cache($term) {
$cids = array();
// Determine object types that are not cacheable.
$obj_types = array();
foreach (field_info_fieldable_types() as $obj_type => $info) {
2009-08-25 21:53:48 +00:00
if (isset($info['cacheable']) && !$info['cacheable']) {
2009-07-31 07:43:33 +00:00
$obj_types[] = $obj_type;
}
}
// Load info for all taxonomy term fields.
$fields = field_read_fields(array('type' => 'taxonomy_term'));
foreach ($fields as $field_name => $field) {
// Assemble an array of vocabulary IDs that are used in this field.
foreach ($field['settings']['allowed_values'] as $tree) {
$vids[$tree['vid']] = $tree['vid'];
}
2009-08-24 00:14:23 +00:00
2009-07-31 07:43:33 +00:00
// Check this term's vocabulary against those used for the field's options.
if (in_array($term->vid, $vids)) {
$conditions = array(array('value', $term->tid));
if ($obj_types) {
$conditions[] = array('type', $obj_types, 'NOT IN');
}
2009-08-11 14:59:40 +00:00
$results = field_attach_query($field['id'], $conditions, FIELD_QUERY_NO_LIMIT);
2009-07-31 07:43:33 +00:00
foreach ($results as $obj_type => $objects) {
foreach (array_keys($objects) as $id) {
$cids[] = "field:$obj_type:$id";
}
}
}
}
if ($cids) {
cache_clear_all($cids, 'cache_field');
}
}
2009-07-08 07:18:08 +00:00
/**
* Title callback for term pages.
*
* @param $term
* A term object.
* @return
* The term name to be used as the page title.
*/
function taxonomy_term_title($term) {
return check_plain($term->name);
}
2009-08-18 06:01:07 +00:00
/**
* Implement hook_field_widget().
*/
2009-08-27 00:33:52 +00:00
function taxonomy_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = NULL) {
2009-08-18 06:01:07 +00:00
$element = array(
'#type' => $instance['widget']['type'],
'#default_value' => !empty($items) ? $items : array(),
);
return $element;
}
/**
* Implement hook_field_widget_error().
*/
function taxonomy_field_widget_error($element, $error) {
$field_key = $element['#columns'][0];
form_error($element[$field_key], $error['message']);
}
/**
* Process an individual autocomplete widget element.
*
* Build the form element. When creating a form using FAPI #process, note that
* $element['#value'] is already set.
*
* The $field and $instance arrays are in $form['#fields'][$element['#field_name']].
*
* @todo For widgets to be actual FAPI 'elements', reusable outside of a 'field'
* context, they shoudn't rely on $field and $instance. The bits of information
* needed to adjust the behavior of the 'element' should be extracted in
* hook_field_widget() above.
*/
function taxonomy_autocomplete_elements_process($element, &$form_state, $form) {
$field = $form['#fields'][$element['#field_name']]['field'];
$instance = $form['#fields'][$element['#field_name']]['instance'];
$field_key = $element['#columns'][0];
// See if this element is in the database format or the transformed format,
// and transform it if necessary.
2009-08-24 21:00:23 +00:00
if (is_array($element['#value'])) {
if (!array_key_exists($field_key, $element['#value'])) {
$tags = array();
foreach ($element['#default_value'] as $item) {
$tags[$item['value']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['value']);
}
$typed_string = taxonomy_implode_tags($tags);
}
else {
$typed_string = $element['#value'][$field_key];
2009-08-18 06:01:07 +00:00
}
}
2009-08-24 21:00:23 +00:00
else {
$typed_string = $element['#value'];
}
2009-08-18 06:01:07 +00:00
$value = array();
$element[$field_key] = array(
'#type' => 'textfield',
'#default_value' => $typed_string,
'#autocomplete_path' => 'taxonomy/autocomplete/'. $element['#field_name'] .'/'. $element['#bundle'],
'#size' => $instance['widget']['settings']['size'],
2009-08-22 14:34:23 +00:00
'#attributes' => array('class' => array('text')),
2009-08-18 06:01:07 +00:00
'#title' => $element['#title'],
'#description' => $element['#description'],
'#required' => $element['#required'],
);
$element[$field_key]['#maxlength'] = !empty($field['settings']['max_length']) ? $field['settings']['max_length'] : NULL;
// Set #element_validate in a way that it will not wipe out other validation
// functions already set by other modules.
if (empty($element['#element_validate'])) {
$element['#element_validate'] = array();
}
array_unshift($element['#element_validate'], 'taxonomy_autocomplete_validate');
// Make sure field info will be available to the validator which does not get
// the values in $form.
$form_state['#fields'][$element['#field_name']] = $form['#fields'][$element['#field_name']];
return $element;
}
/**
* FAPI function to validate taxonomy term autocomplete element.
*/
function taxonomy_autocomplete_validate($element, &$form_state) {
2009-08-24 21:00:23 +00:00
$field_name = $element['#field_name'];
if (!isset($form_state['values'][$field_name])) {
return;
}
2009-08-18 06:01:07 +00:00
// Autocomplete widgets do not send their tids in the form, so we must detect
// them here and process them independently.
2009-08-24 21:00:23 +00:00
$langcode = $form_state['complete form'][$field_name]['#language'];
if ($tags = $form_state['values'][$field_name][$langcode]['value']) {
2009-08-18 06:01:07 +00:00
// @see taxonomy_node_save
$field = $form_state['#fields'][$element['#field_name']]['field'];
$field_key = $element['#columns'][0];
$vids = array();
foreach ($field['settings']['allowed_values'] as $tree) {
$vids[] = $tree['vid'];
}
2009-08-24 21:00:23 +00:00
$typed_terms = drupal_explode_tags($tags);
2009-08-18 06:01:07 +00:00
$values = array();
foreach ($typed_terms as $typed_term) {
// See if the term exists in the chosen vocabulary and return the tid;
// otherwise, add a new record.
$possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => $vids));
$typed_term_tid = NULL;
// tid match, if any.
foreach ($possibilities as $possibility) {
$typed_term_tid = $possibility->tid;
break;
}
if (!$typed_term_tid) {
$vocabulary = taxonomy_vocabulary_load($vids[0]);
$edit = array(
'vid' => $vids[0],
'name' => $typed_term,
'vocabulary_machine_name' => $vocabulary->machine_name,
);
$term = (object) $edit;
if ($status = taxonomy_term_save($term)) {
$typed_term_tid = $term->tid;
}
}
$values[$typed_term_tid] = $typed_term_tid;
}
$results = options_transpose_array_rows_cols(array($field_key => $values));
form_set_value($element, $results, $form_state);
}
}
/**
2009-09-10 06:31:39 +00:00
* Implement hook_element_info().
*/
function taxonomy_element_info() {
$types['taxonomy_autocomplete'] = array(
'#input' => TRUE,
'#columns' => array('value'),
'#delta' => 0,
'#process' => array('taxonomy_autocomplete_elements_process'),
2009-08-18 06:01:07 +00:00
);
2009-09-10 06:31:39 +00:00
return $types;
2009-08-18 06:01:07 +00:00
}
2009-08-19 13:31:14 +00:00
/**
* Implement hook_field_settings_form().
*/
2009-09-26 15:57:39 +00:00
function taxonomy_field_settings_form($field, $instance, $has_data) {
2009-08-19 13:31:14 +00:00
// Get proper values for 'allowed_values_function', which is a core setting.
$vocabularies = taxonomy_get_vocabularies();
$options = array();
foreach ($vocabularies as $vocabulary) {
$options[$vocabulary->vid] = $vocabulary->name;
}
$form['allowed_values'] = array(
'#tree' => TRUE,
);
foreach ($field['settings']['allowed_values'] as $delta => $tree) {
$form['allowed_values'][$delta]['vid'] = array(
'#type' => 'select',
'#title' => t('Vocabulary'),
'#default_value' => $tree['vid'],
'#options' => $options,
'#required' => TRUE,
'#description' => t('The vocabulary which supplies the options for this field.'),
2009-09-26 15:57:39 +00:00
'#disabled' => $has_data,
2009-08-19 13:31:14 +00:00
);
$form['allowed_values'][$delta]['parent'] = array(
'#type' => 'value',
'#value' => $tree['parent'],
);
}
return $form;
}
2009-10-08 07:58:47 +00:00
/**
* @defgroup taxonomy indexing Taxonomy functions maintaining {taxonomy_index}.
*
* Taxonomy uses default field storage to store canonical relationships
* between terms and fieldable entities. However its most common use case
* requires listing all content associated with a term or group of terms
* sorted by creation date. To avoid slow queries due to joining across
* multiple node and field tables with various conditions and order by criteria,
* we maintain a denormalized table with all relationships between terms,
* published nodes and common sort criteria such as sticky and created.
* This is used as a lookup table by taxonomy_select_nodes(). When using other
* field storage engines or alternative methods of denormalizing this data
* you should set the variable 'taxonomy_maintain_index_table' to FALSE
* to avoid unnecessary writes in SQL.
* @{
*/
/**
* Implement hook_field_insert().
*/
function taxonomy_field_insert($obj_type, $object, $field, $instance, $langcode, &$items) {
// We maintain a denormalized table of term/node relationships, containing
// only data for current, published nodes.
if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $obj_type == 'node' && $object->status) {
$query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created', ));
foreach ($items as $item) {
$query->values(array(
'nid' => $object->nid,
'tid' => $item['value'],
'sticky' => $object->sticky,
'created' => $object->created,
));
}
$query->execute();
}
}
/**
* Implement hook_field_update().
*/
function taxonomy_field_update($obj_type, $object, $field, $instance, $langcode, &$items) {
if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $obj_type = 'node') {
$first_call = &drupal_static(__FUNCTION__, array());
// We don't maintain data for old revisions, so clear all previous values
// from the table. Since this hook runs once per field, per object, make
// sure we only wipe values once.
if (!isset($first_call[$object->nid])) {
$first_call[$object->nid] = FALSE;
db_delete('taxonomy_index')->condition('nid', $object->nid)->execute();
}
// Only save data to the table if the node is published.
if ($object->status) {
$query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created'));
foreach ($items as $item) {
$query->values(array(
'nid' => $object->nid,
'tid' => $item['value'],
'sticky' => $object->sticky,
'created' => $object->created,
));
}
$query->execute();
}
}
}
/**
* Implement hook_node_delete().
*/
function taxonomy_node_delete($node) {
if (variable_get('taxonomy_maintain_index_table', TRUE)) {
// Clean up the {taxonomy_index} table when nodes are deleted.
db_delete('taxonomy_index')->condition('nid', $node->nid)->execute();
}
}
/**
* Implement hook_taxonomy_term_delete().
*/
function taxonomy_taxonomy_term_delete($term) {
if (variable_get('taxonomy_maintain_index_table', TRUE)) {
// Clean up the {taxonomy_index} table when terms are deleted.
db_delete('taxonomy_index')->condition('tid', $term->tid)->execute();
}
}
/**
* @} End of "defgroup taxonomy indexing"
*/