3

Im using "Views field view" module to display a secondary view inside a main view. The secondary view has a contextual filter. The problem is that when Im trying to set up no result condition it doesnt show up. I have tried this cases:

  • Secondary view no result: nothing

    Main view no result (In settings of the field with sec. view): text

    Output: nothing

  • Secondary view no result: 0

    Main view no result (read "0" as no result option enabled): text

    Output: nothing

  • Secondary view no result: text

    Main view no result: nothing

    Output: text

In the last case views display the text but if it is an html with arguments from the main view the tokens wont be converted. If no result case is set to display 0 it will display nothing.

Views           7.x-3.0-rc3
Views Field View    7.x-1.x-dev
Drupal          7.9
  • 1
    The only problem Im currently facing with this issue is "How to set up Views to display zero number as output for a no result case?" even if it is a php code with echo 0; it will display nothing and not 0 – Alex Dec 14 '11 at 16:11
  • In setup of views field there is an option "Count the number 0 as empty" Im trying to pass No result condition from 1 view to another so it would be able to read this 0 case and display a text with token instead of displaying secondary view. – Alex Dec 14 '11 at 16:29
1

How does Views know that The View Created By The Views Field View Module is not returning a result - on a row-by-row basis? I will hypothesize that Views is probably able to suppress the output of a field because very early on the process of creating/rendering the View, when the Views query is done, that field comes empty-handed from the database, and somewhere, internally, Views says: NO SHOW.

In my experience, I have never heard of a Views hook that allows a third-party module to short-wire this process and for example, arbitrarily command Views to render a field as it were populated, even if it is not, because a third-party module used that field as a contextual argument, created a view (or got data from somewhere else), and inserted that data into that otherwise empty field. This is what are you trying to do, and simply put, I am not sure if it is possible at all, at least in the way that you are trying to do it.

It would be possible however, for you to:

  1. Create a primary view.
  2. Create a custom preprocessor or template for that field. You would look for the proper name of the template by clicking your View's theme information link. A few caveats: For a preprocessor to be called, a matching template must exist first; Views won't recognize the preprossor (won't call it) if you declare it in a custom module, you will need to put it in your template.php; It isn't really necessary for you to create a preprocessor, however if you want to keep your templates clean (and any Drupalista worth his salt should want to) a preprocessor implementation is in order.
  3. In your preprocessor, if that field is not empty, pass the results of that field to your secondary View's filter. This will require you to instantiate that View programatically.
  4. If your secondary View returns any results, append those results to the current field.

So in essence, I think that what you want to do is 100% possible. I think your requirement will be possible (with Views) depending on A) How comfortable you are hacking with Drupal and it's theming layer, B) The performance requirements of your application.

If you are trying to do this for a high-performance site with a large number of visitors, I would be very wary of the solution that I am proposing, even with full Views caching turned on. So for performance and simplicity's sake, you can make your own database queries the ol' fashioned way. IMHO, your web server's CPU and whoever interacts with your site, clients and coders included, will appreciate it. Otherwise, if you still decide to go with my solution, here's a PROTIP:

  1. Go to the Views advanced settings options.
  2. Select show query.
  3. Go back to your primary and secondary Views and test them. The query that is used to construct your Views will be revealed to you.
  4. Copypasta this into your custom module for usage with db_query().
  5. ...
  6. Profit!
| improve this answer | |
0

Your problem is that your secondary view, even if not having any result row, will return an empty enclosing tag for the view itself. This is different from the way normal fields work (and you may consider filing an issue report). In effect, the secondary view output is different from both the empty string and 0, meaning that it cannot trigger the "No results behavior" of your primary view as you want to.

As a solution, you can create a tiny module that will post-process the output in this case, to make it indeed empty. You have to implement hook_views_post_render(), like this:

function mymodule_views_post_render(&$view, &$output, &$cache) {

  if ($view->name == 'your_views_name' && count($view->result) == 0) {
    $output = '';
  }
}

In addition, you may need to implement hook_views_api() to declare using the Views API, not sure about it. For other ways to check in code if the view result is empty, see Drupal issue #446798.

| improve this answer | |

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.