0

I'm writing a custom Drupal 7 module which will completely override the search page and search method for a website. Here is what I have so far:

/**
 * Custom search.
 */
function mymodule_search_page() {
  drupal_add_css('css/search.css');

  // Perform a search (not important how)
  $result = do_custom_search('foo');

  return '<p>Results:</p>';
}

Now, as you can see, it's not complete. I don't know how to properly return structured HTML from this. How would I go about using Drupal's built-in template system to render the results?

damd
  • 6,116
  • 7
  • 48
  • 77
  • Why 'overriding' the search completely when you can plug your own search logic? http://api.drupal.org/api/drupal/7/search/hook_search – Pierre Buyle Mar 27 '13 at 16:16

2 Answers2

2

you have to make use of drupal inbuilt functions. i hope you are looking for something like this http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_render/7

Soni Kishan
  • 488
  • 2
  • 8
  • Looks about right... But could you show me an example on how to use `drupal_render` in my case? I'm not sure whether I should combine it with theme template overrides or what content my arrays should have. – damd Mar 26 '13 at 10:56
  • do u have any prior knowledge of drupal coding ? if not i would prefer u to go through this http://drupal.org/developing/modules – Soni Kishan Mar 26 '13 at 11:04
  • Yes, I have made a couple of smaller websites, but I have never made a custom Drupal module. I'm also relatively new to Drupal 7, I've only used 6 before. – damd Mar 26 '13 at 11:07
0

This is what I ended up doing:

/**
 * Implements hook_menu().
 */
function mymodule_search_menu() {
  $items = array();
  $items['search'] = array('page callback' => 'mymodule_search_page',
                       'access callback' => TRUE);
  return $items;
}

/**
 * Mymodule search page callback.
 */
function mymodule_search_page() {
  $variables = array();

  // Add stuff to $variables.  This is the "context" of the file,
  // e.g. if you add "foo" => "bar", variable $foo will have value
  // "bar".
  ...

  // This works together with `mymodule_search_theme'.
  return theme('mymodule_search_foo', $variables);
}

/**
 * Idea stolen from: http://api.drupal.org/comment/26824#comment-26824
 *
 * This will use the template file custompage.tpl.php in the same
 * directory as this file.
 */
function mymodule_search_theme() {
  return array ('mymodule_search_foo' =>
                array('template' => 'custompage',
                      'arguments' => array()));
}

Hope this helps someone!

damd
  • 6,116
  • 7
  • 48
  • 77