0

I have an Autocomplete (Tags style) field with multiple values in the field.

I'm using exposed filters to display nodes with one or more values in the Autocomplete field matching the filters.

I want the node field to display only the matched values, not all of them when rendered in a view.

For example. Say I have three nodes with the following field values:

enter image description here

Then I apply exposed filters of Orange and Grape. This results in the following (as Node A does not contain these values):

enter image description here

What I want is values not in the exposed filters to also disappear, so the user can see exactly what selected fields matched the node. Like so:

enter image description here

I assume this can be done using the Global PHP field, but really need some help to figure it out.

1 Answer 1

0

I managed to do this by creating a module that implements hook_views_pre_render()

Your field values will need to be taxonomy references for this to work, like so:

function MyModule_views_pre_render(&$view) {
    // Set the view we want to work with
    if ($view->name=='your_view') {
        // print out your array, so you know what you have (comment out in live site)
        drupal_set_message('<pre>' . print_r($view->result, TRUE) . '</pre>'); 
        // Check to see if the filter has been triggered, otherwise you will get errors
        if (isset($view->exposed_input['exposed_filter_name'])) {
            foreach($view->result as $result_id => $result) {
                // cycle through each view result row, check that each result exists in the exposed filter. if it does not, then remove it.     
                foreach ($result->field_your_field as $field_id => $field_val) {
                    //find the TID related to your result
                    $field_tid = $result->field_your_field[$field_id]['raw']['target_id'];
                    // Check if the TID is missing from the exposed filter values
                    if (array_search($field_tid, $view->exposed_input['exposed_filter_name']) === false) {
                        // Now look through your field values to find the TID missing from the values
                        if (($key = array_search($field_val, $result->field_your_field)) !== false) {
                            // And remove it
                            unset($result->field_your_field[$key]);
                        }
                    }
                }         
            }
        }
    }
    // Finally re-number array to account for missing elements - i.e. [1],[3],[4],[6] to [1],[2],[3],[4] (optional, but tidier)
    $result->field_your_field = array_values($result->field_your_field);
}

Note that field_your_field is written like that because you need to append an extra field_ to the machine name. e.g. machine name field_my_field would become field_field_my_field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.