I'm using custom search module for the advanced search on my website. The advanced search by criteria (phrase) and content types are being used. I wanted to customize the search form then I created a custom module and implemented the hook_form_alter for customizing the search form.

<?php
MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'custom_search_blocks_form_1') {
     ...
     // for some reasons I want to hide the criteria phrase field
     $form['custom_search_criteria_phrase']['#type'] = 'hidden';
    ...
  }
}
?>

The code above doesn't work. I always have the criteria field showed in my advanced search form, so I use var_dump($form) for looking into the $form variable, I found that there is no custom_search_criteria_phrase key in search form.
So I'm wondering maybe the custom_search_form_alter function overwrote my search form?
How can I do it correctly?
Thanks in advance.

CommentFileSizeAuthor
#3 advanced_search_form.png6.2 KBkuangshi.yan
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kuangshi.yan’s picture

Issue summary: View changes

Also I cannot modify the field weight in hook_form_alter

<?php
  if ($form_id == 'custom_search_blocks_form_1') {
     ...
     $form[$form_id]['#weight'] = '0';
    ...
  }
}
?>

I think this is the same issue, there is no #weight key in $form variable.

jdanthinne’s picture

I don't understand why you want to change that with a module a hook_form_alter, because those options are configurable in the block option page itself (weights, criteria…). That's why Custom Search was created in fact…

kuangshi.yan’s picture

FileSize
6.2 KB

In fact, I don't want to display the search box field and search criteria phrase field at the same time. Instead of that I will add a checkbox to the advanced search form. When unchecked, the search box will display, in contrast, when checked, it's the search criteria phrase will display. So I want to alter the form for hiding/showing fields.

jdanthinne’s picture

Ok, I see.
I think your form_alters don't work because they are ran before Custom Search. On install Custom Search changes its weight to 100, so you need to have a heavier weight.
Try putting this in you module.install file (and uninstall and re-install it):

/**
 * Implements hook_install().
 */
function MYMODULE_install() {
  db_update('system')
    ->fields(array('weight' => 110))
    ->condition('name', 'MYMODULE')
    ->execute();
}
kuangshi.yan’s picture

Ok, I see, `weight` => 100 has been set for custom search module.
May I know why you set a weight for your module?
Thanks.

jdanthinne’s picture

Because it has to run after all the module that already alters the search.

kuangshi.yan’s picture

Status: Active » Closed (works as designed)

Thanks. I pass the status closed for this issue.