| 1 |
<?php
|
| 2 |
// $Id: taxonomy.pages.inc,v 1.42 2009/10/17 12:08:21 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Page callbacks for the taxonomy module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Menu callback; displays all nodes associated with a term.
|
| 11 |
*
|
| 12 |
* @param $term
|
| 13 |
* The taxonomy term.
|
| 14 |
* @return
|
| 15 |
* The page content.
|
| 16 |
*/
|
| 17 |
function taxonomy_term_page($term) {
|
| 18 |
// Build breadcrumb based on the hierarchy of the term.
|
| 19 |
$current = (object) array(
|
| 20 |
'tid' => $term->tid,
|
| 21 |
);
|
| 22 |
$breadcrumb = array();
|
| 23 |
while ($parents = taxonomy_get_parents($current->tid)) {
|
| 24 |
$current = array_shift($parents);
|
| 25 |
$breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
|
| 26 |
}
|
| 27 |
$breadcrumb[] = l(t('Home'), NULL);
|
| 28 |
$breadcrumb = array_reverse($breadcrumb);
|
| 29 |
drupal_set_breadcrumb($breadcrumb);
|
| 30 |
drupal_add_feed(url('taxonomy/term/' . $term->tid . '/feed'), 'RSS - ' . $term->name);
|
| 31 |
drupal_add_css(drupal_get_path('module', 'taxonomy') . '/taxonomy.css');
|
| 32 |
|
| 33 |
field_attach_prepare_view('taxonomy_term', array($term->tid => $term), 'full');
|
| 34 |
$build = array();
|
| 35 |
$build += field_attach_view('taxonomy_term', $term);
|
| 36 |
if (!empty($term->description)) {
|
| 37 |
$build['term_description'] = array(
|
| 38 |
'#markup' => filter_xss_admin($term->description),
|
| 39 |
'#weight' => -1,
|
| 40 |
'#prefix' => '<div class="taxonomy-term-description">',
|
| 41 |
'#suffix' => '</div>',
|
| 42 |
);
|
| 43 |
}
|
| 44 |
if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {
|
| 45 |
$nodes = node_load_multiple($nids);
|
| 46 |
$build += node_build_multiple($nodes);
|
| 47 |
$build['pager'] = array(
|
| 48 |
'#markup' => theme('pager', array('tags' => NULL)),
|
| 49 |
'#weight' => 5,
|
| 50 |
);
|
| 51 |
}
|
| 52 |
else {
|
| 53 |
$build['no_content'] = array(
|
| 54 |
'#prefix' => '<p>',
|
| 55 |
'#markup' => t('There are currently no posts in this category.'),
|
| 56 |
'#suffix' => '</p>',
|
| 57 |
);
|
| 58 |
}
|
| 59 |
return $build;
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Generate the content feed for a taxonomy term.
|
| 64 |
*
|
| 65 |
* @param $term
|
| 66 |
* The taxonomy term.
|
| 67 |
*/
|
| 68 |
function taxonomy_term_feed($term) {
|
| 69 |
$channel['link'] = url('taxonomy/term/' . $term->tid, array('absolute' => TRUE));
|
| 70 |
$channel['title'] = variable_get('site_name', 'Drupal') . ' - ' . $term->name;
|
| 71 |
// Only display the description if we have a single term, to avoid clutter and confusion.
|
| 72 |
// HTML will be removed from feed description, so no need to filter here.
|
| 73 |
$channel['description'] = $term->description;
|
| 74 |
$nids = taxonomy_select_nodes($term->tid, FALSE, variable_get('feed_default_items', 10));
|
| 75 |
|
| 76 |
node_feed($nids, $channel);
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Helper function for autocompletion
|
| 81 |
*/
|
| 82 |
function taxonomy_autocomplete($field_name, $bundle, $tags_typed = '') {
|
| 83 |
$instance = field_info_instance($field_name, $bundle);
|
| 84 |
$field = field_info_field($field_name);
|
| 85 |
|
| 86 |
// The user enters a comma-separated list of tags. We only autocomplete the last tag.
|
| 87 |
$tags_typed = drupal_explode_tags($tags_typed);
|
| 88 |
$tag_last = drupal_strtolower(array_pop($tags_typed));
|
| 89 |
|
| 90 |
$matches = array();
|
| 91 |
if ($tag_last != '') {
|
| 92 |
|
| 93 |
// Part of the criteria for the query come from the field's own settings.
|
| 94 |
$vids = array();
|
| 95 |
foreach ($field['settings']['allowed_values'] as $tree) {
|
| 96 |
$vids[] = $tree['vid'];
|
| 97 |
}
|
| 98 |
|
| 99 |
$query = db_select('taxonomy_term_data', 't');
|
| 100 |
$query->addTag('translatable');
|
| 101 |
$query->addTag('term_access');
|
| 102 |
|
| 103 |
// Do not select already entered terms.
|
| 104 |
if (!empty($tags_typed)) {
|
| 105 |
$query->condition('t.name', $tags_typed, 'NOT IN');
|
| 106 |
}
|
| 107 |
$tags_return = $query
|
| 108 |
->fields('t', array('tid', 'name'))
|
| 109 |
->condition('t.vid', $vids)
|
| 110 |
// Select rows that match by term name.
|
| 111 |
->condition(db_or()
|
| 112 |
->where("t.name LIKE :last_string", array(':last_string' => '%' . $tag_last . '%'))
|
| 113 |
)
|
| 114 |
->range(0, 10)
|
| 115 |
->execute()
|
| 116 |
->fetchAllKeyed();
|
| 117 |
|
| 118 |
$prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
|
| 119 |
|
| 120 |
$term_matches = array();
|
| 121 |
foreach ($tags_return as $tid => $name) {
|
| 122 |
$n = $name;
|
| 123 |
// Term names containing commas or quotes must be wrapped in quotes.
|
| 124 |
if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
|
| 125 |
$n = '"' . str_replace('"', '""', $name) . '"';
|
| 126 |
}
|
| 127 |
else {
|
| 128 |
$term_matches[$prefix . $n] = filter_xss($name);
|
| 129 |
}
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
drupal_json_output($term_matches);
|
| 134 |
}
|