2
votes

I'm sure I'm not the first one to try to address this, but Google isn't doing me any good.

If you use the Advanced Search in Drupal to filter on taxonomy terms, the search form comes back with the term IDs in the keyword textbox like so:

search phrase category:32,33

The chosen values are NOT selected again in the taxonomy select box.

Instead of showing them in the keyword textbox, I would like them to be selected in the taxonomy select box - the way that any user would expect a form like this to act. I have been looking for a module that will add this functionality, to no avail. I have tried implementing hook_form_alter() to set the #default_value on that form element based on the previous form submission (available in the $form_state arg), but a) this seems kludgy and b) it seems that this function is called once to validate the form and again to re-display it, and the submitted value isn't available the second time (which is when it's needed).

Any suggestions?

2
Isn't this really a bug?vfclists

2 Answers

3
votes

It took much longer than it should have, but I finally figured out how to do this. I may release a module on the Drupal site later on, but in case anyone else has this same problem, this was the solution I came to.

Create a module and use hook_form_alter() to modify the form. I already had a module I was using to customize the advanced search, so I put it in there. I won't get into the detail of building your own module - you can find a simple tutorial for that, and you only need to define this one function.

/**
 * Implementation of hook_form_alter().
 * Remove 'category:x,y,z' from the keyword textbox and select them in the taxonomy terms list
 */
function modulename_form_alter(&$form, $form_state, $form_id) {
    // Advanced node search form
    if ($form_id == 'search_form' && $form['module']['#value'] == 'node' && user_access('use advanced search')) {
        // Remove category:x,y,z from the keyword box
        $searchPhrase = $form['basic']['inline']['keys']['#default_value'];
        if(!empty($searchPhrase) && strpos($searchPhrase, 'category:') !== false) {
            $searchWords = explode(' ', $form['basic']['inline']['keys']['#default_value']);
            foreach($searchWords as $index=>$word) {
                if(strpos($word, 'category:') === 0) {
                    // Use the value to set the default on the taxonomy search
                    $word = substr($word, strlen('category:'));
                    $form['advanced']['category']['#default_value'] = explode(',', $word);
                    // Remove it from the keyword textbox
                    unset($searchWords[$index]);
                }
            }

            // Re-set the default value for the text box without the category: part
            $form['basic']['inline']['keys']['#default_value'] = implode(' ', $searchWords);
        }
    }
}
1
votes

Thanks for sharing this. After several drupal installations I am facing this problem now, too. However, this time I had to use some search extensions like search_config and search_files to meet the clients requirements. Furthermore, I'm using better_select which turns select lists into lists of checkboxes (easier to select mulitple values on long lists). Thus, the checkboxes always returned some value (either the id if selected or 0 (zero) if not selected) and I ended up with something like "search phrase category:0,0,0,0,...,0".

Your solution above indeed removes the "category:0,0,0,0,0,0,...,0" string from keyword search field and checks all taxonomy terms correctly. The same can be done for content types, you'll just have to replace "category:" by "type:" throughout the whole script.

Paul