0

I have a search api view. Which has an exposed some exposed filters.

The exposed filters contains postal code, city and latlong field.

The user will enter the city and postal code and then in the code I will get a latlong from that address and will assign to the latlong field.

I have tried the following code but it is not working.

function mymodule_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] == 'views-exposed-form-id') {
    array_unshift($form['#submit'], 'mymodule_maps_exposed_form_submit');
  }
}

function mymodule_maps_exposed_form_submit(&$form, &$form_state) {
  $address = $form_state['values']['zipcode'] . ' ' . $form_state['values']['city'];
  $latlong = somefunction($address);
  // Tried all of these options but nothing is working.
  $form_state['values']['latlong'] = $latlong;
  $form_state['input']['latlong'] = $latlong;
  $form_state['view']->exposed_input['latlong'] = $latlong;
  $form_state['view']->display_handler->handlers['filter']['latlong']->validated_exposed_input = $latlong;
  $form_state['view']->display_handler->handlers['filter']['latlong']->value = $latlong;
  // This line adds the value to the form field but the filter doesn't apply.
  $form_state['complete form']['latlong']['#value'] = $latlong;
  $form_state['complete form']['latlong']['#default_value'] = $latlong;
}

Is there any trick I am missing? Can someone guide me? Anyone who can give a hint?

  • Anyone who can give a hint? – Ahmad Oct 17 at 6:04
0

I have found an answer. I am sharing it here so that it can help someone.

Instead of form_alter hook, we can use hook_views_pre_view.

Here is the code

/**
 * Implements hook_views_pre_view().
 */
function mymodule_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'my_view_name') {
    $address = 'some address';
    $latlong = somefunction($address);
    $view->get_exposed_input();
    $view->exposed_input['latlon'] = $latlon;
    $view->execute().
  }
}

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.