i18n_taxonomy.pages.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @file
  4. * Page callbacks for the taxonomy module, i18n remake.
  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 i18n_taxonomy_term_page($term) {
  15. $term = i18n_taxonomy_localize_terms($term);
  16. // Assign the term name as the page title.
  17. drupal_set_title($term->name);
  18. // Build breadcrumb based on the hierarchy of the term.
  19. $current = (object) array(
  20. 'tid' => $term->tid,
  21. );
  22. // @todo This overrides any other possible breadcrumb and is a pure hard-coded
  23. // presumption. Make this behavior configurable per vocabulary or term.
  24. $breadcrumb = array();
  25. while ($parents = taxonomy_get_parents($current->tid)) {
  26. $parents = i18n_taxonomy_localize_terms($parents);
  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. $build = array();
  35. $build['term_heading'] = array(
  36. '#prefix' => '<div class="term-listing-heading">',
  37. '#suffix' => '</div>',
  38. 'term' => taxonomy_term_view($term, 'full'),
  39. );
  40. if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {
  41. $nodes = node_load_multiple($nids);
  42. $build += node_view_multiple($nodes);
  43. $build['pager'] = array(
  44. '#theme' => 'pager',
  45. '#weight' => 5,
  46. );
  47. }
  48. else {
  49. $build['no_content'] = array(
  50. '#prefix' => '<p>',
  51. '#markup' => t('There is currently no content classified with this term.'),
  52. '#suffix' => '</p>',
  53. );
  54. }
  55. return $build;
  56. }
  57. /**
  58. * Render a taxonomy term page HTML output.
  59. *
  60. * @param $tids
  61. * An array of term ids.
  62. * @param $result
  63. * A pager_query() result, such as that performed by taxonomy_select_nodes().
  64. *
  65. * @ingroup themeable
  66. */
  67. function theme_i18n_taxonomy_term_page($tids, $result) {
  68. drupal_add_css(drupal_get_path('module', 'taxonomy') . '/taxonomy.css');
  69. $output = '';
  70. // Only display the description if we have a single term, to avoid clutter and confusion.
  71. if (count($tids) == 1) {
  72. $term = i18n_taxonomy_localize_terms(taxonomy_term_load($tids[0]));
  73. // Check that a description is set.
  74. if (!empty($term->description)) {
  75. $output .= '<div class="taxonomy-term-description">';
  76. $output .= filter_xss_admin($term->description);
  77. $output .= '</div>';
  78. }
  79. }
  80. $output .= taxonomy_render_nodes($result);
  81. return $output;
  82. }
  83. /**
  84. * Helper function for autocompletion. Replacement for taxonomy_autocomplete
  85. */
  86. function i18n_taxonomy_autocomplete_field($field_name, $tags_typed = '') {
  87. // Part of the criteria for the query come from the field's own settings.
  88. $field = field_info_field($field_name);
  89. $vids = array();
  90. $vocabularies = taxonomy_vocabulary_get_names();
  91. foreach ($field['settings']['allowed_values'] as $tree) {
  92. $vids[] = $vocabularies[$tree['vocabulary']]->vid;
  93. }
  94. // This has been redirected from taxonomy module so we add current language and no language
  95. // Because some of the vocabularies may not have language
  96. $langcode = array(i18n_langcode(), LANGUAGE_NONE);
  97. return _i18n_taxonomy_autocomplete($langcode, $vids, $tags_typed);
  98. }
  99. /**
  100. * Helper function for autocompletion. Select by language
  101. */
  102. function i18n_taxonomy_autocomplete_language($langcode, $vocabulary, $tags_typed = '') {
  103. $vids = $vocabulary ? array($vocabulary->vid) : NULL;
  104. return _i18n_taxonomy_autocomplete($langcode, $vids, $tags_typed);
  105. }
  106. /**
  107. * Helper function for autocompletion
  108. */
  109. function _i18n_taxonomy_autocomplete($langcode, $vids, $tags_typed = '') {
  110. // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  111. $tags_typed = drupal_explode_tags($tags_typed);
  112. $tag_last = drupal_strtolower(array_pop($tags_typed));
  113. $matches = array();
  114. if ($langcode && $tag_last != '') {
  115. $query = db_select('taxonomy_term_data', 't')
  116. ->fields('t', array('tid', 'name'));
  117. $query->addTag('translatable');
  118. $query->addTag('term_access');
  119. // Disable i18n_select for this query
  120. $query->addTag('i18n_select');
  121. // Add language condition
  122. $query->condition('t.language', $langcode);
  123. // Do not select already entered terms.
  124. if (!empty($tags_typed)) {
  125. $query->condition('t.name', $tags_typed, 'NOT IN');
  126. }
  127. // There may be vocabulary restrictions
  128. if ($vids) {
  129. $query->condition('t.vid', $vids);
  130. }
  131. // Select rows that match by term name.
  132. $tags_return = $query
  133. ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
  134. ->range(0, 10)
  135. ->execute()
  136. ->fetchAllKeyed();
  137. $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
  138. $term_matches = array();
  139. foreach ($tags_return as $tid => $name) {
  140. $n = $name;
  141. // Term names containing commas or quotes must be wrapped in quotes.
  142. if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
  143. $n = '"' . str_replace('"', '""', $name) . '"';
  144. }
  145. $term_matches[$prefix . $n] = check_plain($name);
  146. }
  147. }
  148. drupal_json_output($term_matches);
  149. }