I'm trying to figure out how to have, for examples sake, custom_view_2 take the place of custom_view_1 if custom_view_1 is empty.

I guess I should note that each view is it's own block. They are actually cloned views, just displaying different data.

Is there an easy way to accomplish this? I am not too familiar with how to use arguments in views, but for some reason I feel like it might be able to be done through that.

Anyway, if anyone has any idea, I'd totally appreciate the feedback.

Thank you.

Comments

roh846’s picture

Are you looking to display custom_view_2 irrespective of whether custom_view_1 shows or not? Or, do you only want custom_view_2 to show ONLY when custom_view_1 is empty (and hidden), otherwise only custom_view_1 is visible?

If you are attempting to achieve the first objective, then you just have to set "hide view when empty" under the default settings in argument. If you want to do the second thing, the only option that comes to my mind is to embed both views in the page-type.tpl.php file and add an if statement.

jh3’s picture

Yeah, I want to be able to show custom_view_2 only when custom_view_1 is empty and hidden, otherwise view one is visible.

I tried embedding one of the views in the default page.tpl.php file, but it did not include the custom styling for some reason or another. However, what you said is what I was thinking.

Do you happen to know why the styling wouldn't appear with the view if it's embedded?

Thanks.

roh846’s picture

Sorry, I can't comment on the styling..............

You may try doing the styling you want through css.........use a custom div where you embedded the view.

steveadamo’s picture

i know im hijacking here, but in my case, im looking to accomplish goal #2... :)

If you want to do the second thing, the only option that comes to my mind is to embed both views in the page-type.tpl.php file and add an if statement.

steveadamo’s picture

embarassing... i completely missed this thread when i added my own here...

http://drupal.org/node/551914

ill keep an eye on both... ;)

jh3’s picture

Ok, I think I have something that works. It's pretty much what this thread says to do, but slightly different:

// Setup your first view
$view1 = views_get_view('view_name');
$view1->set_display('display_id');
$output_view1 = $view1->preview();

// Setup your second view
$view2 = views_get_view('view_name');
$view2->set_display('display_id');
$output_view2 = $view2->preview();

if ($view1->result) { // If your first view exists, display it
  print $output_view1;
} else { // If not, display your second view
  print $output_view2;
}

This should display view1 if it exists. Otherwise, view2 is printed. In that other thread, if $view1->result returned true, it was doing "return $output;"

I changed it to "print $output" and it started working.

Let me know if this works for anyone.

Also, when printing my views this way, it doesn't inherit any of the CSS that it uses when I regularly enable the view through the blocks page. Both of these views are using the views rotator style plugin, so if anyone knows if this can be passed as an argument or something to make it work when manually embedding views, let me know that as well :D

jh3’s picture

Well, I atleast know why it's not displaying correctly when I embed the node in the page.tpl.php file: the views-rotator.css file isn't being included and neither is the jQuery that controls the rotation.

Does anyone know how to go about telling my page template file to include these things when embedding my views instead of displaying them through the drupal interface?

jh3’s picture

I feel like I'd need to use drupal_add_css() and drupal_add_js() somewhere to include the missing javascript and css files, but I don't know where to include these functions or how to really use them either.

jh3’s picture

Here's another way of doing something...

Say you have two views: one you want to display by default and one you want to display only if the default one is empty. Go to the "Edit" tab for your default view and begin editing the "Empty Text" option. For the input format, make sure it's PHP code.

Now here is where you enter the second view you want to display if your first one is empty. I put this code in and it seemed to work (minus a few things, but for the most part it worked. It even used the views rotator style properly):

$view = views_get_view('your_view_name');
$view->set_display('your_views_display_name');
$output = $view->preview();
print $output;

I tried entering just the views_embed_view function and it didn't work. You might be able to just do $view->preview(); instead of copying it to the $output variable and then printing $output, but I haven't tried that.

Like I said, with the exception of a few styling errors, this seems to work like I thought it would. Try it out.

EDIT:

I'm wrong, the views_embed_view function also seems to work in this case. I don't think I was printing it before when I had it in there :x

print views_embed_view('your_view', $display_id = 'your_display');

jh3’s picture

This seems to have solved my problem. All my styling and javascript issues are fine too. Hope this helps someone else.