1

We have an All Articles view that is added to the http://www.example.com/articles page that is supposed to display all pages where content type is Article. The content type has a taxonomy term, Category, which has several filter values such as IoT, Security, and Cloud. On the view, the field_category is exposed for visitors to change the value and it will automatically update the results (Use Ajax = true).

Goal:

  • On change of the exposed filter to Cloud, we would like to redirect the user to another page (http://www.example.com/some-page).

Question:

  • How would we redirect a user to the other page when the exposed filter is set to Cloud?

View Settings:

Format

  • Format: Unformatted List | setting (default settings)
  • Show: Content | Listing (custom view mode)

Filter Criteria

  • content: published (= yes)
  • content: content type (= Article)
  • content: category (exposed)

Advanced

  • Use Ajax: Yes
  • Query Option: Distinct = true

Here is what we tried, but it does not redirect with no errors.

mymodule.module

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'view_exposed_form' && $form['#id'] == 'views-exposed-form-articles-page-1') {

   if($form['field_category_target_id'] == '123') {
     $form['field_category_target_id']['#ajax] = [
       'callback' => 'cloud_ajax_callback',
       'wrapper' => 'category_cloud',
     ],
   }
  }
}

function cloud_ajax_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $cloud_cmd = $new RedirectCommand('http://www.example.com/some-page');
  return $response->addCommand($cloud_cmd);
}
  • Not an expert, but have gone down this path a bit. If I understand correctly, the issue is that $form_state doesn't have an" event listener" for the '#options' array state change in the context you need. Even $form_state->setRebuild(TRUE); needs an interaction from some $form element (e.g. submit). This task could easily be achieved with javascript/jQuery. – Prestosaurus Apr 6 at 1:53
  • 1
    drupal.stackexchange.com/questions/277980/…. And the most recent result I could find that I think relates to this question: drupal.stackexchange.com/questions/21355/… (n/a here). – Prestosaurus Apr 6 at 1:54

Your Answer

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

Browse other questions tagged or ask your own question.