1

I know how to add additional class by preprocess function but I need to combine it with the view. So the new custom class should be added if my view returns 0 results. It looks like I can't use hook_views_pre_build because there's no vars for page elements. Any thoughts how to achieve it?

4
  • I'm almost positive you'd have to build the view manually using the views API in your pre-process function, check the results array, and if empty, add your class.
    – Webdrips
    Oct 10 '14 at 11:59
  • have you tried no result behaviour and adding class from their? Oct 10 '14 at 12:47
  • Target a views hook that is post execution and contains the result count. Throw the result in a drupal_static and then read that static from the preprocess. Perhaps a bit dirty but if not overused in hard to solve situations like these perhaps an option. Oct 10 '14 at 16:12
  • @J. Reynolds this sounds like a good approach, even if I need to read the variable in different function.
    – David
    Oct 11 '14 at 12:36
1

Might be a little over-kill, but it's the first thing that comes to mind without having to build a custom view in code (which is cleaner imo). I am referencing Views 2 only, might be different in 3.

  • Create an empty view page, strip it of everything if you'd like, it just needs to exist.
  • In the view you are using, for the no results behavior, choose to include the empty view.
  • Using the Context module, add the condition that if the empty view is shown on the page to execute. In the reactions area, add Theme HTML which will allow you to enter in your custom css class to the body tag.

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.