Pager in Render Array In a custom module I have a block defined and the function that builds the block returns a render array. In that render array I am looking to create a pager for my content since it is a listing of items.

If I run pager_default_initialize during my query building like so this automatically adds a pager to the top of the block.

$total = $query-&gt;countQuery()
  -&gt;execute()
  -&gt;fetchField();

$page = pager_default_initialize($total, $per);

$query-&gt;extend('PagerDefault')
  -&gt;limit($per)
  -&gt;range($page * $per, $per);


I want to have more control over the pager, like adding it to the bottom of the block. If I remove the pager_default_initialize from the query building code like so, I can stop the pager from automatically being added.

$total = $query-&gt;countQuery()
  -&gt;execute()
  -&gt;fetchField();

$page = pager_find_page();

$query-&gt;extend('PagerDefault')
  -&gt;limit($per)
  -&gt;range($page * $per, $per);


So, the question is, how do I add the pager to my render array so it renders the pager at the bottom of the block?

My render array currently looks like this:

$content['results'] = array(
  '#type'       =&gt; 'container',
  '#attributes' =&gt; array(
    'class' =&gt; 'results',
  ),
);

if ($data-&gt;rowCount()) {
  foreach ($data as $record) {
    $content['results'][] = array(
      '#theme' =&gt; 'CUSTOM_NODE_THEME',
      '#node'   =&gt; node_load($record-&gt;nid),
    );
  }
  if ($per &gt; 0) {
    $content['results']['pager'] = array(
      '#theme' =&gt; 'pager',
    );
  }
}


The theme_pager() is not being run when the block is rendered.

Any help would be awesome!
Here is the solution. I knew as soon as I asked the question I would find the answer!

First off, the query building code must not run pager_default_initialize, like so:

$total = $query-&gt;countQuery()
  -&gt;execute()
  -&gt;fetchField();

$page = pager_find_page();

$query-&gt;extend('PagerDefault')
  -&gt;limit($per)
  -&gt;range($page * $per, $per);


The pager render array should be created like so:

$content['results']['pager'] = array(
  '#pre_render' =&gt; array('CUSTOM_PRE_RENDER'),
  '#theme' =&gt; 'pager',
  '#variables' =&gt; array(
    'total' =&gt; $total,
    'per' =&gt; $per,
  ),
);


And then run pager_default_initialize in the custom pre render function:

function custom_pre_render_intialize_pager($_element) {
  pager_default_initialize($_element['#variables']['total'], $_element['#variables']['per']);

  return $_element;
}


This code will only work to add a single pager to the block and may not work if there is more then one pager on the page.