1466 lines
46 KiB
Plaintext
1466 lines
46 KiB
Plaintext
<?php
|
|
// $Id$
|
|
|
|
/**
|
|
* @file
|
|
* Enables the organization of content into categories.
|
|
*/
|
|
|
|
/**
|
|
* Implements hook_help().
|
|
*/
|
|
function taxonomy_help($path, $arg) {
|
|
switch ($path) {
|
|
case 'admin/help#taxonomy':
|
|
$output = '';
|
|
$output .= '<h3>' . t('About') . '</h3>';
|
|
$output .= '<p>' . t('The Taxonomy module allows you to classify the content of your website. To classify content, you define <em>vocabularies</em> that contain related <em>terms</em>, and then assign the vocabularies to content types. For more information, see the online handbook entry for the <a href="@taxonomy">Taxonomy module</a>.', array('@taxonomy' => 'http://drupal.org/handbook/modules/taxonomy/')) . '</p>';
|
|
$output .= '<h3>' . t('Uses') . '</h3>';
|
|
$output .= '<dl>';
|
|
$output .= '<dt>' . t('Creating vocabularies') . '</dt>';
|
|
$output .= '<dd>' . t('Users with sufficient <a href="@perm">permissions</a> can create <em>vocabularies</em> and <em>terms</em> through the <a href="@taxo">Taxonomy page</a>. The page listing the terms provides a drag-and-drop interface for controlling the order of the terms and sub-terms within a vocabulary, in a hierarchical fashion. A <em>controlled vocabulary</em> classifying music by genre with terms and sub-terms could look as follows:', array('@taxo' => url('admin/structure/taxonomy'), '@perm' => url('admin/config/people/permissions', array('fragment'=>'module-taxonomy'))));
|
|
$output .= '<ul><li>' . t ('<em>vocabulary</em>: Music'); '</li>';
|
|
$output .= '<ul><li>' . t('<em>term</em>: Jazz') . '</li>';
|
|
$output .= '<ul><li>' . t('<em>sub-term</em>: Swing') . '</li>';
|
|
$output .= '<li>' . t('<em>sub-term</em>: Fusion') . '</li></ul></ul>';
|
|
$output .= '<ul><li>' . t('<em>term</em>: Rock') . '</li>';
|
|
$output .= '<ul><li>' . t('<em>sub-term</em>: Country rock') . '</li>';
|
|
$output .= '<li>' . t('<em>sub-term</em>: Hard rock') . '</li></ul></ul></ul>';
|
|
$output .= t('You can assign a sub-term to multiple parent terms. For example, <em>fusion</em> can be assigned to both <em>rock</em> and <em>jazz</em>.') . '</dd>';
|
|
$output .= '<dd>' . t('Terms in a <em>free-tagging vocabulary</em> can be built gradually as you create or edit content. This is often done used for blogs or photo management applications.') . '</dd>';
|
|
$output .= '<dt>' . t('Assigning vocabularies to content types') . '</dt>';
|
|
$output .= '<dd>' . t('Before you can use a new vocabulary to classify your content, a new Taxonomy term field must be added to a <a href="@ctedit">content type</a> on its <em>manage fields</em> page. When adding a taxonomy field, you choose a <em>widget</em> to use to enter the taxonomy information on the conent editing page: a select list, checkboxes, radio buttons, or an auto-complete field (to build a free-tagging vocabulary). After choosing the field type and widget, on the subsequent <em>field settings</em> page you can choose the desired vocabulary, whether one or multiple terms can be chosen from the vocabulary, and other settings. The same vocabulary can be added to multiple content types, by using the "Add existing field" section on the manage fields page.', array('@ctedit' => url('admin/structure/types'))) . '</dd>';
|
|
$output .= '<dt>' . t('Classifying content') . '</dt>';
|
|
$output .= '<dd>' . t('After the vocabulary is assigned to the content type, you can start classifying content. The field with terms will appear on the content editing screen when you edit or <a href="@addnode">create content</a>.', array('@addnode' => url('node/add'))) . '</dd>';
|
|
$output .= '<dt>' . t('Viewing listings and RSS feeds by term') . '</dt>';
|
|
$output .= '<dd>' . t("Each taxonomy term automatically provides a page listing content that has its classification, and a corresponding RSS feed. For example, if the taxonomy term <em>country rock</em> has the ID 123 (you can see this by looking at the URL when hovering on the linked term, which you can click to navigate to the listing page), then you will find this list at the path <em>taxonomy/term/123</em>. The RSS feed will use the path <em>taxonomy/term/123/feed</em> (the RSS icon for this term's listing will automatically display in your browser's address bar when viewing the listing page).") . '</dd>';
|
|
$output .= '<dt>' . t('Extending Taxonomy module') . '</dt>';
|
|
$output .= '<dd>' . t('There are <a href="@taxcontrib">many contributed modules</a> that extend the behavior of the Taxonomy module for both display and organization of terms.', array('@taxcontrib' => 'http://drupal.org/project/modules?filters=tid:71&solrsort=sis_project_release_usage%20desc'));
|
|
$output .= '</dl>';
|
|
return $output;
|
|
case 'admin/structure/taxonomy':
|
|
$output = '<p>' . t('Configure the vocabularies and terms for your site.') . '</p>';
|
|
return $output;
|
|
case 'admin/structure/taxonomy/%':
|
|
$vocabulary = taxonomy_vocabulary_load($arg[3]);
|
|
switch ($vocabulary->hierarchy) {
|
|
case 0:
|
|
return '<p>' . t('You can reorganize the terms in %capital_name using their drag and drop handles, and group terms under a parent term by sliding them under and to the right of the parent.', array('%capital_name' => drupal_ucfirst($vocabulary->name), '%name' => $vocabulary->name)) . '</p>';
|
|
case 1:
|
|
return '<p>' . t('%capital_name contains terms grouped with parent terms. You can reorganize the terms in %capital_name using their drag and drop handles.', array('%capital_name' => drupal_ucfirst($vocabulary->name), '%name' => $vocabulary->name)) . '</p>';
|
|
case 2:
|
|
return '<p>' . t('%capital_name contains terms with multiple parents. Drag and drop of terms with multiple parents 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>';
|
|
}
|
|
case 'admin/structure/taxonomy/add':
|
|
return '<p>' . t('To create a new taxonomy vocabulary, type in a name and a description.') . '</p>';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_permission().
|
|
*/
|
|
function taxonomy_permission() {
|
|
$permissions = array(
|
|
'administer taxonomy' => array(
|
|
'title' => t('Administer vocabularies and terms'),
|
|
),
|
|
);
|
|
foreach (taxonomy_get_vocabularies() as $vocabulary) {
|
|
$permissions += array(
|
|
'edit terms in ' . $vocabulary->vid => array(
|
|
'title' => t('Edit terms in %vocabulary', array('%vocabulary' => $vocabulary->name)),
|
|
),
|
|
);
|
|
$permissions += array(
|
|
'delete terms in ' . $vocabulary->vid => array(
|
|
'title' => t('Delete terms from %vocabulary', array('%vocabulary' => $vocabulary->name)),
|
|
),
|
|
);
|
|
}
|
|
return $permissions;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_info().
|
|
*/
|
|
function taxonomy_entity_info() {
|
|
$return = array(
|
|
'taxonomy_term' => array(
|
|
'label' => t('Taxonomy term'),
|
|
'controller class' => 'TaxonomyTermController',
|
|
'base table' => 'taxonomy_term_data',
|
|
'fieldable' => TRUE,
|
|
'object keys' => array(
|
|
'id' => 'tid',
|
|
'bundle' => 'vocabulary_machine_name',
|
|
),
|
|
'bundle keys' => array(
|
|
'bundle' => 'machine_name',
|
|
),
|
|
'bundles' => array(),
|
|
),
|
|
);
|
|
foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocabulary) {
|
|
$return['taxonomy_term']['bundles'][$machine_name] = array(
|
|
'label' => $vocabulary->name,
|
|
'admin' => array(
|
|
'path' => 'admin/structure/taxonomy/%taxonomy_vocabulary',
|
|
'real path' => 'admin/structure/taxonomy/' . $vocabulary->vid,
|
|
'bundle argument' => 3,
|
|
'access arguments' => array('administer taxonomy'),
|
|
),
|
|
);
|
|
}
|
|
$return['taxonomy_vocabulary'] = array(
|
|
'label' => t('Taxonomy vocabulary'),
|
|
'controller class' => 'TaxonomyVocabularyController',
|
|
'base table' => 'taxonomy_vocabulary',
|
|
'object keys' => array(
|
|
'id' => 'vid',
|
|
),
|
|
'fieldable' => FALSE,
|
|
);
|
|
|
|
return $return;
|
|
}
|
|
|
|
/**
|
|
* 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 $tid
|
|
* The term ID.
|
|
* @param $pager
|
|
* Boolean to indicate whether a pager should be used.
|
|
* @param $limit
|
|
* Integer. The maximum number of nodes to find.
|
|
* Set to FALSE for no limit.
|
|
* @order
|
|
* An array of fields and directions.
|
|
*
|
|
* @return
|
|
* An array of nids matching the query.
|
|
*/
|
|
function taxonomy_select_nodes($tid, $pager = TRUE, $limit = FALSE, $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');
|
|
if ($limit !== FALSE) {
|
|
$query = $query->limit($limit);
|
|
}
|
|
$query->setCountQuery($count_query);
|
|
}
|
|
else {
|
|
if ($limit !== FALSE) {
|
|
$query->range(0, $limit);
|
|
}
|
|
}
|
|
$query->condition('tid', $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();
|
|
}
|
|
|
|
/**
|
|
* Implements 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;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_theme().
|
|
*/
|
|
function taxonomy_theme() {
|
|
return array(
|
|
'taxonomy_overview_vocabularies' => array(
|
|
'render element' => 'form',
|
|
),
|
|
'taxonomy_overview_terms' => array(
|
|
'render element' => 'form',
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_menu().
|
|
*/
|
|
function taxonomy_menu() {
|
|
$items['admin/structure/taxonomy'] = array(
|
|
'title' => 'Taxonomy',
|
|
'description' => 'Manage tagging, categorization, and classification of your content.',
|
|
'page callback' => 'drupal_get_form',
|
|
'page arguments' => array('taxonomy_overview_vocabularies'),
|
|
'access arguments' => array('administer taxonomy'),
|
|
'file' => 'taxonomy.admin.inc',
|
|
);
|
|
$items['admin/structure/taxonomy/list'] = array(
|
|
'title' => 'List',
|
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
|
'weight' => -10,
|
|
);
|
|
$items['admin/structure/taxonomy/add'] = array(
|
|
'title' => 'Add vocabulary',
|
|
'page callback' => 'drupal_get_form',
|
|
'page arguments' => array('taxonomy_form_vocabulary'),
|
|
'access arguments' => array('administer taxonomy'),
|
|
'type' => MENU_LOCAL_ACTION,
|
|
'file' => 'taxonomy.admin.inc',
|
|
);
|
|
|
|
$items['taxonomy/term/%taxonomy_term'] = array(
|
|
'title' => 'Taxonomy term',
|
|
'title callback' => 'taxonomy_term_title',
|
|
'title arguments' => array(2),
|
|
'page callback' => 'taxonomy_term_page',
|
|
'page arguments' => array(2),
|
|
'access arguments' => array('access content'),
|
|
'type' => MENU_CALLBACK,
|
|
'file' => 'taxonomy.pages.inc',
|
|
);
|
|
$items['taxonomy/term/%taxonomy_term/view'] = array(
|
|
'title' => 'View',
|
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
|
);
|
|
$items['taxonomy/term/%taxonomy_term/edit'] = array(
|
|
'title' => 'Edit term',
|
|
'title callback' => 'taxonomy_term_title',
|
|
'title arguments' => array(2),
|
|
'page callback' => 'drupal_get_form',
|
|
'page arguments' => array('taxonomy_form_term', 2),
|
|
'access callback' => 'taxonomy_term_edit_access',
|
|
'access arguments' => array(2),
|
|
'type' => MENU_LOCAL_TASK,
|
|
'weight' => 10,
|
|
'file' => 'taxonomy.admin.inc',
|
|
);
|
|
$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,
|
|
'file' => 'taxonomy.pages.inc',
|
|
);
|
|
$items['taxonomy/autocomplete'] = array(
|
|
'title' => 'Autocomplete taxonomy',
|
|
'page callback' => 'taxonomy_autocomplete',
|
|
'access arguments' => array('access content'),
|
|
'type' => MENU_CALLBACK,
|
|
'file' => 'taxonomy.pages.inc',
|
|
);
|
|
|
|
$items['admin/structure/taxonomy/%taxonomy_vocabulary'] = array(
|
|
'title callback' => 'taxonomy_admin_vocabulary_title_callback',
|
|
'title arguments' => array(3),
|
|
'page callback' => 'drupal_get_form',
|
|
'page arguments' => array('taxonomy_overview_terms', 3),
|
|
'access arguments' => array('administer taxonomy'),
|
|
'file' => 'taxonomy.admin.inc',
|
|
);
|
|
$items['admin/structure/taxonomy/%taxonomy_vocabulary/list'] = array(
|
|
'title' => 'List',
|
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
|
'weight' => -20,
|
|
);
|
|
$items['admin/structure/taxonomy/%taxonomy_vocabulary/edit'] = array(
|
|
'title' => 'Edit',
|
|
'page callback' => 'drupal_get_form',
|
|
'page arguments' => array('taxonomy_form_vocabulary', 3),
|
|
'access arguments' => array('administer taxonomy'),
|
|
'type' => MENU_LOCAL_TASK,
|
|
'weight' => -10,
|
|
'file' => 'taxonomy.admin.inc',
|
|
);
|
|
|
|
$items['admin/structure/taxonomy/%taxonomy_vocabulary/add'] = array(
|
|
'title' => 'Add term',
|
|
'page callback' => 'drupal_get_form',
|
|
'page arguments' => array('taxonomy_form_term', array(), 3),
|
|
'access arguments' => array('administer taxonomy'),
|
|
'type' => MENU_LOCAL_ACTION,
|
|
'file' => 'taxonomy.admin.inc',
|
|
);
|
|
|
|
return $items;
|
|
}
|
|
|
|
/**
|
|
* Return edit access for a given term.
|
|
*/
|
|
function taxonomy_term_edit_access($term) {
|
|
return user_access("edit terms in $term->vid") || user_access('administer taxonomy');
|
|
}
|
|
|
|
/**
|
|
* Return the vocabulary name given the vocabulary object.
|
|
*/
|
|
function taxonomy_admin_vocabulary_title_callback($vocabulary) {
|
|
return check_plain($vocabulary->name);
|
|
}
|
|
|
|
/**
|
|
* Save a vocabulary given a vocabulary object.
|
|
*/
|
|
function taxonomy_vocabulary_save($vocabulary) {
|
|
|
|
if (!empty($vocabulary->name)) {
|
|
// Prevent leading and trailing spaces in vocabulary names.
|
|
$vocabulary->name = trim($vocabulary->name);
|
|
}
|
|
|
|
if (!isset($vocabulary->module)) {
|
|
$vocabulary->module = 'taxonomy';
|
|
}
|
|
|
|
if (!empty($vocabulary->vid) && !empty($vocabulary->name)) {
|
|
$status = drupal_write_record('taxonomy_vocabulary', $vocabulary, 'vid');
|
|
module_invoke_all('taxonomy_vocabulary_update', $vocabulary);
|
|
}
|
|
elseif (empty($vocabulary->vid)) {
|
|
$status = drupal_write_record('taxonomy_vocabulary', $vocabulary);
|
|
field_attach_create_bundle('taxonomy_term', $vocabulary->machine_name);
|
|
taxonomy_vocabulary_create_field($vocabulary);
|
|
module_invoke_all('taxonomy_vocabulary_insert', $vocabulary);
|
|
}
|
|
|
|
cache_clear_all();
|
|
entity_get_controller('taxonomy_vocabulary')->resetCache();
|
|
|
|
return $status;
|
|
}
|
|
|
|
/**
|
|
* Delete a vocabulary.
|
|
*
|
|
* @param $vid
|
|
* A vocabulary ID.
|
|
* @return
|
|
* Constant indicating items were deleted.
|
|
*/
|
|
function taxonomy_vocabulary_delete($vid) {
|
|
$vocabulary = (array) taxonomy_vocabulary_load($vid);
|
|
|
|
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);
|
|
}
|
|
|
|
field_attach_delete_bundle('taxonomy_term', $vocabulary['machine_name']);
|
|
module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
|
|
|
|
cache_clear_all();
|
|
entity_get_controller('taxonomy_vocabulary')->resetCache();
|
|
|
|
return SAVED_DELETED;
|
|
}
|
|
|
|
/**
|
|
* Dynamically check and update the hierarchy flag of a vocabulary.
|
|
*
|
|
* 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
|
|
* hierarchy of 2.
|
|
*
|
|
* @param $vocabulary
|
|
* A vocabulary object.
|
|
* @param $changed_term
|
|
* An array of the term structure that was updated.
|
|
*/
|
|
function taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term) {
|
|
$tree = taxonomy_get_tree($vocabulary->vid);
|
|
$hierarchy = 0;
|
|
foreach ($tree as $term) {
|
|
// Update the changed term with the new parent value before comparison.
|
|
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;
|
|
}
|
|
}
|
|
if ($hierarchy != $vocabulary->hierarchy) {
|
|
$vocabulary->hierarchy = $hierarchy;
|
|
taxonomy_vocabulary_save($vocabulary);
|
|
}
|
|
|
|
return $hierarchy;
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* Save a term object to the database.
|
|
*
|
|
* @param $term
|
|
* A term object.
|
|
* @return
|
|
* Status constant indicating if term was inserted or updated.
|
|
*/
|
|
function taxonomy_term_save($term) {
|
|
if ($term->name) {
|
|
// Prevent leading and trailing spaces in term names.
|
|
$term->name = trim($term->name);
|
|
}
|
|
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);
|
|
|
|
if (!empty($term->tid) && $term->name) {
|
|
$status = drupal_write_record('taxonomy_term_data', $term, 'tid');
|
|
field_attach_update('taxonomy_term', $term);
|
|
module_invoke_all('taxonomy_term_update', $term);
|
|
}
|
|
else {
|
|
$status = drupal_write_record('taxonomy_term_data', $term);
|
|
_taxonomy_clean_field_cache($term);
|
|
field_attach_insert('taxonomy_term', $term);
|
|
module_invoke_all('taxonomy_term_insert', $term);
|
|
}
|
|
|
|
db_delete('taxonomy_term_hierarchy')
|
|
->condition('tid', $term->tid)
|
|
->execute();
|
|
|
|
if (!isset($term->parent) || empty($term->parent)) {
|
|
$term->parent = array(0);
|
|
}
|
|
if (!is_array($term->parent)) {
|
|
$term->parent = array($term->parent);
|
|
}
|
|
$query = db_insert('taxonomy_term_hierarchy')
|
|
->fields(array('tid', 'parent'));
|
|
if (is_array($term->parent)) {
|
|
foreach ($term->parent as $parent) {
|
|
if (is_array($parent)) {
|
|
foreach ($parent as $tid) {
|
|
$query->values(array(
|
|
'tid' => $term->tid,
|
|
'parent' => $tid
|
|
));
|
|
}
|
|
}
|
|
else {
|
|
$query->values(array(
|
|
'tid' => $term->tid,
|
|
'parent' => $parent
|
|
));
|
|
}
|
|
}
|
|
}
|
|
$query->execute();
|
|
|
|
cache_clear_all();
|
|
taxonomy_terms_static_reset();
|
|
|
|
return $status;
|
|
}
|
|
|
|
/**
|
|
* Delete a term.
|
|
*
|
|
* @param $tid
|
|
* The term ID.
|
|
* @return
|
|
* Status constant indicating deletion.
|
|
*/
|
|
function taxonomy_term_delete($tid) {
|
|
$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;
|
|
}
|
|
}
|
|
}
|
|
|
|
$term = taxonomy_term_load($tid);
|
|
|
|
db_delete('taxonomy_term_data')
|
|
->condition('tid', $tid)
|
|
->execute();
|
|
db_delete('taxonomy_term_hierarchy')
|
|
->condition('tid', $tid)
|
|
->execute();
|
|
|
|
field_attach_delete('taxonomy_term', $term);
|
|
_taxonomy_clean_field_cache($term);
|
|
module_invoke_all('taxonomy_term_delete', $term);
|
|
}
|
|
|
|
$tids = $orphans;
|
|
}
|
|
|
|
cache_clear_all();
|
|
taxonomy_terms_static_reset();
|
|
|
|
return SAVED_DELETED;
|
|
}
|
|
|
|
/**
|
|
* 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');
|
|
entity_get_controller('taxonomy_term')->resetCache();
|
|
}
|
|
|
|
/**
|
|
* Generate a set of options for selecting a term from all vocabularies.
|
|
*/
|
|
function taxonomy_form_all() {
|
|
$vocabularies = taxonomy_get_vocabularies();
|
|
$options = array();
|
|
foreach ($vocabularies as $vid => $vocabulary) {
|
|
$tree = taxonomy_get_tree($vid);
|
|
if ($tree && (count($tree) > 0)) {
|
|
$options[$vocabulary->name] = array();
|
|
foreach ($tree as $term) {
|
|
$options[$vocabulary->name][$term->tid] = str_repeat('-', $term->depth) . $term->name;
|
|
}
|
|
}
|
|
}
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* Return an array of all vocabulary objects.
|
|
*
|
|
* @param $type
|
|
* If set, return only those vocabularies associated with this node type.
|
|
*/
|
|
function taxonomy_get_vocabularies() {
|
|
return taxonomy_vocabulary_load_multiple(FALSE, array());
|
|
}
|
|
|
|
/**
|
|
* Get names for all taxonomy vocabularies.
|
|
*
|
|
* @return
|
|
* An array of vocabulary ids, names, machine names, keyed by machine name.
|
|
*/
|
|
function taxonomy_vocabulary_get_names() {
|
|
$names = db_query('SELECT name, machine_name, vid FROM {taxonomy_vocabulary}')->fetchAllAssoc('machine_name');
|
|
return $names;
|
|
}
|
|
|
|
/**
|
|
* Find all parents of a given term ID.
|
|
*/
|
|
function taxonomy_get_parents($tid, $key = 'tid') {
|
|
if ($tid) {
|
|
$query = db_select('taxonomy_term_data', 't');
|
|
$query->join('taxonomy_term_hierarchy', 'h', 'h.parent = t.tid');
|
|
$result = $query
|
|
->addTag('translatable')
|
|
->addTag('term_access')
|
|
->fields('t')
|
|
->condition('h.tid', $tid)
|
|
->orderBy('weight')
|
|
->orderBy('name')
|
|
->execute();
|
|
$parents = array();
|
|
foreach ($result as $parent) {
|
|
$parents[$parent->$key] = $parent;
|
|
}
|
|
return $parents;
|
|
}
|
|
else {
|
|
return array();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Find all ancestors of a given term ID.
|
|
*/
|
|
function taxonomy_get_parents_all($tid) {
|
|
$cache = &drupal_static(__FUNCTION__, array());
|
|
|
|
if (isset($cache[$tid])) {
|
|
return $cache[$tid];
|
|
}
|
|
|
|
$parents = array();
|
|
if ($term = taxonomy_term_load($tid)) {
|
|
$parents[] = $term;
|
|
$n = 0;
|
|
while ($parent = taxonomy_get_parents($parents[$n]->tid)) {
|
|
$parents = array_merge($parents, $parent);
|
|
$n++;
|
|
}
|
|
}
|
|
|
|
$cache[$tid] = $parents;
|
|
|
|
return $parents;
|
|
}
|
|
|
|
/**
|
|
* Find all children of a term ID.
|
|
*/
|
|
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
|
|
$query = db_select('taxonomy_term_data', 't');
|
|
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
|
|
$query
|
|
->addTag('translatable')
|
|
->addTag('term_access')
|
|
->fields('t')
|
|
->condition('parent', $tid)
|
|
->orderBy('weight')
|
|
->orderBy('name');
|
|
if ($vid) {
|
|
$query->condition('t.vid', $vid);
|
|
}
|
|
$result = $query->execute();
|
|
|
|
$children = array();
|
|
foreach ($result as $term) {
|
|
$children[$term->$key] = $term;
|
|
}
|
|
return $children;
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
* @param $max_depth
|
|
* The number of levels of the tree to return. Leave NULL to return all levels.
|
|
* @param $depth
|
|
* Internal use only.
|
|
*
|
|
* @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.
|
|
* Results are statically cached.
|
|
*/
|
|
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());
|
|
|
|
$depth++;
|
|
|
|
// 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();
|
|
$parents[$vid] = array();
|
|
$terms[$vid] = array();
|
|
|
|
$query = db_select('taxonomy_term_data', 't');
|
|
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
|
|
$result = $query
|
|
->addTag('translatable')
|
|
->addTag('term_access')
|
|
->fields('t')
|
|
->fields('h', array('parent'))
|
|
->condition('t.vid', $vid)
|
|
->orderBy('weight')
|
|
->orderBy('name')
|
|
->execute();
|
|
foreach ($result as $term) {
|
|
$children[$vid][$term->parent][] = $term->tid;
|
|
$parents[$vid][$term->tid][] = $term->parent;
|
|
$terms[$vid][$term->tid] = $term;
|
|
}
|
|
}
|
|
|
|
$max_depth = (is_null($max_depth)) ? count($children[$vid]) : $max_depth;
|
|
$tree = array();
|
|
if ($max_depth > $depth && !empty($children[$vid][$parent])) {
|
|
foreach ($children[$vid][$parent] as $child) {
|
|
$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));
|
|
}
|
|
}
|
|
}
|
|
|
|
return $tree;
|
|
}
|
|
|
|
/**
|
|
* Try to map a string to an existing term, as for glossary use.
|
|
*
|
|
* 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.
|
|
*/
|
|
function taxonomy_get_term_by_name($name) {
|
|
return taxonomy_term_load_multiple(array(), array('name' => trim($name)));
|
|
}
|
|
|
|
/**
|
|
* 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']);
|
|
}
|
|
return parent::load($ids, $conditions);
|
|
}
|
|
|
|
protected function buildQuery() {
|
|
parent::buildQuery();
|
|
$this->query->addTag('translatable');
|
|
$this->query->addTag('term_access');
|
|
// 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';
|
|
}
|
|
}
|
|
}
|
|
// 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, $conditions = array()) {
|
|
$terms = parent::cacheGet($ids, $conditions);
|
|
// 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]);
|
|
}
|
|
}
|
|
return $terms;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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->addTag('translatable');
|
|
$this->query->orderBy('base.weight');
|
|
$this->query->orderBy('base.name');
|
|
}
|
|
|
|
protected function attachLoad(&$records) {
|
|
foreach ($records as $record) {
|
|
// If no node types are associated with a vocabulary, the LEFT JOIN will
|
|
// return a NULL value for type.
|
|
$queried_vocabularies[$record->vid] = $record;
|
|
}
|
|
$records = $queried_vocabularies;
|
|
parent::attachLoad($records);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*
|
|
* @see entity_load()
|
|
*
|
|
* @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.
|
|
*/
|
|
function taxonomy_term_load_multiple($tids = array(), $conditions = array()) {
|
|
return entity_load('taxonomy_term', $tids, $conditions);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
$vocabularies = taxonomy_vocabulary_load_multiple(array($vid));
|
|
return reset($vocabularies);
|
|
}
|
|
|
|
/**
|
|
* Return the term object matching a term ID.
|
|
*
|
|
* @param $tid
|
|
* A term's ID
|
|
*
|
|
* @return
|
|
* A term object. Results are statically cached.
|
|
*/
|
|
function taxonomy_term_load($tid) {
|
|
if (!is_numeric($tid)) {
|
|
return FALSE;
|
|
}
|
|
$term = taxonomy_term_load_multiple(array($tid), array());
|
|
return $term ? $term[$tid] : FALSE;
|
|
}
|
|
|
|
/**
|
|
* Helper function for array_map purposes.
|
|
*/
|
|
function _taxonomy_get_tid_from_term($term) {
|
|
return $term->tid;
|
|
}
|
|
|
|
/**
|
|
* Implodes 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.
|
|
if (is_null($vid) || $tag->vid == $vid) {
|
|
|
|
// Commas and quotes in tag names are special cases, so encode 'em.
|
|
if (strpos($tag->name, ',') !== FALSE || strpos($tag->name, '"') !== FALSE) {
|
|
$tag->name = '"' . str_replace('"', '""', $tag->name) . '"';
|
|
}
|
|
|
|
$typed_tags[] = $tag->name;
|
|
}
|
|
}
|
|
return implode(', ', $typed_tags);
|
|
}
|
|
|
|
/**
|
|
* Implements 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',
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_field_widget_info().
|
|
*/
|
|
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,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements 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';
|
|
}
|
|
|
|
/**
|
|
* Implements hook_options_list().
|
|
*/
|
|
function taxonomy_options_list($field) {
|
|
return taxonomy_allowed_values($field);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_field_schema().
|
|
*/
|
|
function taxonomy_field_schema($field) {
|
|
return array(
|
|
'columns' => array(
|
|
'tid' => array(
|
|
'type' => 'int',
|
|
'unsigned' => TRUE,
|
|
'not null' => FALSE,
|
|
),
|
|
),
|
|
'indexes' => array(
|
|
'tid' => array('tid'),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_field_validate().
|
|
*
|
|
* Possible error codes:
|
|
* - 'taxonomy_term_illegal_value': The value is not part of the list of allowed values.
|
|
*/
|
|
function taxonomy_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) {
|
|
$allowed_values = taxonomy_allowed_values($field);
|
|
$widget = field_info_widget_types($instance['widget']['type']);
|
|
|
|
foreach ($items as $delta => $item) {
|
|
if (!empty($item['tid'])) {
|
|
if (!isset($allowed_values[$item['tid']])) {
|
|
$errors[$field['field_name']][$langcode][$delta][] = array(
|
|
'error' => 'taxonomy_term_illegal_value',
|
|
'message' => t('%name: illegal value.', array('%name' => t($instance['label']))),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_field_is_empty().
|
|
*/
|
|
function taxonomy_field_is_empty($item, $field) {
|
|
if (!is_array($item) || (empty($item['tid']) && (string) $item['tid'] !== '0')) {
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_field_formatter_info().
|
|
*/
|
|
function taxonomy_field_formatter_info() {
|
|
return array(
|
|
'taxonomy_term_link' => array(
|
|
'label' => t('Link'),
|
|
'field types' => array('taxonomy_term'),
|
|
),
|
|
'taxonomy_term_plain' => array(
|
|
'label' => t('Plain text'),
|
|
'field types' => array('taxonomy_term'),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_field_formatter().
|
|
*/
|
|
function taxonomy_field_formatter($object_type, $object, $field, $instance, $langcode, $items, $display) {
|
|
$element = array();
|
|
|
|
switch ($display['type']) {
|
|
case 'taxonomy_term_link':
|
|
foreach ($items as $delta => $item) {
|
|
// @todo Remove this when "node_build() does not call
|
|
// field_attach_prepare_view()" bug is fixed.
|
|
// See http://drupal.org/node/493314.
|
|
if (!isset($item['taxonomy_term'])) {
|
|
$item['taxonomy_term'] = taxonomy_term_load($item['tid']);
|
|
}
|
|
$term = $item['taxonomy_term'];
|
|
$element[$delta] = array(
|
|
'#type' => 'link',
|
|
'#title' => $term->name,
|
|
'#href' => 'taxonomy/term/' . $term->tid,
|
|
);
|
|
}
|
|
break;
|
|
|
|
case 'taxonomy_term_plain':
|
|
foreach ($items as $delta => $item) {
|
|
$term = $item['taxonomy_term'];
|
|
$element[$delta] = array(
|
|
'#markup' => check_plain($term->name),
|
|
);
|
|
}
|
|
break;
|
|
}
|
|
|
|
return $element;
|
|
}
|
|
|
|
/**
|
|
* Returns the set of valid terms for a taxonomy field.
|
|
*
|
|
* @param $field
|
|
* The field definition.
|
|
* @return
|
|
* The array of valid terms for this field, keyed by term id.
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_field_formatter_prepare_view().
|
|
*
|
|
* This preloads all taxonomy terms for multiple loaded objects at once and
|
|
* unsets values for invalid terms that do not exist.
|
|
*/
|
|
function taxonomy_field_formatter_prepare_view($obj_type, $objects, $field, $instances, $langcode, &$items, $displays) {
|
|
$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['tid']] = $item['tid'];
|
|
}
|
|
}
|
|
if ($tids) {
|
|
$terms = taxonomy_term_load_multiple($tids);
|
|
|
|
// 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['tid']])) {
|
|
// Replace the instance value with the term data.
|
|
$items[$id][$delta]['taxonomy_term'] = $terms[$item['tid']];
|
|
}
|
|
// 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 (entity_get_info() as $obj_type => $info) {
|
|
if (isset($info['cacheable']) && !$info['cacheable']) {
|
|
$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'];
|
|
}
|
|
|
|
// Check this term's vocabulary against those used for the field's options.
|
|
if (in_array($term->vid, $vids)) {
|
|
$conditions = array(array('tid', $term->tid));
|
|
if ($obj_types) {
|
|
$conditions[] = array('type', $obj_types, 'NOT IN');
|
|
}
|
|
$results = field_attach_query($field['id'], $conditions, array('limit' => FIELD_QUERY_NO_LIMIT));
|
|
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');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_field_widget().
|
|
*/
|
|
function taxonomy_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
|
|
$tags = array();
|
|
foreach ($items as $item) {
|
|
$tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
|
|
}
|
|
|
|
$element += array(
|
|
'#type' => 'textfield',
|
|
'#default_value' => taxonomy_implode_tags($tags),
|
|
'#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'] . '/' . $field['field_name'],
|
|
'#size' => $instance['widget']['settings']['size'],
|
|
'#element_validate' => array('taxonomy_autocomplete_validate'),
|
|
);
|
|
|
|
return $element;
|
|
}
|
|
|
|
/**
|
|
* Form element validate handler for taxonomy term autocomplete element.
|
|
*/
|
|
function taxonomy_autocomplete_validate($element, &$form_state) {
|
|
// Autocomplete widgets do not send their tids in the form, so we must detect
|
|
// them here and process them independently.
|
|
if ($tags = $element['#value']) {
|
|
// Collect candidate vocabularies.
|
|
$field = $form_state['complete form']['#fields'][$element['#field_name']]['field'];
|
|
$vids = array();
|
|
foreach ($field['settings']['allowed_values'] as $tree) {
|
|
$vids[] = $tree['vid'];
|
|
}
|
|
|
|
// Translate term names into actual terms.
|
|
$typed_terms = drupal_explode_tags($tags);
|
|
$values = array();
|
|
foreach ($typed_terms as $typed_term) {
|
|
// See if the term exists in the chosen vocabulary and return the tid;
|
|
// otherwise, create a new term.
|
|
if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => $vids))) {
|
|
$term = array_pop($possibilities);
|
|
}
|
|
else {
|
|
$vocabulary = taxonomy_vocabulary_load($vids[0]);
|
|
$term = (object) array(
|
|
'vid' => $vids[0],
|
|
'name' => $typed_term,
|
|
'vocabulary_machine_name' => $vocabulary->machine_name,
|
|
);
|
|
taxonomy_term_save($term);
|
|
}
|
|
$values[] = $term->tid;
|
|
}
|
|
$value = options_array_transpose(array('tid' => $values));
|
|
}
|
|
else {
|
|
$value = array();
|
|
}
|
|
|
|
form_set_value($element, $value, $form_state);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_field_widget_error().
|
|
*/
|
|
function taxonomy_field_widget_error($element, $error) {
|
|
form_error($element, $error['message']);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_field_settings_form().
|
|
*/
|
|
function taxonomy_field_settings_form($field, $instance, $has_data) {
|
|
// 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.'),
|
|
'#disabled' => $has_data,
|
|
);
|
|
$form['allowed_values'][$delta]['parent'] = array(
|
|
'#type' => 'value',
|
|
'#value' => $tree['parent'],
|
|
);
|
|
}
|
|
|
|
return $form;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_rdf_mapping().
|
|
*
|
|
* @return array
|
|
* The rdf mapping for vocabularies and terms.
|
|
*/
|
|
function taxonomy_rdf_mapping() {
|
|
return array(
|
|
array(
|
|
'type' => 'taxonomy_term',
|
|
'bundle' => RDF_DEFAULT_BUNDLE,
|
|
'mapping' => array(
|
|
'rdftype' => array('skos:Concept'),
|
|
'name' => array(
|
|
'predicates' => array('skos:prefLabel'),
|
|
),
|
|
'description' => array(
|
|
'predicates' => array('skos:definition'),
|
|
),
|
|
// The vocabulary this term belongs to.
|
|
'vid' => array(
|
|
'predicates' => array('skos:member'),
|
|
),
|
|
),
|
|
),
|
|
array(
|
|
'type' => 'taxonomy_vocabulary',
|
|
'bundle' => RDF_DEFAULT_BUNDLE,
|
|
'mapping' => array(
|
|
'rdftype' => array('skos:Collection'),
|
|
'name' => array(
|
|
'predicates' => array('rdfs:label'),
|
|
),
|
|
'description' => array(
|
|
'predicates' => array('rdfs:comment'),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @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.
|
|
* @{
|
|
*/
|
|
|
|
/**
|
|
* Implements 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['tid'],
|
|
'sticky' => $object->sticky,
|
|
'created' => $object->created,
|
|
));
|
|
}
|
|
$query->execute();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements 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['tid'],
|
|
'sticky' => $object->sticky,
|
|
'created' => $object->created,
|
|
));
|
|
}
|
|
$query->execute();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements 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();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements 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"
|
|
*/
|