Hide links to empty views

BWPanda - June 23, 2009 - 06:49
Project:Views
Version:6.x-2.6
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I'm trying to get the results of a view using PHP, but the result of views_get_view always seems to be empty, even though there are nodes in that view...

The code I'm using is as follows:

<?php
  $my_view
= views_get_view('Activities_Accommodation');
 
$my_view->set_display('page_1');
 
$my_view->pre_execute($fields['title']->raw);
 
$my_view->execute();
 
dsm($my_view->result);
?>

When I go to the view in question with the argument in the URL, I see two nodes; however the output of dsm() is an empty array. What am I doing wrong?

#1

BWPanda - July 2, 2009 - 03:16
Title:views_get_view result always empty...» Hide links to empty views

Still having issues with this...

To explain what I'm wanting:
I have 2 content types: Activities and Areas. One of the fields in an Activity is a node_reference to an Area (i.e. activities are located in a specific area). I then have 2 views to display the nodes from each content type. The Areas view displays the area nodes whose titles I've set to link to the Activities view.

The problem occurs, however, when there aren't any Activity nodes in a given Area - clicking on the Area title takes you to an empty view. This is not desirable, so I'm trying to make it so that only Areas that have associated Activity nodes are displayed.

I'm now trying to use the following code, but it's still producing empty results:

<?php
  $my_view
= views_get_view('Activities_Accommodation');
 
$my_view->set_arguments(array($fields['title']->raw));
 
$my_view->execute('page_1');
 
dsm($my_view->result);
?>

If anyone could help me out, or tell me of a better/easier way to do what I'm wanting, it'd be much appreciated!

#2

dereine - July 2, 2009 - 20:12

did you forgot pre_execute() ? its definitive needed!

#3

BWPanda - July 2, 2009 - 23:48
Status:active» fixed

This code produced the following error:

<?php
  $my_view
= views_get_view('Activities');
 
$my_view->pre_execute(array($fields['title']->raw));
 
$my_view->execute('page_1');
 
dsm($my_view->result);
?>

Fatal error: Call to a member function pre_execute() on a non-object in /var/www/drupal/sites/default/modules/views/includes/view.inc on line 928

But adding $my_view->set_display(); just before the pre_execute() did the trick.
Thanks dereine!

#4

System Message - July 16, 2009 - 23:50
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.