Hướng dẫn tạo Pager trong Drupal 8 không cần DB_SELECT() và PAGERDEFAULT

Hướng dẫn tạo Pager trong Drupal 8 không cần DB_SELECT() và PAGERDEFAULT

Pager: no new to the dictionary of Web, plays an important role in reducing long scrolling and improvises the visual look of the site. One of the pages in my site lists statistics based details. It wasn't that appealing to go ahead with this page which had almost 100 plus items to display. At this point I had a sudden idea to split the statistics with the help of pager.

I stoked with this decision as I thought PagerDefault in Drupal can help me a hand. But later I recognized the same cannot be used in my case as it was not rendered with any query. What I had in my page was just array which was rendered by a theme_table(), to construct a pager for this proved my decision quite rugged. I leave no stones unturned to pick the best way to solve my need.

And finally I found a solution and created a pager. The below mentioned code proved worthy for a solution.

$per_page = 10;

// Initialize the pager

$current_page = pager_default_initialize(count($rows), $per_page);

// Split your list into page sized chunks

$chunks = array_chunk($rows, $per_page, TRUE);

// Show the appropriate items from the list

$output = theme('table', array('header' => $header, 'rows' => $chunks[$current_page]));

// Show the pager

$output .= theme('pager', array('quantity',count($rows)));

The Page_default_initialize() as mentioned initializes pager for a page. The first argument in my case was the total number of items that was to be paged. I broke this total items into array of size 10 using array_chunk(). And the rest is just a normal table theming.

The above function can be used in the corresponding theme function either in module level or in template level. Once done the pager must start working in your case too.

if ($type == 21) { 
  $query = new EntityFieldQuery(); 

  $entities = $query->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'product') 
    ->fieldCondition('field_razred', 'tid', array (607,608,609,610), 'IN')
    ->pager(10) // this is the bit you're missing
    ->execute(); 

  $nodes = node_load_multiple(array_keys($entities['node'])); 
  print render(node_view_multiple($nodes, 'teaser')) . theme('pager');
}
Bạn thấy bài viết này như thế nào?: 
Average: 7.1 (16 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Bình luận (0)

 

Add Comment

Filtered HTML

  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Các thẻ HTML được chấp nhận: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Tự động ngắt dòng và đoạn văn.

Plain text

  • No HTML tags allowed.
  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Tự động ngắt dòng và đoạn văn.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Phân tích exposing External Content trong Drupal’s Search

Phân tích exposing External Content trong Drupal’s Search

Search is a vital part of content-rich websites. Like many CMS or blogging platforms, Drupal provides basic search functionality out-of-the-box. 

10 Facebook Timeline sáng tạo mà bạn có thể lấy ý tưởng

10 Facebook Timeline sáng tạo mà bạn có thể lấy ý tưởng

Thiết kế Timeline mới của Facebook hiện cho phép người dùng có một không gian khá lớn ngay đầu trang để trình bày ảnh bìa. 

Sử dụng panels page để làm trang Access Denied - 403

Sử dụng panels page để làm trang Access Denied - 403

What happens when a user bookmarks an access-protected page?

Công ty diệt chuột T&C

 

Diet con trung