Drupal: creating custom search

I am trying to create a custom search but am stuck. I want to have a dropdownbox so that the user can choose where to look. These parameters can represent one or more types of content.

So, if he chooses options A, the search will look in node-type P, Q, R. But he cannot give these results, but only the uid, which will then be designed to collect specific data for that user.

To make it a little clearer, let's say I want people to be able to. I am looking in 2 types of content profile. But of course you don't want to display them in the result, but a nice user picture and some data.

I started by creating a form with a textbox and a dropdown. Then, in the handler, I created the keys and redirected to other pages with those keys as a tail. This page has been defined in the menu, just like the search.

After that, I want to call hook_view

to do the actual search by calling node_search

and will return the results.

Unfortunately, things are not going well. When I click the Search button, it gives me a 404.

But am I on the right track? Is this a way to create a custom search?

thanks for your help.

Here's the code for some clarity:

<?php
// $Id$

/*
 * @file
 * Searches on Project, Person, Portfolio or Group.
 */

/**
 * returns an array of menu items
 * @return array of menu items
 */
function vm_search_menu() {

  $subjects = _vm_search_get_subjects();
  foreach ($subjects as $name => $description) {
    $items['zoek/'. $name .'/%menu_tail'] = array(
      'page callback' => 'vm_search_view',
      'page arguments' => array($name),
      'type' => MENU_LOCAL_TASK,
    );
  }
  return $items;
}


/**
 * create a block to put the form into.
 * @param $op
 * @param $delta
 * @param $edit
 * @return mixed
 */
function vm_search_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Algemene zoek');
      return $blocks;
    case 'view':
        if (0 == $delta) {
          $block['subject'] = t('');
          $block['content'] = drupal_get_form('vm_search_general_form');
        }
      return $block;
    }
}

/**
  * Define the form.
  */
function vm_search_general_form() {
  $subjects = _vm_search_get_subjects();
  foreach ($subjects as $key => $subject) {
    $options[$key] = $subject['desc'];
  }

    $form['subjects'] = array(
        '#type' => 'select',
    '#options' => $options,
    '#required' => TRUE,
    );
  $form['keys'] = array(
    '#type' => 'textfield',
    '#required' => TRUE,
  );
   $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Zoek'),
   );
   return $form;
}


function vm_search_general_form_submit($form, &$form_state) {
  $subjects = _vm_search_get_subjects();
  $keys = $form_state['values']['keys']; //the search keys
  //the content types to search in
  $keys .= ' type:' . implode(',', $subjects[$form_state['values']['subjects']]['types']);



  //redirect to the page, where vm_search_view will handle the actual search
  $form_state['redirect'] = 'zoek/'. $form_state['values']['subjects'] .'/'. $keys;
}


/**
 * Menu callback; presents the search results.
 */
function vm_search_view($type = 'node') {
  // Search form submits with POST but redirects to GET. This way we can keep
  // the search query URL clean as a whistle:
  // search/type/keyword+keyword
  if (!isset($_POST['form_id'])) {
    if ($type == '') {
      // Note: search/node can not be a default tab because it would take on the
      // path of its parent (search). It would prevent remembering keywords when
      // switching tabs. This is why we drupal_goto to it from the parent instead.
      drupal_goto($front_page);
    }

    $keys = search_get_keys();
    // Only perform search if there is non-whitespace search term:
    $results = '';
    if (trim($keys)) {
      // Log the search keys:
      watchdog('vm_search', '%keys (@type).', array('%keys' => $keys, '@type' => $type));

      // Collect the search results:
      $results = node_search('search', $type);

      if ($results) {
        $results = theme('box', t('Zoek resultaten'), $results);
      }
      else {
        $results = theme('box', t('Je zoek heeft geen resultaten opgeleverd.'));
      }
    }
  }
  return $results;
}


/**
 * returns array where to look for
 * @return array
 */
function _vm_search_get_subjects() {
  $subjects['opdracht'] =
    array('desc' => t('Opdracht'),
          'types' => array('project')
          );
  $subjects['persoon'] =
        array('desc' => t('Persoon'),
          'types' => array('types_specialisatie', 'smaak_en_interesses')
          );
  $subjects['groep'] =
    array('desc' => t('Groep'),
        'types' => array('Villamedia_groep')
        );
  $subjects['portfolio'] =
    array('desc' => t('Portfolio'),
          'types' => array('artikel')
          );
   return $subjects;
}

      

+2


source to share


2 answers


To be honest, I have not seen many people use hook_search. Just use Views , or for more advanced things, something like Faceted Search .



Are you considering using either for your current project? Why didn't it work?

0


source


you can also use a combination of hook_menu for your results and db_queries with your custom (and optimized so fast) queries.

For example:

Search/%/%

where the arguments can be whatever you want, for example, the first for the lowest price, the second for the highest price, the third for the minimum bedrooms ... Your url will look like this:

search / 200/400 / null / 3 / ...



I used null, but that may be whatever you prefer to think of this field as empty.

Then, from your selected form, you should simply redirect to the following structure of that url and add the parameters where you want.

It's not the prettiest way to build a URL, but using this technique and hook_theme will give you unlimited flexibility. I can show you a project where we are using this technique and I think it looks pretty good :-).

Any comments regarding this would be greatly appreciated :-).

0


source







All Articles