Hi!
I need to search terms by keywords, but only in one vocab - so i wrote the taxonomy_search() - function.
/**
* Implementation of hook_search().
*/
function taxonomy_search($op = 'search', $keys = NULL, $vid=NULL) {
switch ($op) {
case 'name':
{
return t('Taxonomy');
}
case 'search':
{
$find = array();
if (!$vid){
$db_result = db_query(db_rewrite_sql("SELECT t.tid, t.* FROM {term_data} t WHERE LOWER(t.name) LIKE LOWER('%%%s%%')", 't', 'tid'), trim($keys));
}
else {
$db_result = db_query(db_rewrite_sql("SELECT t.tid, t.* FROM {term_data} t WHERE LOWER(t.name) LIKE LOWER('%%%s%%') AND t.vid=%d", 't', 'tid'), trim($keys), $vid);
}
while ($term = db_fetch_object($db_result))
{ $find[] = array('tid'=>$term->tid, 'title'=>$term->name, 'link'=>'/taxonomy/term/'.$term->tid);}
return $find;
}
}
}
Although i added some code to taxonomy_form_alter() to support the search-module
if ($form_id == 'search_form' && $form['module']['#value'] == 'taxonomy') {
$vocabs=taxonomy_get_vocabularies();
$vop=array();
foreach($vocabs as $voc)
{
$vop[$voc->vid]= $voc->name;
}
$form['vocabulary'] = array(
'#type' => 'checkboxes',
'#title' => t('In the vocabulary'),
'#prefix' => '<div class="criterion">',
'#size' => 10,
'#suffix' => '</div>',
'#options' => $vop,
'#multiple' => TRUE,
'#name' => 'vid',
);
}
----------------------------------------
So, now i have my own page-node with embedded PHP-Code. It is a form, that displays a autocomplete-Field for one Vocabulary; goal is to show the results!
function verein_form($form_state) {
$form['verein'] = array(
'#type' => 'textfield',
'#title' => t('Verein'),
'#autocomplete_path' => 'taxonomy/auto23complete/4',
'#maxlength' => 255,
'#attributes' => array('title' => t('Enter the terms you wish to search for.')),
'#default_value' => '',
'#attributes' => array('title' => t('Enter the terms you wish to search for.')),
);
$form['taxonomy']= array(
'#value' => 'taxonomy',
'#type' => 'hidden',
'#name' => 'type[taxonomy]',
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Search'));
// $form['#submit'][] = 'search_box_form_submit';
$form['#submit'][] = 'verein_form_submit';
$form['#validate'][] = 'search_box_form_validate';
return $form;
}
if i coose $form['#submit'][] = 'search_box_form_submit'; it will redirect to the /search/taxonomy/.$keys - page and search for my keywords.
But the issues:
- There is no "Taxonomy"-Button next to "Content" and "Users" - the search seems not to be registered.
- How do i add the criterion $vid?
--------------------
Another aproach is to integrate the results into my Page - I would prefer that. So i wrote a own form_submit-function, to redirect the User to the Term-Page, if there is only one result
function verein_form_submit($form, &$form_state) {
$vereine=taxonomy_search('search', $form_state['values']['verein'], 4);
if (count($vereine)==1) {
drupal_goto("verein/".$vereine[0]['title']);
}
else
{
// drupal_goto("search/taxonomy/".$form_state['values']['verein']); //This does not submit the criterion "vid"
// drupal_goto("vereine/".$form_state['values']['verein']); // This ends up in an 404-Error
//print_r(vereine); // does not work!
}
}
I cannot echo or print from within verein_form_submit - how do i theme or anything else the output?
So pleaaase - how do i add the results to my page?
do i have to create a view? Can i use Wildcards with the Arguments?
Thank You for Your help!