0

I have a views that lists webform results. There are some fields such as name, e-mail address, city etc comes from webform submission.

I want an exposed filter to filter the views based on city. This can be done and a search input appears at the top of the views but there is a problem: you should enter the full city name and then submit. There is no autocomplete or select list feature.

There is a module called Views Autocomplete Filters for this purpose but doesn't work with webform results data.

There was a sandbox project called Webform Selectable Filters before but doesn't exist now.

So, how to add an autocomplete exposed filter to a view that is based on webform results data?

enter image description here

1

I did this in my custom module :

function hook_form_alter( &$form, &$form_state,$form_id ) {

  if($form_id == 'views_exposed_form') {
    $view = $form_state['view'];
    if($view->name == 'your_view_name') {  
     $form['data']['#autocomplete_path'] = 'example/autocomplete';
   }
  }
}

'#autocomplete_path' => 'example/autocomplete' defines an autocomplete callback for our textfield.

When user types something into our textfield, Drupal sends a request via this url to send data you input and to receive suggestions via json if there are any.

To make Drupal aware of this path, let’s add it to the menu system(hook_menu):

$items['example/autocomplete'] = array(
  'page callback' => '_module_name_autocomplete',
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK
);

Now we need to create a _module_name_autocomplete function which will look for a suggestion and return it to the user.

function _module_name_autocomplete($string) {

  $matches = array();
  // Some fantasy DB table which holds cities
  $return = db_select('webform_submitted_data', 'w')
  ->fields('w')
  ->condition('w.cid', 'your_component_id', '=')
  ->condition('w.data', '%' . db_like($string) . '%', 'LIKE')
  ->execute();

  // add matches to $matches
  foreach ($return as $row) {
   $matches[$row->data] = check_plain($row->data);
  }

 // return for JS
 drupal_json_output($matches);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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