0

I need to change the text that appears in pagers (first/previous/next/last). I would really prefer to not get into the overhead (and sloppiness IMO) of a string override module.

Pager.inc seems extremely themable, but man is it strangely designed ...

This is as far as I got -- in template.php:

function phptemplate_pager_first($text, $limit, $element=0, $parameters=array()) {
    return theme_pager_first('« Oldest', $limit, $element, $parameters);
}

function phptemplate_pager_previous($text, $limit, $element=0, $interval=1, $parameters=array()) {
    if (is_numeric($text)) {
        return theme_pager_previous($text, $limit, $element, $interval, $parameters);
    } else {
        return theme_pager_previous('‹ Older', $limit, $element, $interval, $parameters);
    }
}

function phptemplate_pager_next($text, $limit, $element=0, $interval=1, $parameters=array()) {
    if (is_numeric($text)) {
        return theme_pager_next($text, $limit, $element, $interval, $parameters);
    } else {
        return theme_pager_next('Newer ›', $limit, $element, $interval, $parameters);
    }
}

function phptemplate_pager_last($text, $limit, $element=0, $parameters=array()) {
    return theme_pager_last('Newest »', $limit, $element, $parameters);
}

Almost works, until you travel to the second to last page where (in pager.inc) theme_pager_next() delegates to theme_pager_last() and this replacement breaks ...

Isn't there a clean way to do this with themeing? Hopefully better than just copy/pasting theme_pager() and replacing strings there?

2

Hi I used this and it works very well. Put it into template.php

function yourTheme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 5) {
  global $pager_page_array, $pager_total;

  // Calculate various markers within this pager piece:
  // Middle is used to "center" pages around the current page.
  $pager_middle = ceil($quantity / 2);
  // current is the page we are currently paged to
  $pager_current = $pager_page_array[$element] + 1;
  // first is the first page listed by this pager piece (re quantity)
  $pager_first = $pager_current - $pager_middle + 1;
  // last is the last page listed by this pager piece (re quantity)
  $pager_last = $pager_current + $quantity - $pager_middle;
  // max is the maximum page number
  $pager_max = $pager_total[$element];
  // End of marker calculations.

  $pager_delim = '';

  // Prepare for generation loop.
  $i = $pager_first;
  if ($pager_last > $pager_max) {
    // Adjust "center" if at end of query.
    $i = $i + ($pager_max - $pager_last);
    $pager_last = $pager_max;
  }
  if ($i <= 0) {
    // Adjust "center" if at start of query.
    $pager_last = $pager_last + (1 - $i);
    $i = 1;
  }
  // End of generation loop preparation.
$li_first_numb = theme('pager_first', (isset($tags[0]) ? $tags[0] : t('1')), $limit, $element, $parameters);
  $li_first = theme('pager_first', (isset($tags[0]) ? $tags[0] : t('<<')), $limit, $element, $parameters);
  $li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('<')), $limit, $element, 1, $parameters);
  $li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('>')), $limit, $element, 1, $parameters);
  $li_last = theme('pager_last', (isset($tags[4]) ? $tags[4] : t('>>')), $limit, $element, $parameters);
  $li_last_numb = theme('pager_last', (isset($tags[4]) ? $tags[4] : t($pager_max)), $limit, $element, $parameters);


  if ($pager_total[$element] > 1) {
    if ($li_first) {
      $items[] = array(
        'class' => 'pager-first',
        'data' => $li_first,
      );
    }
    if ($li_previous) {
      $items[] = array(
        'class' => 'pager-previous',
        'data' => $li_previous,
      );
    }

    // When there is more than one page, create the pager list.
    if ($i != $pager_max) {
    if ($i > 1) {
        $items[] = array(
          'class' => 'pager-first',
          'data' => $li_first_numb,
        );
      }
      if ($i > 1) {
        $items[] = array(
          'class' => 'pager-ellipsis',
          'data' => '...',
        );
      }
      // Now generate the actual pager piece.
      for (; $i <= $pager_last && $i <= $pager_max; $i++) {
        if ($i < $pager_current) {
          $items[] = array(
            'class' => 'pager-item',
            'data' => theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters).$pager_delim,
          );
        }
        if ($i == $pager_current) {
          $items[] = array(
            'class' => 'pager-current',
            /*'data' => '['.$i.']'.$pager_delim,*/
            'data' => $i,
          );
        }
        if ($i > $pager_current) {
          $items[] = array(
            'class' => 'pager-item',
            'data' => $pager_delim.theme('pager_next', $i, $limit, $element, ($i - $pager_current), $parameters),
          );
        }
      }
      if ($i < $pager_max) {
        $items[] = array(
          'class' => 'pager-ellipses',
          'data' => '...',
        );
      }
      if ($i < $pager_max) {
        $items[] = array(
          'class' => 'pager-max-numb',
          'data' => $li_last_numb,
        );
      }

    }
    // End generation.
    if ($li_next) {
      $items[] = array(
        'class' => 'pager-next',
        'data' => $li_next,
      );
    }
    if ($li_last) {
      $items[] = array(
        'class' => 'pager-last',
        'data' => $li_last,
      );
    }
    return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
  }
}

In this part of the code

 $li_first_numb = theme('pager_first', (isset($tags[0]) ? $tags[0] : t('1')), $limit, $element, $parameters);
      $li_first = theme('pager_first', (isset($tags[0]) ? $tags[0] : t('<<')), $limit, $element, $parameters);
      $li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('<')), $limit, $element, 1, $parameters);
      $li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('>')), $limit, $element, 1, $parameters);
      $li_last = theme('pager_last', (isset($tags[4]) ? $tags[4] : t('>>')), $limit, $element, $parameters);

You can modify first/previous/next/last

1
  • Well, I was hoping to not copy/paste such a large function... But it's not so bad, and it works! – ack Jun 1 '12 at 5:25
0

You can use one of the following for a simple version (works in Drupal 7)

function THEMENAME_preprocess_pager(&$variables, $hook) {
  if ($variables['quantity'] > 5) $variables['quantity'] = 5;
  $variables['tags'][0] = '<<';
  $variables['tags'][1] = '<';
  $variables['tags'][3] = '>';
  $variables['tags'][4] = '>>';
}

or

function THEMENAME_pager($tags = array(), $limit = 10, $element = 0, $parameters = array()) {
  global $pager_page_array, $pager_total;
  $page_prev = $pager_page_array[$element] - 1;
  $page_curr = $pager_page_array[$element] + 1;
  $page_next = $pager_page_array[$element] + 1;
  if ($pager_total[$element] > 1) {
    $output = '<div class="previous-next">';
    if ($pager_page_array[$element]!=0) $output.= '<a href="?page='.$page_prev.'" class="previous">Previous</a>';
    $output.= '<div class="previous-next-page">Page '.$page_curr.'/'.$pager_total[$element].'</div>';
    if ($page_curr!=$pager_total[$element]) $output.= '<a href="?page='.$page_next.'" class="next">Older blog posts</a>';
    $output.= '</div>';
    return $output;
  }
}

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.