i18n_taxonomy.pages.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * Helper function for autocompletion. Replacement for taxonomy_autocomplete
  59. */
  60. function i18n_taxonomy_autocomplete_field($field_name, $tags_typed = '') {
  61. // Part of the criteria for the query come from the field's own settings.
  62. $field = field_info_field($field_name);
  63. $vids = array();
  64. $vocabularies = taxonomy_vocabulary_get_names();
  65. foreach ($field['settings']['allowed_values'] as $tree) {
  66. $vids[] = $vocabularies[$tree['vocabulary']]->vid;
  67. }
  68. // This has been redirected from taxonomy module so we add current language and no language
  69. // Because some of the vocabularies may not have language
  70. $langcode = array(i18n_langcode(), LANGUAGE_NONE);
  71. return _i18n_taxonomy_autocomplete($langcode, $vids, $tags_typed);
  72. }
  73. /**
  74. * Helper function for autocompletion. Select by language
  75. */
  76. function i18n_taxonomy_autocomplete_language($langcode, $vocabulary, $tags_typed = '') {
  77. $vids = $vocabulary ? array($vocabulary->vid) : NULL;
  78. return _i18n_taxonomy_autocomplete($langcode, $vids, $tags_typed);
  79. }
  80. /**
  81. * Helper function for autocompletion
  82. */
  83. function _i18n_taxonomy_autocomplete($langcode, $vids, $tags_typed = '') {
  84. // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  85. $tags_typed = drupal_explode_tags($tags_typed);
  86. $tag_last = drupal_strtolower(array_pop($tags_typed));
  87. $matches = array();
  88. if ($langcode && $tag_last != '') {
  89. $query = db_select('taxonomy_term_data', 't')
  90. ->fields('t', array('tid', 'name'));
  91. $query->addTag('translatable');
  92. $query->addTag('taxonomy_term_access');
  93. // Disable i18n_select for this query
  94. $query->addTag('i18n_select');
  95. // Add language condition
  96. $query->condition('t.language', $langcode);
  97. // Do not select already entered terms.
  98. if (!empty($tags_typed)) {
  99. $query->condition('t.name', $tags_typed, 'NOT IN');
  100. }
  101. // There may be vocabulary restrictions
  102. if ($vids) {
  103. $query->condition('t.vid', $vids);
  104. }
  105. // Select rows that match by term name.
  106. $tags_return = $query
  107. ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
  108. ->range(0, 10)
  109. ->execute()
  110. ->fetchAllKeyed();
  111. $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
  112. $term_matches = array();
  113. foreach ($tags_return as $tid => $name) {
  114. $n = $name;
  115. // Term names containing commas or quotes must be wrapped in quotes.
  116. if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
  117. $n = '"' . str_replace('"', '""', $name) . '"';
  118. }
  119. $term_matches[$prefix . $n] = check_plain($name);
  120. }
  121. }
  122. drupal_json_output($term_matches);
  123. }