Viết lại đầu ra của taxonomy_term_page () mà không cần hack lõi


10

Trong Drupal 7, taxonomy.pages.inc chứa taxonomy_term_page(), trong đó đặt một <div class="term-listing-heading">đầu ra xung quanh đầu ra phân loại.

Làm cách nào tôi có thể viết lại đầu ra của taxonomy_term_page () trong chủ đề của mình, để tôi có thể xóa DIV mà không cần hack lõi?

Tôi khá ngạc nhiên khi không có tệp tpl.php taxonomy_term_page()vì điều này sẽ giúp việc lấy chủ đề dễ dàng hơn nhiều.


Câu trả lời:


11

Bạn có thể làm điều đó với trang tiền xử lý một cái gì đó như thế này:

function themename_preprocess_page(&$vars) {
  if  (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
    unset($vars['page']['content']['system_main']['term_heading']['#prefix']);
    unset($vars['page']['content']['system_main']['term_heading']['#suffix']);
  }
}

trong chủ đề của bạn template.php

Tôi tin rằng system_maincó thể được gọi là một cái gì đó khác, tùy thuộc vào thiết lập trang web của bạn.


6

Vì nó là một cuộc gọi lại menu, bạn có thể triển khai hook_menu_alter () trong một mô-đun để thay đổi cuộc gọi lại menu được gọi cho trang đó.

function mymodule_menu_alter(&$items) {
  if (!empty($items['taxonomy/term/%taxonomy_term'])) {
    $items['taxonomy/term/%taxonomy_term']['page callback'] = 'mymodule_term_page';
  }
}

function mymodule_term_page($term) {
  // Build breadcrumb based on the hierarchy of the term.
  $current = (object) array(
    'tid' => $term->tid,
  );
  $breadcrumb = array();

  while ($parents = taxonomy_get_parents($current->tid)) {
    $current = array_shift($parents);
    $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
  }
  $breadcrumb[] = l(t('Home'), NULL);
  $breadcrumb = array_reverse($breadcrumb);
  drupal_set_breadcrumb($breadcrumb);
  drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);

  $build = array();

  $build['term_heading'] = array(
    'term' => taxonomy_term_view($term, 'full'),
  );

  if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {
    $nodes = node_load_multiple($nids);
    $build += node_view_multiple($nodes);
    $build['pager'] = array(
      '#theme' => 'pager', 
      '#weight' => 5,
    );
  }
  else {
    $build['no_content'] = array(
      '#prefix' => '<p>', 
      '#markup' => t('There is currently no content classified with this term.'), 
      '#suffix' => '</p>',
    );
  }
  return $build;
}

Cảm ơn! Tôi thích phương pháp của googletorp vì nó không cần một mô-đun riêng. Nhưng đề nghị của bạn là tuyệt vời vì nó cho phép kiểm soát nhiều hơn. Cảm ơn!
big_smile

Theo tôi nhớ, hook_menu_alter()có thể được thực hiện trong một chủ đề quá; trong Drupal 7, các chủ đề có thể thực hiện các móc thay đổi.
kiamlaluno

2

Giống như ví dụ trước, ngoại trừ có thể rõ ràng hơn và bằng chứng trong tương lai để sửa đổi lợi nhuận từ taxonomy_term_page trong trình bao bọc thay vì sao chép chức năng bán buôn ban đầu:

function mymodule_menu_alter(&$items) {
  if (!empty($items['taxonomy/term/%taxonomy_term'])) {
    $items['taxonomy/term/%taxonomy_term']['page callback'] = '_custom_taxonomy_term_page';
  }
}

function _custom_taxonomy_term_page ( $term ) {

   $build = taxonomy_term_page( $term );

   // Make customizations then return
   unset( $build['term_heading']['#prefix'] ); 
   unset( $build['term_heading']['#suffix'] );

   return $build;
}

Đây có lẽ là cách tốt nhất để làm điều đó.
Horatio Alderaan
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.