In the footer of a view, I have created code that serializes the current view:

global $current_view;
$saved_view = serialize($current_view);

I then save the serialized object in a node. When I retrieve the node, I unserialize the view object:

$retrieved_view = unserialize($saved_view);

If I look at the object(print_r()), it appears to have all the view elements. However, when I try to build the view, I get nothing:

$output = views_build_view('embed', $retrieved_view, array(), false, false);
print $output;

Does the view object need to be initialized as a view object?

The reason I am saving this object is because it contains filters that have been applied by the end user using exposed filters. So, essentially, I'm saving a customized version of the original view (which is why I don't simply use views_get_view to call the original view.

Any suggestions would be appreciated.

Comments

somebodysysop’s picture

Any help on this? If I execute this code as a footer to a view, it works:

// Placed in the footer of a view;
// Get the current view, serialize, unserialize, then extract items.
// This is simply to test if unserialized view object will work.
	
	global $current_view;
	$serialized_view = serialize($current_view);
	$unserialized_view = unserialize($serialized_view);
        $view_result = views_build_view('items',$unserialized_view,array());
        $view_items = $view_result['items'];
	print_r($view_items); // I see the items

However, this code, placed in a node, does not work. What else do I need to do?:

// PHP code placed in node.
// Get the serialized view from a node, unserialize, then extract items.
// 
	
        $node = node_load(150);
	$serialized_view = $node->field_serialized_view[0]['value'];
	$unserialized_view = unserialize($serialized_view);
        $view_result = views_build_view('items',$unserialized_view,array());
        $view_items = $view_result['items'];
	print_r($view_items); // I don't see the items

I compared the views object in both cases -- they are the exact same. But, as I said, in the footer of a view, the views_build_view function works, but in a regular node, it does not -- using the exact same views object.

Can someone please help?

merlinofchaos’s picture

I'm not ignoring this; however, I don't understand why you're getting the results you do. As far as I can tell, that should basically not happen.

All I can suggest is to trace into views_build_view and see if you can figure out what's going on. Or when I have some time to do so, I can do this, but I warn you it's not as high a priority as a lot of other items right now, so it could be some time before I'm able.

somebodysysop’s picture

Thanks. I understand.

By trace, do you mean use the trace.module? If so, no 5.x version of it is available. If not, what else could I use to trace what's happening in that function?

Again, thanks.

merlinofchaos’s picture

Generally I trace either using xdebug (which takes a lot of setup) or by putting dprint statements in the function and seeing what gets printed.

somebodysysop’s picture

I have devel installed, so I turned on error backtrace. Discovered the problem, but don't know how to fix it.

As I said, the view object is the exact same in the view footer as in the node where I'm trying to extract the view items. Before I call view_build_views, I examine the view object, and it contains the correct filter elements in the array: $unserialized_view->used_filters.

However, when I use the devel module to look at the view array processed by view_build_views, the used_filters array is empty. In fact, that is the only thing different between the view object and what I see in the array returned by view_build_views (using the devel module). Since the view contains an exposed_filter, it goes without saying that that no items are going to appear in the view unless there are used_filters to process.

I tried to troubleshoot this myself, and the only place I see where this problem could potentially occur is here, in views_query.inc, where the used_filters array is specifically blanked out::

/*
 * Take all the filters in a view and add them to the query object.
 */
function _views_view_build_filters(&$query, &$view) {
  $filters = _views_get_filters();
  $view->used_filters = array();

  foreach ($view->filter as $i => $filter) {
    $filterinfo = $filters[$filter['field']]; // shortcut
    $field = $filterinfo['field'];
    if (!$field) {
      $fieldbits = explode('.', $filter['field']);
      $field = $fieldbits[1];
    }
    $filterinfo['field'] = $field;
  ...

Apparently, when my snippet is ran in the view footer, _views_get_filters() gets the used_filters correctly. However, when it is ran in the node, the used_filters are lost, the therefore no view items can be retrieved.

In my mind, it seems like there is some setting in the general environment that is present when code is executed in the footer that is not present when in a node, and the absence of this setting is causing the used_filters to be lost.

Can you give me some suggstions as to how to proceed from here?

sun’s picture

Status: Active » Closed (won't fix)

Sorry, this support request is way too specific. Please have a look at the issue queue - Views maintainers are buried already.