taxonomy.pages.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @file
  4. * Page callbacks for the taxonomy module.
  5. */
  6. /**
  7. * Menu callback; displays all nodes associated with a term.
  8. *
  9. * @param $term
  10. * The taxonomy term.
  11. * @return
  12. * The page content.
  13. */
  14. function taxonomy_term_page($term) {
  15. // If there is a menu link to this term, the link becomes the last part of
  16. // the active trail, and the link name becomes the page title. Thus, we must
  17. // explicitly set the page title to be the term title.
  18. drupal_set_title($term->name);
  19. // Build breadcrumb based on the hierarchy of the term.
  20. $current = (object) array(
  21. 'tid' => $term->tid,
  22. );
  23. // @todo This overrides any other possible breadcrumb and is a pure hard-coded
  24. // presumption. Make this behavior configurable per vocabulary or term.
  25. $breadcrumb = array();
  26. while ($parents = taxonomy_get_parents($current->tid)) {
  27. $current = array_shift($parents);
  28. $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
  29. }
  30. $breadcrumb[] = l(t('Home'), NULL);
  31. $breadcrumb = array_reverse($breadcrumb);
  32. drupal_set_breadcrumb($breadcrumb);
  33. drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);
  34. // Set the term path as the canonical URL to prevent duplicate content.
  35. $uri = entity_uri('taxonomy_term', $term);
  36. drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
  37. // Set the non-aliased path as a default shortlink.
  38. drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);
  39. // Normally we would call taxonomy_term_show() here, but for backwards
  40. // compatibility in Drupal 7 we do not want to do that (it produces different
  41. // data structures and HTML markup than what Drupal 7 released with). Calling
  42. // taxonomy_term_view() directly provides essentially the same thing, but
  43. // allows us to wrap the rendered term in our desired array structure.
  44. $build['term_heading'] = array(
  45. '#prefix' => '<div class="term-listing-heading">',
  46. '#suffix' => '</div>',
  47. 'term' => taxonomy_term_view($term, 'full'),
  48. );
  49. if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {
  50. $nodes = node_load_multiple($nids);
  51. $build += node_view_multiple($nodes);
  52. $build['pager'] = array(
  53. '#theme' => 'pager',
  54. '#weight' => 5,
  55. );
  56. }
  57. else {
  58. $build['no_content'] = array(
  59. '#prefix' => '<p>',
  60. '#markup' => t('There is currently no content classified with this term.'),
  61. '#suffix' => '</p>',
  62. );
  63. }
  64. return $build;
  65. }
  66. /**
  67. * Generate the content feed for a taxonomy term.
  68. *
  69. * @param $term
  70. * The taxonomy term.
  71. */
  72. function taxonomy_term_feed($term) {
  73. $channel['link'] = url('taxonomy/term/' . $term->tid, array('absolute' => TRUE));
  74. $channel['title'] = variable_get('site_name', 'Drupal') . ' - ' . $term->name;
  75. // Only display the description if we have a single term, to avoid clutter and confusion.
  76. // HTML will be removed from feed description.
  77. $channel['description'] = check_markup($term->description, $term->format, '', TRUE);
  78. $nids = taxonomy_select_nodes($term->tid, FALSE, variable_get('feed_default_items', 10));
  79. node_feed($nids, $channel);
  80. }
  81. /**
  82. * Page callback: Outputs JSON for taxonomy autocomplete suggestions.
  83. *
  84. * Path: taxonomy/autocomplete
  85. *
  86. * This callback outputs term name suggestions in response to Ajax requests
  87. * made by the taxonomy autocomplete widget for taxonomy term reference
  88. * fields. The output is a JSON object of plain-text term suggestions, keyed by
  89. * the user-entered value with the completed term name appended. Term names
  90. * containing commas are wrapped in quotes.
  91. *
  92. * For example, suppose the user has entered the string 'red fish, blue' in the
  93. * field, and there are two taxonomy terms, 'blue fish' and 'blue moon'. The
  94. * JSON output would have the following structure:
  95. * @code
  96. * {
  97. * "red fish, blue fish": "blue fish",
  98. * "red fish, blue moon": "blue moon",
  99. * };
  100. * @endcode
  101. *
  102. * @param $field_name
  103. * The name of the term reference field.
  104. * @param $tags_typed
  105. * (optional) A comma-separated list of term names entered in the
  106. * autocomplete form element. Only the last term is used for autocompletion.
  107. * Defaults to '' (an empty string).
  108. *
  109. * @see taxonomy_menu()
  110. * @see taxonomy_field_widget_info()
  111. */
  112. function taxonomy_autocomplete($field_name, $tags_typed = '') {
  113. // If the request has a '/' in the search text, then the menu system will have
  114. // split it into multiple arguments, recover the intended $tags_typed.
  115. $args = func_get_args();
  116. // Shift off the $field_name argument.
  117. array_shift($args);
  118. $tags_typed = implode('/', $args);
  119. // Make sure the field exists and is a taxonomy field.
  120. if (!($field = field_info_field($field_name)) || $field['type'] !== 'taxonomy_term_reference') {
  121. // Error string. The JavaScript handler will realize this is not JSON and
  122. // will display it as debugging information.
  123. print t('Taxonomy field @field_name not found.', array('@field_name' => $field_name));
  124. exit;
  125. }
  126. // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  127. $tags_typed = drupal_explode_tags($tags_typed);
  128. $tag_last = drupal_strtolower(array_pop($tags_typed));
  129. $matches = array();
  130. if ($tag_last != '') {
  131. // Part of the criteria for the query come from the field's own settings.
  132. $vids = array();
  133. $vocabularies = taxonomy_vocabulary_get_names();
  134. foreach ($field['settings']['allowed_values'] as $tree) {
  135. $vids[] = $vocabularies[$tree['vocabulary']]->vid;
  136. }
  137. $query = db_select('taxonomy_term_data', 't');
  138. $query->addTag('translatable');
  139. $query->addTag('term_access');
  140. // Do not select already entered terms.
  141. if (!empty($tags_typed)) {
  142. $query->condition('t.name', $tags_typed, 'NOT IN');
  143. }
  144. // Select rows that match by term name.
  145. $tags_return = $query
  146. ->fields('t', array('tid', 'name'))
  147. ->condition('t.vid', $vids)
  148. ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
  149. ->range(0, 10)
  150. ->execute()
  151. ->fetchAllKeyed();
  152. $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
  153. $term_matches = array();
  154. foreach ($tags_return as $tid => $name) {
  155. $n = $name;
  156. // Term names containing commas or quotes must be wrapped in quotes.
  157. if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
  158. $n = '"' . str_replace('"', '""', $name) . '"';
  159. }
  160. $term_matches[$prefix . $n] = check_plain($name);
  161. }
  162. }
  163. drupal_json_output($term_matches);
  164. }