1

I want to combine two or more filter criteria to a dropdown in views filter criteria. so that i can select a filter criteria in the dropdown box and can search with a string in a text box. so that the result will be similar to the string from the filter criteria which is selected from the dropdown listbox(combination of filter criterias).

Is there any way to combine the filter criteria as dropdown...?

  • I don't understand the part "so that i can select a filter criteria in the dropdown box and can search with a string in a text box" How many filter do you have? You want to combine few filter as dropdown and one filter is as text box? – MutantMahesh Aug 1 '14 at 15:58
  • This may help drupal.stackexchange.com/questions/40629/… – MutantMahesh Aug 1 '14 at 16:02
  • i am using a using a dropdown list and a textbox. I want to use the dropdown to select the main filter and i will use a text box to search for a string. so the result will be similar to the string from the text box with the selected filter criteria. – Mukilan R Aug 1 '14 at 16:03
  • i want to add two fields like Content: Title and Content: Body (body) together in a dropdown. so that i can select a field from the dropdown and search some string in that field using the textbox string. – Mukilan R Aug 1 '14 at 16:07
  • I am still not able to understand completely you want to accomplish. If you expose Content: Title and Content: Body (body) filter those will be exposed as textfield. right? How you suppose to combine them as dropdown? – MutantMahesh Aug 1 '14 at 17:24
1

I Found this problem interesting and spend little time over this. Here is the solution i come with.

  1. Create a view with title and Body field.
  2. Add two filters Content:Title and Content:Body with Contains operator, expose these two filters.
  3. Create a custom module with the following hooks.

    //custom.module
    function custom_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
    
    if ($form_id == 'views_exposed_form' && $form_state['view']->name == 'VIEW_NAME') {
    
        // Unset the title and body filter info so that it doesn't print any label.
        unset($form['#info']['filter-title']);
        unset($form['#info']['filter-body_value']);
        // Unset the title and body filter
        unset($form['title']);
        unset($form['body_value']); 
    
        $form_element = array();
        $options = array(
        // '' => '-Select-',
          'title' => 'Title',
          'body' => 'Body',
        );
        $form_element['search_within'] = array(
          '#type' => 'select',
          '#title' => t('Search within'),
          '#options' => $options,
          '#default_value' => !empty($form_state['search_within']) ? $form_state['search_within'] : '',
          '#description' => t('Set the <em>Title</em> or <em>Body</em> within which you want to search.'),
          '#prefix' => '<div class="views-exposed-widget">',
          '#suffix' => '</div>',
        );
        $form_element['search_box'] = array(
          '#type' => 'textfield',
          '#title' => t('Search term'),
          '#default_value' => !empty($form_state['search_box']) ? $form_state['search_box'] : '',
          '#description' => t('Enter the term which you want to search.'),
          '#prefix' => '<div class="views-exposed-widget">',
          '#suffix' => '</div>',
        );
        $form = $form_element + $form;
    }
    }
    
    function custom_views_query_alter(&$view, &$query) {
    if ($view->name == 'VIEWS_NAME') {
        if (!empty($view->exposed_input['search_box']) && $view->exposed_input['search_within'] == 'title') {
        // Add title filter condition if title is selected
            $query->where[1]['conditions'][] = array(
              'field' => 'node.title',
              'value' => '%' . $view->exposed_input['search_box'] . '%',
              'operator' => 'LIKE',
              );
        }
        if (!empty($view->exposed_input['search_box']) && $view->exposed_input['search_within'] == 'body') {
        // Create join with fiedl_data_body table.
        // source:
        // https://api.drupal.org/api/views/includes!handlers.inc/function/views_join%3a%3aconstruct/
        // https://www.drupal.org/node/2049051#comment-7784967
            $join = new views_join;
            $extra = array();
            $extra[] = array(
              'table' => 'field_data_body',
              'field' => 'entity_type',
              'value' => 'node',
              'operator' => '=',
              );
            $extra[] = array(
              'table' => 'field_data_body',
              'field' => 'deleted',
              'value' => '0',
              'operator' => '=',
              );
            $join->construct('field_data_body', 'node', 'nid', 'entity_id', $extra);
        // Add join to query; 'node' is the left table name
            $view->query->add_relationship('field_data_body',$join,'node');
        // Add fields from table (or where clause, or whatever)
            $view->query->add_field('field_data_body','entity_id');
        // Add body filter condition if body is selected
            $query->where[1]['conditions'][] = array(
              'field' => 'field_data_body.body_value',
              'value' => '%' . $view->exposed_input['search_box'] . '%',
              'operator' => 'LIKE',
              );
        }
    }
    }
    

    Note: This solution will only work with Contains operator and very case specific. It would be great if somebody will create a generic views filter handler to combine exposed filter as select list and allow search within selected filter.

  • This solution helped me out of the issue. tq @MutantMahesh – Mukilan R Aug 4 '14 at 7:39
0

You may need the Better Exposed Filters module for this.

Make sure the fields that you want to filter through are in your fields area, even if they are not visible. Add a new filter criteria. You are looking for the one named Global: Combine fields filter. From here you can choose two of the fields that you have added to the Fields area. Set your operator to contains and make sure it is exposed.

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.