24

I have two user profile fields "Last Name" and "First Name". I also have a user list views. I want to expose "Name" as filter which can search for both Last Name and First Name. How can I do single filter of combination of those two fields? Can I create it from views UI?

One possible way is to create another profile field "Name" which will be hidden in the form. On user save, I would combine the two field values into the "Name" field and then expose it as filter in the views. But this solution is hard coding and it needs to write hooks.

21

I eventually got a solution from this. I followed the second primary solution offered although the blogger personally used hook_views_query_alter().

  1. installed the module Views filters populate.
  2. added two filters "First Name" and "Last Name" (both must not be exposed) and added them to a OR filter group (Views 3 supports this). I had to use the operator "Contains any word" for both fields, otherwise the query gave me no desired result.
  3. created a filter "Global: populate filters", added the two fields into this and exposed it.

This gave me a quick solution without hard coding.
Here is other helpful reference.

  • I don't think you need to use step 2 -- I installed the module and used a "Global: combine fields filter" that was exposed. I selected the two fields within the settings of that combined field. – Laryn - CEDC.org Aug 28 '15 at 14:44
13

On my installation, D7 with Views 7.x-3.6, you can add a filter that is " Global: Combine fields filter" which will do exactly what you need, allow users to search on multiple fields with a single filter.

  • 1
    This should be the accepted answer – frazras Apr 7 '16 at 11:19
5

Its really easy.

  1. Click on Add a filter.
  2. select "Global: Combine fields filter"
  3. Follow the instructions.

Thanks.

2

Works out of the box

The easiest way to go is to use the 'Search Terms' filter which is Views core functionality. It will search through all the fields of the entity and return the result, no extra module necessary!

  1. Go to your View
  2. Add a new filter to view
  3. Select 'Search: Search Terms'

Thats it, nothing else to do (you might want to expose it though)

It performs very well too.

0

You can use hook_views_pre_execute(&$view) to extend your single condition to multiple fields you wish

/**
 * Recursive looks for mentioning of $fname as field of in query QueryConditionInterface::conditions()
 * 
 * @param array $cond result of QueryConditionInterface::conditions to search in
 * @param string $fname a name oof field we are looking for
 * @param array $res result array containing a references of conditions having $fname
 * @param integer $rec_lvl level of recursion
 * 
 * @return array an array containing $fname mentioned in condition $res[]['field'] == $fname
 */
function dolynskyi_help_func_find_field_condition(&$cond, $fname, &$res = array(), &$rec_lvl = 0) {
    $numeric_keys = array_filter(array_keys($cond), function($k) {return is_int($k) || (substr($k, 0, 1) !== '#');});
    $rec_lvl++;
    foreach($numeric_keys as $numkey) {
        $t = gettype($cond[$numkey]['field']);
        if($t == 'string') {
            if($cond[$numkey]['field'] == $fname) {
                $res[] = &$cond[$numkey];
            }
        } elseif($t == 'object') {
            dolynskyi_help_func_find_field_condition($cond[$numkey]['field']->conditions(), $fname, $res, $rec_lvl);
        }
    }
    $rec_lvl--;
    return $res;
}

function dolynskyi_help_func_views_pre_execute(&$view) {
    if($view->name == 'appraisals_special_global_access') { // view name we wanna extend
        $fname = 'field_data_field_ap2_employee.field_ap2_employee_target_id'; // field we look for to use as source of extending
        $search = &dolynskyi_help_func_find_field_condition($view->build_info['query']->conditions(), $fname);
        foreach($search as &$v) { // looping found field references
            $or = db_or(); 
            $or->condition($v['field'], $v['value'], $v['operator']); //repeating existing field condition
            $or->condition('field_data_field_ap2_employee.field_ap2_employee_target_id', array('711'), 'IN'); //adding any extra condition
            $v = array('value'=>null, 'operator'=>'IS NULL', 'field'=>$or); // wrapping up simple condition for $fname to multiple with $fname + our extra field via OR logic
            unset($v);
        }
    }
}

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.