Posted by vistree on October 26, 2008 at 5:40pm
Jump to:
| Project: | Apache Solr Search Integration |
| Version: | 7.x-1.x-dev |
| Component: | User interface |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs work |
Issue Summary
Hi, how can I create a search block like drupals core search block?
Or is it possible to use the core to run Solr-quries?
Regards
vistree
Comments
#1
whether you use the core search block or create a new one, you'll want to set the action of the form to be search/apachesolr_search. The action of the core form is search/node. This means you can probably use form_alter to change the action on that form. Or you can create a new search form in the block.
I'm marking this as a feature request and changing the title because I think the module should either ship with its own block, or hijack the existing one.
#2
That is great. Thank you for reply.
Where can I change the behaviour? In the template.php?
And can you tell me the correct code for this?
Thank your very much!
vistree
#3
Your own custom module is the place where you would implement hook_form_alter. I don't have the exact code for this but you can get a start by reading the API docs: http://api.drupal.org/api/function/hook_form_alter/6
#4
Hey Robert,
Didn't you have me and my dad put this functionality in search_config a couple months ago? (where is is now committed).
Best,
Jacob
#5
Thank you for the link.
My problem is, that I am no programmer and don't know how to create my own module.
I thought that it would be much more easier to generate my own Solr search block, e.g. by adding a "print ... " in a newly created block.
Is there some more information on how to create such a module or block? Or maybe an already existing example?
Regards
vistree
#6
@jacobsingh - I believe you are correct. current dev 5.x-1.4 of search_config module contains that patch (http://drupal.org/project/search_config )
@vistree - after enabling the Search Config module, go to admin/settings/search (Site Configuration | Search Settings). Under "Advanced Search Confuration" you will see "Default Search" which lists of all modules that hook into search. Whichever one you select will cause the Drupal search box to take you to that results tab.
Hope this is what you are looking for.
#7
Yes, that is what I am searching for ;-)
Unfortunately I work with Drupal 6 - where is currently no version of the mentioned module above ;-(
Is there an alternative?
Regards
vistree
#8
Well, not sure if anyone is interested in a followup with this but here's what I did. Dead simple:
Created a block, PHP input filter, with the following code:
<form class="search-form" id="search-form" method="post" accept-charset="UTF-8" action="/path_to_site/search/apachesolr_search/"><input type="text" class="form-text" value="" id="edit-keys-header" name="keys" maxlength="255"/>
<input id="edit-submit" class="form-submit" type="submit" value="Search" />
</form>
Good to go. Substitute the path to your install in the form action.
Hope that helps.
#9
This is great and works well. It is what I was still looking for - thank you!!
Regards
vistree
#10
This is still valid as a feature request since it would be useful if the module provided its own search block.
#11
@David Lesieur - we are planning code to decouple this module from it's dependence on core search, in which case we will certainly provide a separate search block.
#12
I think we should suggest an interim solution [Apache Search Block] in module Documentation.
#13
The way the search_theme_form ($search_box variable in the page.tpl.php file) actually goes to the search page is through a redirect in the submission, so we have to add a submission, then redirect it. (May be a more efficient way, but this works)
<?php
/**
* Implementation of hook_form_alter()
*/
function custommodule_form_alter(&$form, $form_state, $form_id) {
// Search Form (top right), change action to apachesolr
if ($form_id == 'search_theme_form') {
// We need to add a submit, because the search form redirects to a specific place
$form['#submit'][] = 'custommodule_search_submit';
}
}
/**
* Implementation of form submit function
*/
function custommodule_search_submit($form, &$form_state) {
$form_id = $form['form_id']['#value'];
$form_state['redirect'] = 'search/apachesolr_search/'. trim($form_state['values'][$form_id]);
}
?>
#14
see: http://drupal.org/node/405206
#15
Code from #13 doesn't work for me. Using Drupal 6.13 and solr 6.x-dev 12th of august.
While custommodule_form_alter() is called, the custommodule_search_submit() is not... So is $form['#submit'][] correct?
P.S. I changed back from duplicate, because it hasnt been committed to the august 12th dev version either. And nowhere is the real solution patched.
#16
I agree that it's not a duplicate, and it's one of the tasks that we need to handle to totally decouple from search.module. But there is no patch, it's an active issue, and it is for 6.2.
#17
I have "Make Apache Solr Search the default:" set to Enable, which is supposed to redirect the Search Block. But it doesn't work... Only for normal form on /search page
#18
#19
#20
Solution found! It's an incompatibility with search_config.module! They do this:
function search_config_form_submit($form, &$form_state) {$form_id = $form['form_id']['#value'];
$type = $form['module']['#value'];
$form_state['redirect'] = 'search/apachesolr_search/'. trim($form_state['values'][$form_id]);
}
Which overrides apache_solr's redirect... I'm just going to deactivate this module. Rather use Solr at all than have this extra.
#21
subscribing
#22
I think this is solved? Closing out
#23
[edited to add a final sippet at bottom]
I wanted this and started out using the solution in #8, but I wanted to keep the spelling suggestions and retain filters functionality in the block form. I thought I'd post my little solution here.
1) create a block using php with the snippet:
<?php$keys = search_get_keys();
return drupal_get_form('search_form', NULL, $keys, NULL);
?>
2) in your .module file, put a function that recapitulates the apachesolr_search_form_search_form_alter code minus the first conditional statement that selects only forms with ['module']['#value'] == 'apachesolr_search'. Like this:
<?php
/**
* recreate the apachesolr search form alter function for use in creating a search form block
* we have to do it this way instead of using module_invoke, because module invoke does not pass by reference
* and the apachesolr function checks for ['module']['#value'] == apachesolr_search.
* @param $form
* @param $form_state
* @return unknown_type
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
if (($form_id == 'search_form') && ($form['#action'] == '/search/')) { //select only 'normal' search forms
$form['#action'] = '/search/apachesolr_search';
$form['#submit'] = array('apachesolr_search_form_search_submit');
// No other modification make sense unless a query is active.
// Note - this means that the query must always be run before
// calling drupal_get_form('search_form').
$apachesolr_has_searched = apachesolr_has_searched();
$queryvalues = array();
if ($apachesolr_has_searched) {
$query = apachesolr_current_query();
$queryvalues = $query->get_url_queryvalues();
}
$form['basic']['apachesolr_search']['#tree'] = TRUE;
$form['basic']['apachesolr_search']['queryvalues'] = array(
'#type' => 'hidden',
'#default_value' => serialize($queryvalues),
);
$form['basic']['apachesolr_search']['get'] = array(
'#type' => 'hidden',
'#default_value' => serialize(array_diff_key($_GET, array('q' => 1, 'page' => 1, 'filters' => 1, 'solrsort' => 1, 'retain-filters' => 1))),
);
if ($queryvalues || isset($form_state['post']['apachesolr_search']['retain-filters'])) {
$form['basic']['apachesolr_search']['retain-filters'] = array(
'#type' => 'checkbox',
'#title' => t('Retain current filters'),
'#default_value' => (int) isset($_GET['retain-filters']),
);
}
if (variable_get('apachesolr_search_spellcheck', TRUE) && $apachesolr_has_searched && ($response = apachesolr_static_response_cache())) {
// Get spellchecker suggestions into an array.
if (isset($response->spellcheck->suggestions) && $response->spellcheck->suggestions) {
$suggestions = get_object_vars($response->spellcheck->suggestions);
if ($suggestions) {
// Get the original query and replace words.
$query = apachesolr_current_query();
foreach ($suggestions as $word => $value) {
$replacements[$word] = $value->suggestion[0];
}
$new_keywords = strtr($query->get_query_basic(), $replacements);
$query_get = array();
if (isset($_GET['retain-filters'])) {
$query_get = $queryvalues;
$query_get['retain-filters'] = 1;
}
// Show only if suggestion is different than current query.
if ($query->get_query_basic() != $new_keywords) {
$form['basic']['suggestion'] = array(
'#prefix' => '<div class="spelling-suggestions">',
'#suffix' => '</div>',
'#type' => 'item',
'#title' => t('Did you mean'),
'#value' => l($new_keywords, $query->get_path($new_keywords), array('query' => $query_get)),
);
}
}
}
}
}
}
?>
Please note the second line which is added to set the action to search/apachesolr_search.
Also note the added snippet:
<?php$query_get = array();
if (isset($_GET['retain-filters'])) {
$query_get = $queryvalues;
$query_get['retain-filters'] = 1;
}
?>
and the line:
<?php'#value' => l($new_keywords, $query->get_path($new_keywords), array('query' => $query_get)),
?>
which keep the filters and the status of retain-filters=1 when the checkbox is ticked.
Cheers.
#24
I followed #23but i get sent to: search/node/Name the module mymodule
#25
The solution #23 can be simplified if you use
drupal_get_form('search_form', NULL, $keys, 'apachesolr_search');
This will populate ['module']['#value'] with the value apachesolr_search. This means that the default apache solr submit handler will be used!
#26
Just a note to others who might not get this right away - the simplification suggested in comment #25 refers to step 1. in #23 above. I was poking around in the suggested module code for a bit before I realized this...
#27
Hi, I have a similar need which is not redirecting a form the this page: search/apachesolr_search. I recently installed apache solr autocomplete and I would like to just paste this form: search/apachesolr_search on block. Can someone please help deploy this?
#28
Post #11 is very good news, do we know when this will happen? Having a an ApacheSolr search block would be the answer to my prayers!
#29
2x to #28. Also, I'd love to see a search block that can limit search to contexts and return results to any arbitrary url. I'm thinking right now of a block for og search that returns results from within a group context to the group page/search. Is that sort of flexibility feasible?
#30
sub
#31
So um, will there be a 6.x-2.x release ever? Or at the very least a release of a 6.x-2.x-beta module?
As it stands, this is a problem with 6.x-1.x and there are no other releases available unless you expect people to fetch them using git. In which case I don't think you'll get many testers.
#32
cafuego - The 6.2 will be phased out. We are stabilizing the D7 version first so we can backport all of the functionalities for D7 to D6 version. This will be 6.x-3.x release and will be started in this month and hopefully it will see it's completion soon.
#33
Ok. I should note btw that in 6.x-2.x-beta5 the search block doesn't seem to be using solr, but instead still defaults to the core node search. I've added a submit redirect in a custom module to work around that for the moment.
#34
The 6.x-2.x version has been deprecated. Moving this to 6.x-1.x-dev
#35
Not sure if this is already resolved in the 7.x-1.x branch before backporting this to 6