You are here

function rb_misc_action_views_load_node in Rules Bonus Pack 7

Same name and namespace in other branches
  1. 6 rb_misc.module \rb_misc_action_views_load_node()

The 'rb_misc_action_views_load_node' action.

File

./rb_misc.rules.inc, line 737
Miscellaneous conditions and actions for Rules.

Code

function rb_misc_action_views_load_node($view, $args) {

  // Prepare some data about the view.
  $views_settings = explode('|', $view);
  $view_name = $views_settings[0];
  $display_name = $views_settings[1];
  $view_arguments = explode("\r", $args);

  // Load the view and set the properties.
  $view = views_get_view($view_name);
  $view
    ->set_display($display_name);
  $view
    ->set_arguments($view_arguments);
  $view
    ->set_items_per_page(1);
  $view
    ->execute();

  // Check if we have any results from the view, if so return the first one.
  if (count($view->result)) {
    $first_result = $view->result[0];
    $node = node_load($first_result->nid);
    return array(
      'node' => $node,
    );
  }
  else {

    // No results. :-(
    return;
  }
}