You are here

function views_json_get in Views Datasource 7

Same name and namespace in other branches
  1. 6 views_json.module \views_json_get()

Gets JSON data from a View rendered in the JSON data document style.

This is useful for when working with a JSON view in code.

Parameters

$name: The name of the view.

$display_id: The display of the view to use.

$args: The arguments to pass to the view.

$raw: If TRUE, the JSON data is returned as a string. Otherwise, an object representation is returned.

$current_page: The page to be fetched.

Return value

The JSON data in the form of an object or a string or NULL otherwise.

File

./views_json.module, line 366

Code

function views_json_get($name, $display_id = 'default', $args = array(), $raw = FALSE, $current_page = 0) {
  $view = views_get_view($name);
  if (!is_object($view)) {
    return NULL;
  }
  $view
    ->set_current_page($current_page);
  $preview = $view
    ->preview($display_id, $args);
  $start_pos = strpos($preview, '{');
  $finish_pos = strrpos($preview, '}');
  $length = $finish_pos - $start_pos + 1;
  $json = trim(substr($preview, $start_pos, $length));
  if ($raw) {
    return $json;
  }
  return json_decode($json);
}