
Disclaimer: I'm still very new to this, so this may be a stupid question. Most of my experience is in Java programming.
I need to build a module that will let me search my website with an an older external search engine which generates an RSS feed with the results. It uses URL-based queries, so it shouldn't be a big issue to implement, but this is giving me a lot more issues than it should.
Ideally, I would be able to replace the built-in drupal search to use the external search and display the results the same way they show up with the built-in search. I have a multitude of advanced search options I'll need to set up as well for full support.
Here's the module so far. I stole a lot of the code from the core Search module.
<?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 mndmf_search_help($path, $arg) {
switch ($path) {
case "admin/help#mndmf_search":
return '<p>'. t("Keeps the user on the MNDMF site after entering the search query.") .'</p>';
break;
}
}
/**
* Implements hook_permission().
*/
function mndmf_search_permission() {
return array(
'administer search' => array(
'title' => t('Administer search'),
),
'search content' => array(
'title' => t('Use search'),
),
'use advanced search' => array(
'title' => t('Use advanced search'),
),
);
}
/**
* Implements hook_block_info().
*/
function mndmf_search_block_info() {
$blocks['mndmf_search_en'] = array(
'info' => t('MNDMF Search (en)'), //The name that will appear in the block list.
'cache' => DRUPAL_NO_CACHE, //Default
);
$blocks['mndmf_search_fr'] = array(
'info' => t('MNDMF Search (fr)'), //The name that will appear in the block list.
'cache' => DRUPAL_NO_CACHE, //Default
);
return $blocks;
}
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function mndmf_search_block_view($delta = '') {
switch($delta){
case 'mndmf_search_en':
if(user_access('access content')){
$block['content'] = drupal_get_form('mndmf_search_box');
}
break;
case 'mndmf_search_fr':
if(user_access('access content')){
$block['content'] = drupal_get_form('mndmf_search_box');
}
break;
}
return $block;
}
function mndmf_search_form($form, &$form_state, $action = '', $keys = '', $prompt = NULL) {
$action = 'search/mndmf';
if (!isset($prompt)) {
$prompt = t('Enter your keywords');
}
$form['#action'] = url($action);
// Record the $action for later use in redirecting.
$form_state['action'] = $action;
$form['#attributes']['class'][] = 'search-form';
$form['module'] = array('#type' => 'value', '#value' => $module);
$form['basic'] = array('#type' => 'container', '#attributes' => array('class' => array('container-inline')));
$form['basic']['keys'] = array(
'#type' => 'textfield',
'#title' => $prompt,
'#default_value' => $keys,
'#size' => $prompt ? 40 : 20,
'#maxlength' => 255,
);
// processed_keys is used to coordinate keyword passing between other forms
// that hook into the basic search form.
$form['basic']['processed_keys'] = array('#type' => 'value', '#value' => '');
$form['basic']['submit'] = array('#type' => 'submit', '#value' => t('Search'));
return $form;
}
function mndmf_search_box($form, &$form_state) {
$form[] = array(
'#type' => 'textfield',
'#title' => t('txt'),
'#title_display' => 'invisible',
'#size' => 15,
'#default_value' => '',
'#attributes' => array('title' => t('Enter the terms you wish to search for.')),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Search'));
$form['#submit'][] = 'mndmf_search_box_form_submit';
return $form;
}
/**
* Process a block search form submission.
*/
function mndmf_search_box_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
if (isset($_GET['destination'])) {
unset($_GET['destination']);
}
if ($form_state['values']['mndmf_search_block_form'] == '') {
form_set_error('keys', t('Please enter some keywords.'));
}
$form_id = $form['form_id']['#value'];
$form_state['redirect'] = 'search/' . trim($form_state['values'][$form_id]);
}
function mndmf_search_theme() {
return array(
'mndmf_search_block_form' => array(
'render element' => 'form',
'template' => 'search-block-form',
),
'mndmf_search_result' => array(
'variables' => array('result' => NULL, 'module' => NULL),
'file' => 'search.pages.inc',
'template' => 'search-result',
),
'mndmf_search_results' => array(
'variables' => array('results' => NULL, 'module' => NULL),
'file' => 'search.pages.inc',
'template' => 'search-results',
),
);
}
The block shows up fine, but giving a search query gives me these errors:
Notice: Undefined index: mndmf_search_block_form in mndmf_search_box_form_submit() (line 133 of mndmf_search.module).
Please enter some keywords.
Notice: Undefined index: mndmf_search_box in mndmf_search_box_form_submit() (line 138 of mndmf_search.module).
I'm not completely sure how drupal works with some functions and parameters, as I'm still pretty new to this. I can't seem to understand how $form_state really works as well.
Also if someone could point me in the right direction on how to request external XML data, that would be great. I have working source in ASP, but I don't have the slightest clue how to get that to work in PHP.
Thanks in advance :)
Comments
Bump? I need a solution to
Bump? I need a solution to this problem for Monday.
Bump again...
Bump again...
The reason you're getting the
The reason you're getting the first notice is because you haven't given your form element a key in mndmf_search_box_form, $form_state['values'] is just an array of submitted values from the form keyed by the key give to the element in the original form. If you change the first line of that function to this:
The values will get passed to the submit function.
You can use drupal_http_request (http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_ht...) to request external data and then use something like the DOMDocument class to parse it to XML and work on it from there.
Hope that helps
Oh! Great! That makes sense.
Oh! Great! That makes sense. Thanks.
I'll post again if I run into any other issues later on.