7

I have a code, that outputs a standard pager:

print theme('pager');

it outputs this:

<h2 class="element-invisible">Pages</h2>
<div class="item-list">
<ul class="pager"><li class="pager-current first">1</li>
<li class="pager-item"><a title="Go to page 2" href="/drupal/node?page=1">2</a></li>
<li class="pager-item"><a title="Go to page 3" href="/drupal/node?page=2">3</a></li>
<li class="pager-next"><a title="Go to next page" href="/drupal/node?page=1">next ›</a></li>
<li class="pager-last last"><a title="Go to last page" href="/drupal/node?page=5">last »</a></li>
</ul>

How can I change the HTML to something like this:

<ul class="pagination">
    <li><a href="#">&lt;</a></li>
    <li><a href="#">1</a></li>
    <li><a href="#" class="active">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">&gt;</a></li>
</ul>

I looked into this thread: https://www.drupal.org/node/387786, created a function bartik_preprocess_custom_pager and a file custom-pager.tpl.php inside my Bartic theme templates, but I can't make it work.

print theme('custom_pager');

outputs nothing. Does anybody know, how can I customize the HTML output for a pager, creating one just for 1 page?

  • 2
    You also need to declare the new theme function in hook_theme(), or Drupal will not know there is a new theme function. Clear the cache after changing a theme. – kiamlaluno Dec 15 '14 at 13:56
  • @kiamlaluno I added the pager template into the function, and it's being called. But how can I pass the appropriate variables into the pager? They were passed automatically, when I called theme('pager');. Now I don't get them. – user4035 Dec 15 '14 at 14:44
  • @kiamlaluno Hi. Is it possible to customize a pager inside a module? By creating a function mymodule_pager($variables)? – user4035 Dec 15 '14 at 15:07
  • You can do it from a module too, but in that case you need to implement another hook too. – kiamlaluno Dec 15 '14 at 19:54
  • 1
    Theme functions gets $variables automatically; you just need to implement hook_theme() correctly. For changing a theme function from a module, the hook to implement is hook_theme_registry_alter(). – kiamlaluno Dec 15 '14 at 19:58
6

As pointed out in my comment above, most of the details are wrong in your implementation above. Fortunately in Drupal 7, there are six theming functions for the pager:

These should allow you to make most of the customizations that you need. There's a good stackexchange answer about how to override Drupal theme functions() that you can refer to for the details.

Finally, you mentioned that you want to override the pager just for one page. I still think this might be worth doing with CSS alone, but in case it's not, you may be able to just override parts of the $variables array in whichever preprocess function contains the pager (I don't know off the top of my head if it's preprocess_page or preprocess_node or if it's accessible from any of the preprocess functions at all).

2

This is my answer from a previous post.

If you implement theme_pager you'll be expected to create the entire pager render array from scratch using several theme function - theme_pager_first, theme_pager_previous, theme_pager_next and theme_pager_last. That seems like entirely too much work for adding one class.

Since theme_pager calls theme_item_list at the very end to build the final render array, you can use hook_preprocess_HOOK to modify the pager render array before drupal renders it into HTML.

function THEME_preprocess_item_list(&$vars) {
  // make sure we're dealing with a pager item list
  if (isset($vars['attributes']['class']) && in_array('pager', $vars['attributes']['class'])) {
    // make sure there are items
    if (count($vars['items'])) {
      $vars['items'][0]['class'][] = 'test';
    }
  }
}

Clear the cache after adding this hook.

UPDATE: find the first li.pager-item:

function THEME_preprocess_item_list(&$vars) {
  // make sure we're dealing with a pager item list
  if (isset($vars['attributes']['class']) && in_array('pager', $vars['attributes']['class'])) {
    // loop the items and find the first .pager-item
    foreach ($vars['items'] as $index => $item) {
      if (in_array('pager-item', $item['class'])) {
        $vars['items'][$index]['class'][] = 'test';
        break;
      }
    }
  }
}

UPDATE2: find first and last li.pager-item:

function THEME_preprocess_item_list(&$vars) {
  // make sure we're dealing with a pager item list
  if (isset($vars['attributes']['class']) && in_array('pager', $vars['attributes']['class'])) {

    // loop the items and find the first and last .pager-item
    $first = FALSE;
    $last = NULL;
    foreach ($vars['items'] as $index => $item) {
      if (in_array('pager-item', $item['class'])) {

        // first
        if (!$first) {
          $vars['items'][$index]['class'][] = 'test';
          $first = TRUE;
        }

        // last
        $last = $index;
      }
    }

    if (!empty($last)) {
      $vars['items'][$last]['class'][] = 'test2';
    }
  }
}

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.