Hi, everyone. I have been learning drupal for two weeks and now have encountered a problem that has fazed me several days.

The problem is in the attempt of modifying the search module form. Specifically, you know that the "advanced search" frame in the search module provides a dropdown list "Only in the category(s)" and a multiple check box "Only of the type(s)", from which you can specify the search scope.

But, by default "Only in the category(s)" will list all the vocabularies and terms in taxonomy, while "Only of the type(s)" will list all the content types existing in the system. Therefore, I want to efface some of the unnecessary items in the two lists. But, I don't know how to do.

I have search intensively in my drupal folder, but find no clue about "advanced search" at all. And, searching Internet returns me no valuable result, neither.

Thanks for your help~

Comments

abdu’s picture

Hi,

You can use hook_form_alter (http://api.drupal.org/api/function/hook_form_alter/6) to alter the form and reset option values from the drop down

pseudoking’s picture

thanks for your reply very much~

yes, I have tried hook_form_alter, but I am not sure which form to intercept, "search_block_form" or "search_form"?
Actually, I have tried both of them, but it seems that only the three normal search fields: the label showing "Search this site:", the textfield for key word input, and the submit button, can be captured. While no field of the advanced search part is relayed into hook_form_alter.

abdu’s picture

Just try it, you can see all fields

function YOURMODULENAME_form_alter(&$form, $form_state, $form_id){
  if ('search_form' == $form_id) {
		print '<pre>'; print_r($form);	print '</pre>';
  }
}
pseudoking’s picture

thanks. but still don't work; the follows are what are returned:

Array
(
[#action] => /baibao/search/node
[#attributes] => Array
(
[class] => search-form
)

[module] => Array
(
[#type] => value
[#value] => node
)

[basic] => Array
(
[#type] => item
[#title] => Enter your keywords
[inline] => Array
(
[#prefix] =>
[#suffix] =>

[keys] => Array
(
[#type] => textfield
[#title] =>
[#default_value] =>
[#size] => 40
[#maxlength] => 255
)

[processed_keys] => Array
(
[#type] => value
[#value] => Array
(
)

)

[submit] => Array
(
[#type] => submit
[#value] => Search
)

)

)

[#parameters] => Array
(
[0] => search_form
[1] => Array
(
[storage] =>
[submitted] =>
[post] => Array
(
)

)

[2] =>
[3] =>
[4] => node
)

[#build_id] => form-54968560bd48e285a68eee788f06e1d7
[#type] => form
[#programmed] =>
[form_build_id] => Array
(
[#type] => hidden
[#value] => form-54968560bd48e285a68eee788f06e1d7
[#id] => form-54968560bd48e285a68eee788f06e1d7
[#name] => form_build_id
)

[#token] => search_form
[form_token] => Array
(
[#id] => edit-search-form-form-token
[#type] => token
[#default_value] => 80659d61db67febf821106988b706539
)

[form_id] => Array
(
[#type] => hidden
[#value] => search_form
[#id] => edit-search-form
)

[#id] => search-form
[#description] =>
[#required] =>
[#tree] =>
[#parents] => Array
(
)

[#method] => post
[#validate] => Array
(
[0] => search_form_validate
)

[#submit] => Array
(
[0] => search_form_submit
)

)

abdu’s picture

This is what i got,
Make sure you have enabled advanced search

Array
(
    [#action] => /drupal69/search/node
    [#attributes] => Array
        (
            [class] => search-form
        )

    [module] => Array
        (
            [#type] => value
            [#value] => node
        )

    [basic] => Array
        (
            [#type] => item
            [#title] => Enter your keywords
            [inline] => Array
                (
                    [#prefix] => 
[#suffix] =>

                    [keys] => Array
                        (
                            [#type] => textfield
                            [#title] => 
                            [#default_value] => abdu
                            [#size] => 40
                            [#maxlength] => 255
                        )

                    [processed_keys] => Array
                        (
                            [#type] => value
                            [#value] => Array
                                (
                                )

                        )

                    [submit] => Array
                        (
                            [#type] => submit
                            [#value] => Search
                        )

                )

        )

    [#parameters] => Array
        (
            [0] => search_form
            [1] => Array
                (
                    [storage] => 
                    [submitted] => 
                    [post] => Array
                        (
                        )

                )

            [2] => 
            [3] => abdu
            [4] => node
        )

    [#build_id] => form-4e4c5be7cb5f287a7278b239a6d84f58
    [#type] => form
    [#programmed] => 
    [form_build_id] => Array
        (
            [#type] => hidden
            [#value] => form-4e4c5be7cb5f287a7278b239a6d84f58
            [#id] => form-4e4c5be7cb5f287a7278b239a6d84f58
            [#name] => form_build_id
        )

    [#token] => search_form
    [form_token] => Array
        (
            [#id] => edit-search-form-form-token
            [#type] => token
            [#default_value] => 8e93222480b1d810f4079e181aff0809
        )

    [form_id] => Array
        (
            [#type] => hidden
            [#value] => search_form
            [#id] => edit-search-form
        )

    [#id] => search-form
    [#description] => 
    [#required] => 
    [#tree] => 
    [#parents] => Array
        (
        )

    [#method] => post
    [#validate] => Array
        (
            [0] => search_form_validate
            [1] => node_search_validate
        )

    [#submit] => Array
        (
            [0] => search_form_submit
        )

    [advanced] => Array
        (
            [#type] => fieldset
            [#title] => Advanced search
            [#collapsible] => 1
            [#collapsed] => 1
            [#attributes] => Array
                (
                    [class] => search-advanced
                )

            [keywords] => Array
                (
                    [#prefix] => 
[#suffix] =>

                    [or] => Array
                        (
                            [#type] => textfield
                            [#title] => Containing any of the words
                            [#size] => 30
                            [#maxlength] => 255
                        )

                    [phrase] => Array
                        (
                            [#type] => textfield
                            [#title] => Containing the phrase
                            [#size] => 30
                            [#maxlength] => 255
                        )

                    [negative] => Array
                        (
                            [#type] => textfield
                            [#title] => Containing none of the words
                            [#size] => 30
                            [#maxlength] => 255
                        )

                )

            [category] => Array
                (
                    [#type] => select
                    [#title] => Only in the category(s)
                    [#prefix] => 
[#size] => 10 [#suffix] =>

                    [#options] => Array
                        (
                            [Image Galleries] => Array
                                (
                                    [1] => Abdu Gallery
                                    [2] => My Gallery
                                )

                            [Tags] => Array
                                (
                                    [3] => Cricket
                                )

                        )

                    [#multiple] => 1
                )

            [type] => Array
                (
                    [#type] => checkboxes
                    [#title] => Only of the type(s)
                    [#prefix] => 
[#suffix] =>

                    [#options] => Array
                        (
                            [blog] => Blog entry
                            [image] => Image
                            [page] => Page
                            [panel] => Panel
                            [poll] => Poll
                            [story] => Story
                        )

                )

            [submit] => Array
                (
                    [#type] => submit
                    [#value] => Advanced search
                    [#prefix] => 
[#suffix] =>

                )

        )

)
pseudoking’s picture

I think I have (finally) find the problem. The advanced form is altered in node.module instead of search.module. I'm now trying to make the modification.
Thanks for your help, friends~

pseudoking’s picture

I think I have (finally) find the problem. The advanced form is altered in node.module instead of search.module. I'm now trying to make the modification.
Thanks for your help, friends~

reinholdlange’s picture

Hi pseudoking - have you been able to figure this out? I too am trying to change the advanced search form but am having not much luck.

talino’s picture

I had the same problem until I realized there was apparently a syntax problem with the 'case' statement in my hook_form_alter (if you're using 'case', it might help). It used to be :

<?php
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case ('article_node_form' || 'document_node_form'):
      $form['taxonomy'][1]['#collapsed'] = FALSE;
      $form['taxonomy'][1]['#collapsible'] = FALSE;
      break;
    case 'search_form':
      // Whatever you want to do to the search form
    break;
  }
}
?>

Changing it to the following made the second case (the search_form one) work as expected:

<?php
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'article_node_form':
    case 'document_node_form':
      $form['taxonomy'][1]['#collapsed'] = FALSE;
      $form['taxonomy'][1]['#collapsible'] = FALSE;
      break;
    case 'search_form':
      // Whatever you want to do to the search form
    break;
  }
}
?>

To remove some content types from advanced search but still allow the administrator to use them, I did this:

<?php
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'search_form':
      if (!user_access('administer search')) { // You can use any permission you like here
        unset(
          $form['advanced']['type']['#options']['content_type_1'],
          $form['advanced']['type']['#options']['content_type_2']
        );
        // Rename a content-type that users can see but which they normally don't interact with.
        // In my case, 'book' is only use for online help.
        $form['advanced']['type']['#options']['book'] = 'Online help';
      }
    break;
  }
}
?>

Hope it helps.

candelas’s picture

yes it helps, thanks @talino

//trying to answer one question for each one that i make.
//this way, drupal will be more friendly and strong