Wednesday, December 7, 2011

Create Custom Search Criteria Page in Drupal7


function my_module_menu(){
  $items = array();

  $items['my_search_page'] = array(
    'title' => t('my search screen'),
    'description' => 'when the present views filter can not suit your need such as dependent dropdown',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_search_page'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content')
  );

  return $items;
}


function my_search_page($form, &$form_state) {

  if (!isset($form_state['storage']['my_module']['search_criteria'])) {
    return drupal_get_form('my_search_form');
  }

  $build = array();
  $rows = array();

  $header = array(
    array('data' => 'Title', 'field' => 'title', 'sort' => 'asc'),
    array('data' => 'Node ID', 'field' => 'nid'),
    array('data' => 'Type', 'field' => 'type'),
    array('data' => 'Created', 'field' => 'created'),
    array('data' => 'Published'),
    );

  $title = $form_state['input']['title'];

  $query = db_select('node', 'n')
            ->condition('status', 1)  //Only published nodes, change condition as it suits you
            ->condition('title', $title.'%', 'LIKE')
            ->extend('PagerDefault')  //Pager Extender
            ->limit(10)               //10 results per page
            ->extend('TableSort')     //Sorting Extender
            ->orderByHeader($header)  //Field to sort on is picked from $header
            ->fields ('n', array (
              'nid',
              'title',
              'type',
              'created',
              'status',
            ));

  $results = $query->execute();

  foreach ($results as $node) {
    $rows[] = array(
                l($node->title, 'node/'. $node->nid),
                $node->nid,
                $node->type,
                format_date($node->created),
                $node->status
              );
  }

  //Theme the html table: http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_table/7
  $content = theme('table',
            array(
              'header' => $header,
              'rows' => $rows,
              'sticky' => TRUE,                  //Optional to indicate whether the table headers should be sticky
              'empty' => 'Find not found..',    //Optional empty text for the table if resultset is empty
            )
          );

  $build['content'] = array(
      'dummy1' => my_search_form($form, $form_state),
      'dummy2' => array('#markup' => $content,),
  );

  $build['pager'] = array(
      '#theme' => 'pager',
      '#weight' => 5,
  );

  return $build;
}


function my_search_form($form, &$form_state) {

  $form['criteria'] = array(
    '#type' => 'fieldset',
    '#title' => 'Search Criteria',
    '#attributes' => array('class' => array('container-inline')),
  );

  $form['criteria']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('title'),
    '#value' => (isset($form_state['input']['title'])) ? $form_state['input']['title'] : "",
    '#size' => 10,
    '#maxlength' => 20,
  );

  $form['criteria']['submit'] = array(
    '#type' => 'submit',
    '#value' => 'search',
    '#submit' => array('my_search_form_submit'),
  );

  return $form;
}

function my_search_form_submit($form, &$form_state) {
  $form_state['storage']['my_module']['search_criteria'] = $form_state['values'];
  $form_state['rebuild'] = TRUE;
}



Thanks:
=======
Merge Search Criteria and search result in one page
http://drupal.org/node/1074920#comment-4529954

Create sortable and sticky-headers Table
http://www.rahulsingla.com/blog/2011/05/drupal-7-creating-drupal-style-tables-with-paging-sorting-and-sticky-headers

Multistep form
http://drupal.org/node/717750 + http://drupal.org/project/examples

Horizontal form elements
http://drupal.stackexchange.com/questions/7954/horizontal-form-elements


1 comment:

  1. nice write up however there is an issue:

    When you are using the pager links the form_state values are lost.

    ReplyDelete