taxonomy.pages.inc 5.6 KB

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