0

I need to add some different styling to a view based on whether the exposed filters have been applied or not. Eg add the class "filtered" once a person has submitted their info in the exposed filter?

4
+150

You can do this with View's pre_render hook, eg, something along the lines of this:

function YOURMODULE_views_pre_render(&$view) {

  if ($view->name=='NAME_OF_YOUR_VIEW') {
    drupal_set_message('<pre>' . $view->exposed_data . '</pre>'); // to see what you have to work with
    if (!empty($view->exposed_data['type'])) { 
      $view->display_handler->set_option('css_class', 'view-has-been-filtered');
    }
  }

}

The above is based on a node type exposed filter, you will need to look at your View to see what filter names are involved, but the logic is that the exposed_data filter arrays will be empty until you have applied the filter. For example, here, this looks like this when you first go to the View:

Array
(
    [type] => Array
        (
        )

    [submit] => Apply
    [form_build_id] => form-85a47ba22bc12a8b443e4cc35afe75f7
    [form_token] => 105c57eec73efe4cfedd9acabc55786a
    [form_id] => views_exposed_form
    [] => Apply
)

and this is what it looks like after selecting 'foo' as a type in the exposed filter:

Array
(
    [type] => Array
        (
            [foo] => foo
        )

    [submit] => Apply
    [form_build_id] => form-85a47ba22bc12a8b443e4cc35afe75f7
    [form_token] => 105c57eec73efe4cfedd9acabc55786a
    [form_id] => views_exposed_form
    [] => Apply
)

ADDITION:

If this works for you, and per your comment, I suppose the following would work for any/all Views, but I haven't battle tested it...

function YOURMODULE_views_pre_render(&$view) {

  $filter_classes=array('view-has-been-filtered');

  if (!empty($view->exposed_data) && is_array($view->exposed_data)) {

    foreach($view->exposed_data as $k => $key) {
      if (is_array($key) && !empty($key)) {
        $filter_classes[]='view-has-been-filtered-by-' . str_replace('_', '-', $k);
      }
    }

    if (count($filter_classes) > 1) {
      $view->display_handler->set_option('css_class', implode(' ', $filter_classes));
    }

  }

}

...with the logic here being the only arrays in $view->exposed_data are filters, and if they aren't empty, the View has been filtered.

EEEK ADDITION:

My logic is somewhat flawed in that my initial tests were for filters that allowed multiple selections, and, of course, Views used arrays to capture them in $view->exposed_data. If your filters are just single value fields, Views will do something like this first:

Array
(
    [multiple_value_allowed_type] => Array
        (
        )

    [field_single_value_field] => All
    [submit] => Apply
    [form_build_id] => form-f601bde36c625db96bad7a84bf4fa210
    [form_token] => 105c57eec73efe4cfedd9acabc55786a
    [form_id] => views_exposed_form
    [] => Apply
)

so I guess the following dull ax approach would work:

function YOURMODULE_views_pre_render(&$view) {

  $filter_classes=array('view-has-been-filtered');

  if (!empty($view->exposed_data) && is_array($view->exposed_data)) {

    foreach($view->exposed_data as $k => $key) {
      if (is_array($key)) {
        if (!empty($key)) {
          $filter_classes[]='view-has-been-filtered-by-' . str_replace('_', '-', $k);
        }
      } else {
        switch ($k) {
          case 'submit':
          case 'form_build_id':
          case 'form_token':
          case 'form_id':
            break;
          default:
            if (!empty($k) && $key != 'All')  {
              $filter_classes[]='view-has-been-filtered-by-' . str_replace('_', '-', $k);
            }
        }
      }
    }

    if (count($filter_classes) > 1) {
      $view->display_handler->set_option('css_class', implode(' ', $filter_classes));
    }

  }

}

DOUBLE D'OH ADDITION:

Of course, this quick and dirty logic also works:

function YOURMODULE_views_pre_render(&$view) {

  if (!empty($view->exposed_data) && count($_GET) > 1) {
    $view->display_handler->set_option('css_class', 'view-has-been-filtered');
  }

}

ADDITION DUE TO COMMENTARY:

If you are setting css classes via the UI that you want to preserve, you can do this:

$view->display_handler->set_option('css_class', $view->display_handler->get_option('css_class') . ' view-has-been-filtered');

which gets that UI option and then adds the view-has-been-filtered class to it. Please note the required space in front in the code :)

  • Thanks, Jimajamma. I will give this a shot. Any way to modify this so it applies to any and all views in general? – Nigel Waters Jun 4 '13 at 1:15
  • Sorry to poke at this again. If you have some time, is there a way to do this using the theme layer? I'll throw another 100 point bounty on the question if it's something worth your while. – Nigel Waters Jun 19 '13 at 23:45
  • by theme layer, do you mean from a MYTHEME_hook() function implemented inside of your theme's template.php file? If so, you can put a YOURTHEME_views_pre_render() function in there with the same logic as above and it will accomplish the same thing. – Jimajamma Jun 20 '13 at 1:28
  • Great. That sort of works. The "quick and dirt logic one" but it appears to remove any extra added classes that I have set in the views UI. Any way to keep my custom classes set in the UI as well? – Nigel Waters Jun 20 '13 at 4:48
  • I've added in some addition code in the answer above that should address this. Good call. Basically you call the handler's get_option() function and then append the class to it. (This is a space delimited list of classes) – Jimajamma Jun 20 '13 at 19:53
-2

To any view,you can add a different css class by 1. First clicking on the editing option on a view 2. In the Page details,click on ADVANCED properties 3. Add the css class to the second last attribute present there which is css class. This way you can add css classes to every view.

  • I need the class to only be added when a view is in a filtered state so that I can apply different styling. – Nigel Waters Jun 3 '13 at 14:05

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.