1

I am having problems getting a pager to display after a table in the block_view hook of a module

The code for the query is the following one.

$query = db_select('registry', 'r');
$query = $query->extend('PagerDefault')->extend('TableSort');

$query->limit(10)
    ->fields('r', array('name', 'type', 'filename','module','weight'));
$result = $query->execute();

I build and the table with no problem adding it to the content

Then I add the pager or rather I don't with the following code

$block['content'] .= theme('pager').'<br>mmm<br>';

the mmm bit was just there as a quick check to see what was happening.

I know that the extend needs to be assigned to a new variable and I believe that I am doing this.

Is there anything wrong I am doing?

This is the complete code of the module.

/**
 * @file
 * A block module that displays registry info.
 */

/**
 * Implements hook_help.
 *
 * Displays help and module information.
 *
 * @param path 
 *   Which path of the site we're using to display help
 * @param arg 
 *   Array that holds the current path as returned from arg() function
 */
function current_posts_help($path, $arg) {
  switch ($path) {
    case "admin/help#current_posts":
    return '<p>' . t("Displays registry info for testing tables and pager") . '</p>';
    break;
  }
} 

/**
 * Implements hook_block_info().
 */
function current_posts_block_info() {
  $blocks['current_posts'] = array(
    'info' => t('Current posts'), //The name that will appear in the block list.
    'cache' => DRUPAL_CACHE_PER_ROLE, //Default
  );
  return $blocks;
}

function current_posts_block_view($delta = '') {
  switch($delta) {
    case 'current_posts':
      $block['subject'] = t('Modules list');
      if(user_access('access content')) {
        $query = db_select('registry', 'r');
        $query = $query->extend('PagerDefault');
        //$result = $query->limit(50)
        $query->limit(10)
          ->fields('r', array('name', 'type', 'filename','module','weight'));
        $result = $query->execute();

        // Array to contain items for the block to render.

        $rows = array();

        // Iterate over the resultset. 
        foreach ($result as $node) {
          $rows[] = array(
            'name' => t($node->name),
            'type' => t($node->type),
            'filename' => t($node->filename),
            'module' => t($node->filename),
          );
        }

        // Table header.
        $header = array(
          'name' => t('Name'),
          'type'     => t('Type'),
          'filename'    => t('Filename'),
          'module'    => t('Module'),
        );

        $block['content'] = theme('table', array(
          'header'  => $header,
          'rows'    => $rows,
          'id'      => 'table-articles',
          'class'   => 'articles',)
        );

        $block['content'] .= theme('pager').'<br>mmm<br>';
      }

      return $block;        
  }
}

/**
 * Implements hook_menu().
 */
function current_posts_menu() {
  $items = array();

  $items['admin/config/content/current_posts'] = array(
    'title' => 'Current Registry',
    'description' => 'Configuration for Current posts module',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('current_posts_form'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Page callback: Current posts settings
 *
 * @see current_posts_menu()
 */
function current_posts_form($form, &$form_state) {
  $form['current_posts_max'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum number of posts'),
    '#default_value' => variable_get('current_posts_max', 3),
    '#size' => 2,
    '#maxlength' => 2,
    '#description' => t('The maximum number of links to display in the block.'),
    '#required' => TRUE,
  );

  return system_settings_form($form);
}
3
  • 1
    Does the query return more than 10 rows?
    – apaderno
    Oct 21, 2012 at 20:57
  • Yes it does - it's 340 records As additional info I have now tried the example pager. This works but it is a menucallback page rather than part of a module. If I cut and paste the code from here to the example it works. Seems that I have a problem with it being in a module??? Oct 22, 2012 at 10:21
  • I tested your code on my test site, and it works.
    – apaderno
    Oct 24, 2012 at 6:10

5 Answers 5

1

I think I know what is going on. I've had this problem several times before.

The issue is that on some pages, multiple pagers may be in use. Which would explain why theme('pager'); wouldn't work.

I just had the same problem, and I got it working by doing this:

theme('pager', array('element' => 1));

On my end, I wasn't displaying multiple pagers, but I think something else on the page must have been doing this, because the above code made it work! I hope this helps anyone else who encounters the same problem.

1

I have just solved this myself. What a head-banger this one was.

Just try replacing:

$query = $query->extend('PagerDefault')->extend('TableSort');

with:

$query = $query->extend('PagerDefault')->element(0)->extend('TableSort');
0

After the code $result = $query->execute(); try adding the following code for initializing a pager for theme('pager').

pager_default_initialize($total,5);

$total -> total result count and argument '5' means 5 results should show in each page.

1
  • that does work if I put in pager_default_initialize(340,5); problem is that according to the docs it should work from the ->extend('PagerDefault'); and the 340 that I have replaced $total with is the total number or records however I do not have immediate access to that. I have also had a game with just getting the query to work, maybe this is the root of the problem. Oct 22, 2012 at 14:09
0

I'm assuming this is still some kind of bug in Views, but the pager tends to fail when trying to create an HTML template for it. In most cases your could manage with CSS.

0

The ‘->extend(‘PagerDefault’) extension in the query had to be the first function in the daisy chain. If not there is no error but the function seems to not be called.And one more thing, fields('table',array('id','name','created','uid')) check you mention fields properly,This is working for me.

$query = db_select('address_book', 'n') ->extend('PagerDefault') //Pager Extender ->fields('n', array('address_id')) ->condition('status', 1) ->orderBy('created', 'DESC') ->limit(10);

see this link https://stackoverflow.com/questions/9614549/drupal-7-themepager-table-gets-rendered-but-without-pager

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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