[development] How to interrupt/redirect Search submit to auth/validate (via Captcha) *BEFORE* actual submit to search engine & display of results?

Lee Rowlands leerowlands at rowlands-bcs.com
Sun May 16 08:36:56 UTC 2010


Hi Ben
I think captcha can be configured to run on any form, add mollom to the
mix and you get some advanced intelligence as to when you want the captcha
to be shown.

That said you should have a look at hook_form_alter
http://api.lullabot.com/hook_form_alter
You want to target when the form_id argument equals 'search_box_form'
You can add your handlers (submit/validation) to the form processing using

$form['#submit'] and $form['#validate'], the $form arg is passed by
reference.
Both of these are arrays, you should test that they exist first eg
if (is_array($form['#submit']) {
  array_unshift($form['#submit'], 'name_of_your_handler');
}
else {
  $form['#submit'] = array('name_of_your_handler');
}
If you use array_unshift, your handlers will run first.
You can then do form_state['redirect'] = 'your path' if needs be.
You can also add the captcha elements directly to the form if no session
var has been set
Ie in hook_form_alter, check the form_id is the search form, if it is and
there is no session var (tracking they've satisfied the captcha) then you
can add the captcha elements directly to the $form var (and your
submit/validate handlers). In your validate handler, test the captcha was
satisfied and then in your submit handler, set the session variable so the
next time the form is built, your form_alter hook doesn't add the captcha
again.
Hope this makes sense.
Lee



On Sat, 15 May 2010 19:42:54 -0700, Ben DJ
<bendj095124367913213465 at gmail.com> wrote:
> On Sat, May 15, 2010 at 7:00 PM, Ben DJ
> <bendj095124367913213465 at gmail.com> wrote:
>> That does not seem Drupal-esque to me.  I'm guessing that the current
>> best approach is to 'somehow' grab & use FormAPI ...
>> Finding a method recommendation, and docs to go with it, has beeen a
>> challenge -- so far.
> 
> One approach that seems to me to hold some promise, or at least pieces
> of it, is described here
> 
>   "Redirecting Users After Submitting a Form in Drupal 5 and 6"
>    http://drupal.org/node/134000#comment-1556764
> 
> wherein,
> 
> ...
> In Drupal 6, one method is:
> 
> Create a custom module custom.module and place this code in it:
> function custom_form_alter(&$form, $form_state, $form_id) {
>   switch ($form_id) {
>     case 'contact_mail_page':
>         $form['#redirect'] = 'thank-you-page';
>       break;
>   }
> }
> ...
> 
> Looking in ./modules/search/search.module, i see,
> 
> ...
> /**
>  * Process a block search form submission.
>  */
> function search_box_form_submit($form, &$form_state) {
> ...
>   $form_id = $form['form_id']['#value'];
>   $form_state['redirect'] = 'search/node/'.
> trim($form_state['values'][$form_id]);
> }
> ...
> 
> which refers to the 'redirect'.
> 
> Right approach with the right pieces?  If so, unclear -- atm -- how
> they fit together.
> 
> Ben


More information about the development mailing list