1

I am building a simple custom module to search a certain node type and displaying it in the same page as the form. It is like the search function. The problem is how to display the result below the form with paging-teaser node style?

The code is:

<?php
/**
 * 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 book_search_help($path, $arg) {
  switch ($path) {
    case "admin/help#book_search":
      return '<p>' . t("Displays book search page") . '</p>';
      break;
  }
}

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

  $items['book/search'] = array(
    'title' => 'Pencarian book',
    'description' => 'Halaman pencarian book',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('book_search_form'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

function book_search_form($form, &$form_state) {
$form['cari'] = array(
    '#type' => 'fieldset',
    //'#title' => t('Pencarian'),
    //'#attributes' => array('class' => array('container-inline')), 
    '#tree' => TRUE,
  );

 $form['cari']['pilihan'] = array(
    '#type' => 'select',
    '#title' => t('Pilihan Pencarian'),
    '#options' => array(
      '1' => t('Option 1'),
      '2' => t('Option 2'),
      '3' => t('Option 3'),
      '4' => t('Option 4'),
      '5' => t('Option 5'),
      '6' => t('Option 6'),
      '7' => t('Option 7'),
      '8' => t('Option 8'),
    ),
    '#attributes' => array('class' => array('container-inline')), 
    '#default_value' => $category['umum'],
//    '#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
  );

 $form['cari']['kata'] = array(
  '#type' => 'textfield',
  '#title' => t('Kata Kunci'),
  '#size' => 50,
  '#maxlength' => 256,
 );

  $form['cari']['submit'] = array('#type' => 'submit', '#value' => t('Cari'),'#submit' => array('book_search_submit'),);

  if (isset ($form_state['hasil'])){
    print render(node_view_multiple($form_state['hasil'], 'teaser')) . theme('pager');
  }

  return $form;
}

function book_search_submit($form, &$form_state) {


  //db_query("INSERT INTO {table} (name, log, hidden) VALUES ('%s', %d, '%s')", $form_state['values']['name'], $form_state['values']['access']['log'],  $form_state['values']['hidden']);
  $query = new EntityFieldQuery;
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'mytype')
    ->pager(20)
    ->propertyCondition('status', 1); // in case you need it
    // ->fieldCondition('field_customfield3', 'value', $user->uid)
    // ->fieldCondition('field_customfield4', 'value', $anemailaddress);
  $results = $query->execute();

  if (isset($results['node'])) {
  $news_items_nids = array_keys($results['node']);
  $news_items = entity_load('node', $news_items_nids);
  }

 drupal_set_message(t('Your form has been submitted.'));

  $nodes = node_load_multiple(array_keys($results['node'])); 
  $form_state['hasil'] = $nodes;
//  print render(node_view_multiple($nodes, 'teaser')) . theme('pager');
  $form_state['rebuild'] = TRUE;
}

I modified the code so it can dynamically building queries according to selection options. The trick to display the result is by appending the result html in the back of the form code using $form['#suffix'] = $html;. It resulting inconsistency in the result paging. Is there any way to improve it or make it right?

Modified Code:

function book_search_help($path, $arg) {
  switch ($path) {
    case "admin/help#book_search":
      return '<p>' . t("Displays book search page") . '</p>';
      break;
  }
}

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

  $items['book/search'] = array(
    'title' => 'Pencarian book',
    'description' => 'Halaman pencarian book',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('book_search_form'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

function book_search_form($form, &$form_state) {
$form['cari'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
  );

 $form['cari']['pilihan'] = array(
    '#type' => 'select',
    '#title' => t('Pilihan Pencarian'),
    '#options' => array(
      '1' => t('1'),
      '2' => t('2'),
      '3' => t('3'),
      '4' => t('4'),
      '5' => t('5'),
      '6' => t('6'),
      '7' => t('7'),
      '8' => t('8'),
    ),
    '#attributes' => array('class' => array('container-inline')), 
    '#default_value' => $category['umum'],
  );

 $form['cari']['kata'] = array(
  '#type' => 'textfield',
  '#title' => t('Kata Kunci'),
  '#size' => 50,
  '#maxlength' => 256,
 );

  $form['cari']['submit'] = array('#type' => 'submit', '#value' => t('Cari'),'#submit' => array('book_search_submit'),);

  if (isset($_SESSION['hasil'])){
    $html = drupal_render(node_view_multiple($_SESSION['hasil'], 'teaser')) . theme('pager');
    $form['#suffix'] = $html;
  }

  return $form;
}

function book_search_submit($form, &$form_state) {


  $query = new EntityFieldQuery;
  $query->entityCondition('entity_type', 'node');
  switch($form_state['values']['cari']['pilihan']){
    case '1':
    $query->entityCondition('bundle', array('montype','book'), 'IN')
    ->fieldCondition('body', 'value', $form_state['values']['cari']['kata'], 'CONTAINS');
    break;

    case '2':
    $query->entityCondition('bundle', 'montype')
    ->fieldCondition('body', 'value', $form_state['values']['cari']['kata'], 'CONTAINS');
    break;

    case '3':
    $query->entityCondition('bundle', 'montype')
    ->fieldCondition('field_dosis', 'value', $form_state['values']['cari']['kata'], 'CONTAINS');
    break;

    case '4':
    $query->entityCondition('bundle', 'montype')
    ->fieldCondition('field_efek_samping', 'value', $form_state['values']['cari']['kata'], 'CONTAINS');
    break;

    case '5':
    $query->entityCondition('bundle', 'montype')
    ->fieldCondition('field_peringatan', 'value', $form_state['values']['cari']['kata'], 'CONTAINS');
    break;

    case '6':
    $query->entityCondition('bundle', 'montype')
    ->fieldCondition('field_interaksi', 'value', $form_state['values']['cari']['kata'], 'CONTAINS');
    break;

    case '7':
    $query->entityCondition('bundle', 'montype')
    ->fieldCondition('field_kontraindikasi', 'value', $form_state['values']['cari']['kata'], 'CONTAINS');
    break;

    case '8':
    $query->entityCondition('bundle', 'montype')
    ->propertyCondition('title', $form_state['values']['cari']['kata'], 'CONTAINS');
    break;

    default:
    break;
  }
    $query->pager(20)
    ->propertyCondition('status', 1); // in case you need it


  $results = $query->execute();


$_SESSION['hasil']  = node_load_multiple(array_keys($results['node']));
  $form_state['rebuild'] = TRUE;
 drupal_set_message(t('Your form has been submitted.'));
}
  • So what is the effect this code shows now? Where exactly you stuck? – Mołot Nov 5 '14 at 16:06
  • The result is rendered immediately. Before layout/form is rendered. I wonder where to put these lines of code: if (isset ($form_state['hasil'])){ print render(node_view_multiple($form_state['hasil'], 'teaser')) . theme('pager'); } – Mohammad Irfan Nov 5 '14 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.