1

i have a custom module file and i want to add the pagination but nothing is appear, might be i did it wrong way. please suggest the right direction. here is my file. form-list is the url.

<?php
function form_example_permission() {
  return array(
    'submit form_example' => array(
      'title' => t('Submit form_example'),
      'description' => t('Submit the form_example form'),
    ),
    'access form_example submissions' => array(
      'title' => t('Access form_example submissions'),
      'description' => t('Access the form_example submissions'),
    ),
     'access form_example search' => array(
      'title' => t('Access form_example search'),
      'description' => t('Access the form_example search'),
    ),
  );
}
function form_example_menu() {
  $items = array();

  $items['form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Employess Entry Form', //page title
    'description' => "A form to submit the employee's data.",
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('submit form_example'),
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('form_example_form'),

      );

 $items['form-submissions'] = array(
    'title' => t('List'),
    'page callback' => 'form_example_submissions',
    'access callback' => TRUE,
    'access arguments' => array('access form_example submissions'),
    'type' => MENU_CALLBACK,
  );

  $items['form-list'] = array(
    'title' => t('List'),
    'page callback' => 'form_example_list',
    'access callback' => TRUE,
    'access arguments' => array('access form_example search'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function form_example_form($form, &$form_state) {

$form['empcode'] = array(
  '#weight' => '0',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Code'),
);
$form['empname'] = array(
  '#weight' => '1',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Name'),
);
$form['empemail'] = array(
  '#weight' => '2',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Email'),
);
$form['empdesignation'] = array(
  '#weight' => '3',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Designation'),
);
$form['file'] = array(
  '#weight' => '4',
  '#type' => 'file',
  '#size' => '30',
  '#title' => t('Employee Photo'),
);
 $form['submit'] = array(
     '#weight' => '5',
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

return $form;

}

function form_example_form_validate($form, &$form_state) {
  /*if (!($form_state['values']['price'] > 0)){
    form_set_error('price', t('Price must be a positive number.'));
  }*/
 // print('<pre>'.print_r($form_state['values'],1).'</pre>');
  //die();
 $file = file_save_upload('file', array(
    'file_validate_is_image' => array(),
    'file_validate_extensions' => array('png gif jpg jpeg'),
  ));
  if ($file) {
    if ($file = file_move($file, 'public://')) {
      $form_state['values']['file'] = $file;
    }
    else {
      form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
    }
  }
  else {
    form_set_error('file', t('No file was uploaded.'));
  }
}

function form_example_form_submit($form, &$form_state) {
   $file=$form_state['values']['file'];
  unset($form_state['values']['file']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
 // drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));


 $fe_id = db_insert('form_example')
        ->fields(array(

          'empcode' => $form_state['values']['empcode'],
          'empname' => $form_state['values']['empname'],
          'empemail' => $form_state['values']['empemail'],
          'empdesignation' => $form_state['values']['empdesignation'],
          'empphoto' => $file->filename,
        ))
        ->execute();
        drupal_set_message(t('Successfully added'));

}
function form_example_theme($existing, $type, $theme, $path) {
  return array(
    'form_example_function' => array(
      'variables' => array('rows' => NULL),
    ),
    'form_example_template' => array(
      'variables' => array('rows' => NULL),
      'template' => 'form_example_template',
    ),


  );
}

/* view the form submissions*/
function form_example_submissions()
{
  $results =  db_query("Select * from {form_example}");
   $rows = array();
  foreach($results as $result)
  {
    $rows[] = array(
        $result->fe_id,
        $result->empcode,
        $result->empname,
        $result->empphoto,
        $result->empemail,
         $result->empdesignation,
      );

  }
  return theme('form_example_function',array('rows' => $rows));
}
/*function theme_form_example_function($variables)
{
  print_r($variables);
//$output ="<h2>".print_r$variables."</pre></h2>";
/*  foreach ($variables as $result)
  {

    $output = "<h2><pre>".print_r($result)."</pre></h2>";
  }
return $output;
 $output = '';

    foreach($variables['rows'] as $row) {
        foreach($row as $values) {

          print_r($values);
         $output .= "Empcode: " . $values . "</br> ";
         // $output .= "EmpName: " . $values[1] . " </br>";
        //$output .= "Emphot: " . $values[2] . "\n";
        }
    }
    return $output;

}*/
function theme_form_example_function($variables)
{
    $output = '';
   $search_form =drupal_get_form('form_example_search_form');
    $output = '<div class="block" >'.drupal_render($search_form).'</div>';
    $output .= '<div style="clear:both" ></div>';


   //print_r($variables);
    foreach($variables['rows'] as $row) {
      //  echo $path;
      // $output .= "Empcode: " . (isset($row[1]) ? $row[1] : "--") . " ";
      //  $output .= "EmpName: " . (isset($row[2]) ? $row[2] : "--") . " ";
      //  $output .= "Emphot: " . (isset($row[3]) ? $row[3] : "--") . "<br>";   

$img_url = 'public://'.(isset($row[3]) ? $row[3] : "--");  // the orig image uri
$style = 'medium';  // or any other custom image style you've created via /admin/config/media/image-styles



     $output .= '<div class="views-row"><div class="do_featured">
  <div style="position: relative;" class="do_image"> 
            <div class="field field-name-field-thumb-image field-type-image field-label-hidden"><div class="field-items"><div class="field-item "><img  src="'.image_style_url($style, $img_url).'" ></div></div></div></div>
  <div class="do_title">
    <span>    <div class="field"><div class="field-items"><b>'.(isset($row[2]) ? $row[2] : "--") .'</b></div></div>
 <div class="field"><div class="field-items">Emp Code: &nbsp;</div><div class="field-items">'.(isset($row[1]) ? $row[1] : "--") .'</div></div> 
 <div class="field"><div class="field-items">'.(isset($row[4]) ? $row[4] : "--") .'</div></div> 
 <div class="field"><div class="field-items">'.(isset($row[5]) ? $row[5] : "--") .'</div></div> 



      </span> 
   </div></div></div>';



    }

    return $output;
}
function form_example_list(){
    $search_form =drupal_get_form('form_example_search_form');
    $content = '<div class="block" >'.drupal_render($search_form).'</div>';
    $content .= '<div style="clear:both" ></div>';

    $query = db_select('form_example', 'pd');
    if(isset($_GET['empname']) && $_GET['empname'] != ''){
        $query->condition('pd.empname', '%' . db_like($_GET['empname']). '%','LIKE');
    }
    if(!empty($_GET['empcode'])){
        $query->condition('pd.empcode', '%' . db_like($_GET['empcode']). '%','LIKE');
    }
    if(isset($_GET['empdesignation']) && $_GET['empdesignation'] != ''){
        $query->condition('pd.empdesignation', '%' . db_like($_GET['empdesignation']). '%','LIKE');
    }
    $query->fields('pd');
     $query->extend('PagerDefault')->limit(1);
   //$query->limit(10);
    //$table_sort = $query->extend('TableSort')->orderByHeader($header);
    //$pager = $table_sort->extend('PagerDefault')->limit(10);

    $results = $query->execute();
   $rows = array();
  foreach($results as $result)
  {
    $rows[] = array(
        $result->fe_id,
        $result->empcode,
        $result->empname,
        $result->empphoto,
        $result->empemail,
         $result->empdesignation,
      );

  }
  $limit =1;
  pager_default_initialize(count($results), $limit, $element = 0);
$pager = theme('pager',array("quantity"=>5));
return theme("form_example_function",array("rows"=>$rows ,"pager"=>$pager));
//  return theme('form_example_function',array('rows' => $rows));
 //return $content;
}
function form_example_search_form($form, &$form_state){

    $form['#method'] = 'get';
    $form['#token'] = FALSE;
    $form['#form_build_id'] = FALSE;
    $form['#form_id'] = FALSE;
    $form['empname'] = array(
        '#type' => 'textfield',
        '#title' => t('empname'),
        '#attributes' => array( 'maxlength' => '128', 'size'=> '30'),
        '#prefix' => '<div class="form-inline views-exposed-form"> <div class="views-exposed-widget">',
        '#suffix' => '</div>',
        '#default_value' => isset($_GET['empname'])?$_GET['empname']:'',
    );
    $form['empcode'] = array(
        '#type' => 'textfield',
        '#title' => t('empcode'),
        '#attributes' => array( 'maxlength' => '128', 'size'=> '30'),
        '#prefix' => '<div class="form-inline views-exposed-form"> <div class="views-exposed-widget">',
        '#suffix' => '</div>',
        '#default_value' => isset($_GET['empcode'])?$_GET['empcode']:'',
    );
    $form['empdesignation'] = array(
        '#type' => 'textfield',
        '#title' => t('empdesignation'),
        '#attributes' => array( 'maxlength' => '128', 'size'=> '30'),
        '#prefix' => '<div class="form-inline views-exposed-form"> <div class="views-exposed-widget">',
        '#suffix' => '</div>',
        '#default_value' => isset($_GET['empdesignation'])?$_GET['empdesignation']:'',
    );
    //views-exposed-widget

    $form['submit'] = array(
        '#type' => 'submit',
        '#attributes' => array( 'name' => ''),
        '#prefix' => '<div class="views-exposed-widget">',
        '#suffix' => '</div></div>',
        '#value' => 'Apply',
     );  

    return $form;
}
?>

2 Answers 2

2

Issue seems to be that you are not attaching your global pager output with rest of content.

Checkout the following code, slightly different but covers up the way to print the pager for a custom menu page by querying certain table from database.

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

  $items['test-dev-1'] = array(
    'title' => t('List'),
    'page callback' => 'form_example_list',
    'access callback' => TRUE,
    'access arguments' => array('access form_example search'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Menu callback.
 */
function form_example_list() {
  $query = db_select('node', 'n')->extend('PagerDefault');
  $query->fields('n', array('nid', 'title'))
      ->limit(1)
      ->addTag('node_access')
      ->groupBy('n.nid');
  $results = $query->execute()->fetchAllKeyed(0, 1);
  $pager = theme('pager', array("quantity" => 5));
  $output = theme('item_list', array('items' => $results));
  $output = $output . $pager;
  return $output;
}

Instead of sending the pager as argument to your theme function, just concatenate the output of theme_pager and your custom theme function(in above example I am using theme_item_list).

  $output = $output . $pager;
3
  • its didn't work for me i add the pager in custom theme function but it didn't show the pagination on page
    – user67248
    Sep 26, 2016 at 11:23
  • all you need to make sure is you are concatenating the output of both theme functions, I would recommend to give a try to above code, and thena access test-dev-1, you will see the paginated list of nodes, the same thing can be done in your case.
    – arpitr
    Sep 26, 2016 at 11:26
  • Not working i used above code
    – user67248
    Sep 27, 2016 at 4:44
0
$query = db_select('form_example', 'fe')->extend('PagerDefault');
$query->limit(5); // adjust to the number to show per page
$query->fields('fe', array('fe_id', 'empcode', 'empname', 'empphoto', 'empmail', 'empdesignation'));
$results = $query->execute();

After this you can output the pager wherever you want with:

theme('pager');

Or as part of a render array:

$element['pager'] = array
(
  '#theme' => 'pager',
);
10
  • i have a custom theme how can i output with pager i did like $limit =1; pager_default_initialize(count($results), $limit, $element = 0); $pager = theme('pager',array("quantity"=>5)); return theme("form_example_function",array("rows"=>$rows ,"pager"=>$pager));
    – user67248
    Sep 26, 2016 at 10:27
  • in query i did like $query->fields('pd'); $query->extend('PagerDefault')->limit(1);
    – user67248
    Sep 26, 2016 at 10:29
  • I'm not sure if you are asking a question or telling a solution.
    – Jaypan
    Sep 26, 2016 at 10:31
  • i am telling you i did like above how can i render the theme pager with custom view theme
    – user67248
    Sep 26, 2016 at 10:32
  • is that a correct way please check $pager = array ( '#theme' => 'pager', ); return theme("form_example_function",array("rows"=>$rows ,"pager"=>$pager));
    – user67248
    Sep 26, 2016 at 10:37

Your Answer

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