How would I go about checking if an embedded view is empty or not? I tried using the empty() function, but since the view has HTML code links to edit, clone, etc., it doesn't appear as being empty even though there's no data there.

The reason I ask is that I'm printing a title above the embedded view which displays regardless of whether there's anything there or not - not ideal. Any assistance would be much appreciated!

Comments

merlinofchaos’s picture

Status: Active » Fixed

You'll need to break apart the views_embed_view function so you can load the view, do the pre_execute() and argument setting, and check $view->result.

Anonymous’s picture

Status: Fixed » Active

What do you mean by 'break apart the function'? Do I need to override it and edit the function code, or do you mean something else?

dawehner’s picture

Status: Active » Fixed
<?php
$view = views_get_view('foo');
$view->set_display('your_displays');
$output = $view->preview();
// At least in $view->result is the result.
if ($view->result) {
  return $output;
}
?>

So this is fixed

Anonymous’s picture

Thanks!

Anonymous’s picture

Might be worth noting (for the benefit of others), that $view->args = array([insert arguments here]); can also be added if needed...
Mine wouldn't work without it.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

laryn’s picture

I basically recreated views_embed_view with some modifications and saved it in my template.php (called theme_embed_view) so I can call it in my .tpl.php files as needed in exactly the same way I was calling the original function. See below:

<?php
function theme_embed_view ($viewname,$display) {
		$args = func_get_args();
		  array_shift($args); // remove $name
		  if (count($args)) {
		    array_shift($args); // remove $display_id
		  }

		$view = views_get_view($viewname);
		$output = $view->preview($display,$args);
		// At least in $view->result is the result.
		if ($view->result) {
		  return $output;
		}
	}
?>

Thanks for the help!

steveadamo’s picture

Thanks for the tips in this thread... unfortunately, i have been unable to get the expected return values when trying this code snippet:

$view = views_get_view('my_view_name');
$view->set_display('default');
$output = $view->preview();
// At least in $view->result is the result.
if ($view->result) {
  print 'results!';
}

my attempts at calling views that i know have results do not display the string... and calling results with no values has a similar effect...

im using 6.x-2.6

dags88’s picture

steveadamo, I had a similar issue, but was able to get the expected results by executing the view as shown on this handbook page: http://drupal.org/node/342132

So my code looks like this:

  $view = views_get_view('view_page') or die('no such view');
  $view->set_display('page_1');
  $view->args = array('myarg');
  $view->pre_execute();
  $view->execute();
  
    if ($view->result) {...

Michsk’s picture

thanks saldag8 and others

LEternity’s picture

This approach doesn't work if you have "excluded" fields in your view. In that case $view->result still shows them :(. Is there any way that I could filter "excluded" fields?

solona’s picture

knalstaaf’s picture

Version: 6.x-2.5 » 7.x-3.x-dev
Status: Closed (fixed) » Active

How is this done in Drupal 7?

DjebbZ’s picture

Status: Active » Fixed

The same way than stated in #1, #3 : analyzing views_embed_view (in views.module) and checking if you have any results.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

hellomobe’s picture

Status: Closed (fixed) » Active

I tried to have "no results behavior" return a php variable.

 $empty = 'empty';
return $empty; 

Then I used if(isset($empty)) in the .tpl file. However, regardless of an empty view result, the variable is not returning/available to the view template I'm themeing. Please let me know where I'm going wrong with this approach.

merlinofchaos’s picture

Status: Active » Closed (fixed)

Templates do not return values, they print them.

This also doesn't seem particularly relevant to the original issue.

marcopanichi’s picture

Comment #7 works for me, thank you.
I just added "return false;" after the last if statement.

h_dries’s picture

Version: 7.x-3.x-dev » 7.x-3.3
Priority: Normal » Minor

#7 worked for me too, thanks

marcopanichi’s picture

#7 doesn't work with my view, containing a custom field (a common multiple files field "attachments").

It doesn't work because $view->result is an object like this:

$result (array)
	0 => object
		nid => 108
		field_data_field_allegati_node_entity_type => 'node'
		_field_data => array(<IT CONTAINS THE NODE>)
		field_field_allegati => array(<VOID>)

PS: in the view edit page, I have only the field "attachments". for test i've added an advanced>no result behaviour text area. but the text area doesn't show, also if the view is empty

easmad’s picture

I was looking for a solution to check and see if a view had any results. I couldn't get any of the above solutions to work, but I came up with this:

<?php
$view = views_get_view_result($name, $display, $args);
$result = count($view);
if ($result) {
//do something here
}
?>
jeuelc’s picture

#21 works for me. thanks eassae!

sanitaM’s picture

Issue summary: View changes

#9 worked for me (7.x), thanks!

mahipal46’s picture

#21 works for me. thanks.

scotwith1t’s picture

+1 for #21! Thanks.

jo_as_neo’s picture

#21 worked for me (D7.39), thanks !

Tharick’s picture

#21 is perfect :)

themaurice’s picture

Hi, #21 works for me too, thanks. less code is alway better.

erica_hall’s picture

Another thumbs up for #21 thank you.