3

With the Views module for Drupal 7 you can set a "No result behaviour" that uses another View rather than just text. I can't see the same functionality in Views 2 for Drupal 6. Is it possible to extend Views to do this?

The motivation for this is that we have a home page driven by a View that displays the recent contents from a user's groups; if the user hasn't joined any groups yet then we want to display a different View using content from all groups.

  • Have you tried Views 3 for Drupal 6? – mpdonadio Jan 19 '12 at 14:22
  • We considered it, but several of the modules we're using do not work correctly with Views 3 so need to stick with 2. – drmonkeyninja Jan 19 '12 at 14:24
4

You can simulate this if you instantiate the view in code. The general steps are

  1. Make a custom block.
  2. In the block view callback, instantiate and execute the view you want, but don't generate output.
  3. Check the number of rows. If zero, then repeat #2 with your alternate view.
  4. Generate the output for the view, and add to the block output.
  5. Use the block in place of the view where you need to.

If you do everything properly, all of your view templates will be used, so you aren't really losing anything.

All of the views related function are documented at drupalcontrib.org, but I think that is the Drupal 6, Views 3 version. Googling on views_get_view will also get you started and should lead to examples of generating views with code. If you have Advanced Help installed, you will also see some information there.

I have done something similar before, but I can't find the code. I can remember, I will update the answer with some stubbed out code.

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.