2

I've created a view block with views3. When I attach the views block to a node, I want to know if the node is sticky and if the content type is "lodging". If the criteria are met, do not display view result.

In the views filter (php field) I've created the following code.

if (arg(0) == 'node') {
  $nid = arg(1);
  $node = node_load($nid); // get node id
}
$sticky_status = $node->sticky;  //  get sticky status
$node_type = $node->type;  // get node type
if ($sticky_status == '1' && $node_type == 'lodging') // condition if sticky is 1 (active) and node type is "lodging"
{
  $view->result = null;  // do not display views
}

The code is working fine and I get the desired result, however I also get the following warning message:

Warning: Invalid argument supplied for foreach() in views_php_handler_filter->php_post_execute() (line 84 of /var/www/html/planetlodge/sites/all/modules/views_php/plugins/views/views_php_handler_filter.inc).
Warning: array_slice() expects parameter 1 to be array, null given in views_php_plugin_pager->post_execute() (line 46 of /var/www/html/planetlodge/sites/all/modules/views_php/plugins/views/views_php_plugin_pager.inc).
Warning: array_values() expects parameter 1 to be array, null given in view->execute() (line 1155 of /var/www/html/planetlodge/sites/all/modules/views/includes/view.inc).

Where is the error in the code ?

5

The error is here:

$view->result = null;

Try changing it to:

$view->result = array();

Views is running over the $view->result var as an array without checking its type, but it's always expecting an array. As NULL isn't an array it complains, but it's just a Warning so the script continues. Because the results loop has failed you get the same effect as when the array is empty, and as you say everything works as expected.

  • Thank you so much. Very fast answer and the warning is gone. Thank you, thank you – Toci Apr 23 '13 at 17:34
  • you should mark it correct if this solved your problem, to make it useful for others :) – nit3ch Apr 23 '13 at 17:39

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.