Pages

Tuesday, May 24, 2011

Extend / Modify Advanced Search Form in Drupal 6

We can easily extend/ modify advanced search form by editing  " node_form_alter " function on node.module file ( modules/node/node.module ).




<?php
function node_form_alter($form_id, &$form) {
  // Advanced node search form
  if ($form_id == 'search_form' && arg(1) == 'node' && user_access('use advanced search')) {
    // Keyword boxes:
    $form['advanced'] = array(
      '#type' => 'fieldset', 
      '#title' => t('Advanced search'), 
      '#collapsible' => TRUE, 
      '#collapsed' => TRUE, 
      '#attributes' => array('class' => 'search-advanced'),
    );
    $form['advanced']['keywords'] = array(
      '#prefix' => '<div class="criterion">', 
      '#suffix' => '</div>',
    );
    $form['advanced']['keywords']['or'] = array(
      '#type' => 'textfield', 
      '#title' => t('Containing any of the words'), 
      '#size' => 30, 
      '#maxlength' => 255,
    );
    $form['advanced']['keywords']['phrase'] = array(
      '#type' => 'textfield', 
      '#title' => t('Containing the phrase'), 
      '#size' => 30, 
      '#maxlength' => 255,
    );
    $form['advanced']['keywords']['negative'] = array(
      '#type' => 'textfield', 
      '#title' => t('Containing none of the words'), 
      '#size' => 30, 
      '#maxlength' => 255,
    );

    // Taxonomy box:
    if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
      $form['advanced']['category'] = array(
        '#type' => 'select', 
        '#title' => t('Only in the category(s)'), 
        '#prefix' => '<div class="criterion">', 
        '#size' => 10, 
        '#suffix' => '</div>', 
        '#options' => $taxonomy, 
        '#multiple' => TRUE,
      );
    }

    // Node types:
    $types = array_map('check_plain', node_get_types('names'));
    $form['advanced']['type'] = array(
      '#type' => 'checkboxes', 
      '#title' => t('Only of the type(s)'), 
      '#prefix' => '<div class="criterion">', 
      '#suffix' => '</div>', 
      '#options' => $types,
    );
    $form['advanced']['submit'] = array(
      '#type' => 'submit', 
      '#value' => t('Advanced search'), 
      '#prefix' => '<div class="action">', 
      '#suffix' => '</div>',
    );

    $form['#validate']['node_search_validate'] = array();
  }
}
?>

No comments:

Post a Comment