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);
}
'#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.