سؤال

I have a view I'm using as an "Advanced Search" tool - it simply provides a list of relevant nodes which are limited by a number of exposed filters I have in place. In particular, I have a "keyword" exposed filter that's acting as the main search box, which filters on the node title. Now, I've had a request for the ability to toggle (I assume with a checkbox or similar) this filter to search on both the node title and body at the same time, as an option to provide users with more search results.

I currently have a view set up with two displays: one that filters on just the node title, and one that uses Views Or to limit the results to nodes that have the keywords in either the node title OR the node body. By themselves, both of these displays are working the way I'd like.

My question is how I'd go about toggling between these two displays using a checkbox or something similar. I don't want to use Views Display Tabs because both displays look visually identical (the only difference is how the filters are configured to include more/less results) which would be confusing to the user - plus it uses AJAX which breaks some (mostly small) modifications I've made via jQuery to the behavior of the view.

What I'd like to accomplish, in essence, is a conditional check when the view is submitted - depending on whether a checkbox is selected, the arguments are sent to one display or the other. Any suggestions on how to accomplish this?

هل كانت مفيدة؟

المحلول

One way you might be able to go about this could involve adding a custom validate handler on the exposed filter form that would in theory allow you to check the value and forward the request to a different page display appropriately. Exposed filters are exposed in the URL as $_GET variables, so, forwarding a user to a specific page with filters predefined should be easy enough.

<?php

function mymodule_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'views_exposed_form':
      // Deterimine the ID so you only do this
      // to a specific exposed filter form
      // drupal_set_message($form['#id']);
      if ($form['#id'] == 'views-exposed-form-api-search-page-1') {
        // You might also want to add the checkbox FAPI item in this area
        $form['#validate'][] = 'mymodule_api_search_validate'; // custom validate handler function name
      }
    break;
  }
}

function mymodule_api_search_validate($form, &$form_state) {
  // Check if the FAPI item has the specified checkbox value
  if ($form_state['values']['options'] == 'title') {
    // The get variables to pass to the views exposed filters
    // You can configure what this $_GET variable should be while editing the filter
    $query = array(
      'query' => 'the search query',
    );
    drupal_goto('api/search', $query);
  }
}

?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top