0

Original question at the bottom. Quick summary, I needed a way to remove the comma character from views exposed filter queries. Below, I was given an example of custom module to do this using validation, which I used and barely modified. However, I'm getting the errors:

Notice: Undefined index: #parameters in search_helper_form_alter() (line 6 of /Applications/MAMP/htdocs/drupal-7.12_old/sites/all/modules/search_helper/search_helper.module).

Notice: Trying to get property of non-object in search_helper_form_alter() (line 6 of /Applications/MAMP/htdocs/drupal-7.12_old/sites/all/modules/search_helper/search_helper.module).

Since I've never written a module before, I'm not sure how to interpret these errors. I'd really appreciate any suggestions. Here's the code.

<?php
function search_helper_form_alter(&$form, $form_state, $form_id) {  
    switch($form_id) { 
        case 'views_exposed_form': 

        if ($form['#parameters'][1]['view']->name == 'views_exposed_form_address_search_page') { // name of the view
            // display array, you have to enable devel module
            //$output .= dsm($form); 

        $form['#validate'][] = 'no_commas';
   }
  break;  
 }    
}
function no_commas($form, &$form_state) {
  $form_state['values']['d'] = preg_replace(",", ' ', $form_state['values']['d']);
}
?>

Original question: I've been trying to set up a location search using views exposed filters and various methods of storing information. The problem inevitably arises that users obviously will want to search for a location using a comma (San Diego, CA or Portland, OR), but the comma isn't stored with the locations anywhere. So, if they search for San Diego or they search for California it works fine, but when they search for both using comma separation, they query returns no results. Is there a way to remedy this? The best way to do this, it seems, would be to ignore the comma within the query somehow. It's been driving me crazy for quite some time now how to get this working. Any help would be appreciated.

2 Answers 2

1

You could write a small custom module that -

  1. Form alters views exposed filter form of the particular view in question
  2. Add a new validate function to the form above (validate function is called before the actual form submit function is called and allows you to manipulate values)
  3. Then in the validate function you could edit the particular exposed filter form field replacing commas with nothing.

Here is a sample code that I found on Googling around - http://drupal.org/node/716420#comment-2634540

Hope this helps.

1
  • That page did help, but I'm still having issues. I tried writing a modified module from that page, but since I haven't really written a module before, I don't know why it's not working. Would you mind taking a look at the code? I posted it above in the issue.
    – Mrweiner
    Commented Jun 19, 2012 at 21:19
0

Basically, just got rid of unnecessary parts of the code that I didn't know I didn't need. Now, I think that this is going to apply to all views exposed filters, but at this point I don't mind. Here's the working code if anybody cares:

<?php
function search_helper_form_alter(&$form, $form_state, $form_id) {
    switch($form_id) {
       case 'views_exposed_form':

       $form['#validate'][] = 'no_commas';

  break; 
 }   
}

function no_commas($form, &$form_state) {
  $form_state['values'] = preg_replace("/,/", '', $form_state['values']);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.