-1

So I have a custom module where there is a basic function that returns an array of data where I try to add a pager. I then call the function in the template.php the pass a custom variable to my template file. A listing appears there, with the working pagination to say so, but the list items are displayed weird. All the data is added as a HTML attribute.

This is how I create the pager from my function:

$result = Array(
    array(0)
        tile
        author
    array(1)
        tile
        author
    array(2)
        tile
        author
    array(3)
        tile
        author
     ....

  $total = count($result);
  $num_per_page = 12;
  // Initialize pager and gets current page
  $current_page = pager_default_initialize($total, $num_per_page);
  // Split the items up into chunks:
  $chunks = array_chunk($result, $num_per_page);
  // Get the items for our current page:
  $current_page_items = $chunks[$current_page];
  // Theme each item. Note that the theme
  // some_theme_hook is left out of this tutorial
  // for simplicity
  foreach($current_page_items as $i => $item) {
    $current_page_items[$i] = array(
      '#item' => $item,
    );
  }

  // Generate the render array for our page
  $render_array['page'] = array (
    '#theme' => 'item_list',
    '#items' => $current_page_items,
  );

  // Calls Drupal standard pager theme and set 5 page links on pager
  $render_array['pager'] = array (
    '#theme' => 'pager',
    '#quantity' => 5,
  );

  return $render_array;

And this is my rendered markup:

<li #item="http://localhost.../sites/default/files/styles/videos_page/public/16-Inside-03-L.jpg?itok=PXCk5oXs Inside the Track #3 watch for $123 Michael Brauer The Rides Game on Series new 1:20:00" class="first">
</li>

Any ideas of how can I manage this?

  • The Examples module contains a "pager" example. – Patrick Kenny Oct 5 '17 at 15:01
  • Yes. You should leverage the core pager generation pattern, there's nothing much to it. – Kevin Oct 5 '17 at 17:57
  • @PatrickKenny Thanks for your answer. But as I've said above, I'm not using a limit on the query. Because I have multiple queries and I merge the results in an array. I need to create a pagination from that array. – mihai Oct 6 '17 at 8:02
  • @mihai Did you look at the code I suggested? All you have posted is an array structure, and no attempt to make it work with the drupal pager. – Patrick Kenny Oct 6 '17 at 8:04
  • @PatrickKenny Yes, ofcourse. There is only one example and that extends the "PagerDefaul" on a query db_select('date_formats', 'd')->extend('PagerDefault') – mihai Oct 6 '17 at 8:06
0

The code I've pasted works actually. I've just replaced the 'item_list' theme, with a custom one, where I added my markup per Item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.