Custom module Pager does not work with Ajax filter My custom module uses an Ajax enabled drop down to allow users to filter images. There is also a pager attached to the query. The pager works fine when all the data is displayed i.e. no filter applied but after applying a filter, clicking a pager button just gives a white screen.

Here is my code.

function view_colouring_sheets($form, $form_state){
$query = db_select('field_data_field_image','fi')-&gt;extend('PagerDefault');
$query-&gt;join('node','n','n.nid = fi.entity_id');
$query-&gt;fields('fi');
$query-&gt;fields('n',array('title'));
$query-&gt;limit(20);
if($form_state['input']['tid']){
    $query-&gt;condition('entity_id',  colouring_sheets_filtered_tid($form_state['input']['tid']),'in');
}
$images = $query-&gt;execute()-&gt;fetchAll();


$output = array(
    '#theme'=&gt;'image_matrix',
    '#type'=&gt;'markup',
    '#images'=&gt;$images,
    '#title'=&gt;'Colouring Sheets',
    '#pager'=&gt;theme('pager'),
    '#filter_form'=&gt;  drupal_render(drupal_get_form('image_filter_form')),
);

return $output;
}




function colouring_sheets_filtered_tid($tid){
    $query = db_select('field_data_field_image_category','ic');
    $query-&gt;condition('field_image_category_tid',$tid);
    $query-&gt;fields('ic');
    $result = $query-&gt;execute()-&gt;fetchAll();
    $term_array = array();
    foreach($result as $row){
        $term_array[] = $row-&gt;entity_id;
    }
    return $term_array;
}

function image_filter_form($form, $form_state){
    $terms = taxonomy_get_tree(18);
    $terms_array = array();
    foreach($terms as $term){
        $terms_array[$term-&gt;tid] = $term-&gt;name;
    }
    $form['tid'] = array(
        '#type'=&gt;'select',
        '#options'=&gt;$terms_array,
        '#required'=&gt;TRUE,
        '#title'=&gt;'Fitler Images',
        '#ajax'=&gt;array(
            'method'=&gt;'replace',
            'wrapper'=&gt;'image_matrix',
            'callback'=&gt;'view_colouring_sheets',
            'progress' =&gt; array('type' =&gt; 'throbber', 'message' =&gt; 'Images Loading'),
            'effect'=&gt;'fade',
        ),
    );
    return $form;
}
I have discovered a sort of workaround.  It requires storing the result of current_path() in a JS setting variable, and rewriting the path in JS when the pager is regenerated.

Sample code:

php
// building an ajax form...
global $base_path;
$settings = array(
    'my_module' =&gt; array(
    'my_path' =&gt; $base_path . current_path(),
  ),
);
drupal_add_js($settings, 'setting');


That will make the current page's path available in your JavaScript file to manipulate.  There are still kinks though, especially with AJAX filtering and AJAX paging all at once.  I will update this once I resolve all those kinks.