Active
Project:
Views Datasource
Version:
7.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
27 Sep 2011 at 14:39 UTC
Updated:
27 Sep 2011 at 14:42 UTC
I found it useful to write the data to a variable with a views style in order to access it from the client side (ideal for integration with a JS data library like underscore).
After commenting out the image replacement regular expressions in views_json.module (http://drupal.org/node/913668#comment-4690036),
I just added the following code as a views style output theme named 'views-views-json-style-simple.tpl.php'
<?php
/**
* @file views-views-json-style-simple.tpl.php
* Default template for the Views JSON style plugin using the simple format
*
* Variables:
* - $view: The View object.
* - $rows: Hierachial array of key=>value pairs to convert to JSON
* - $options: Array of options for this style
*
* @ingroup views_templates
*/
$jsonp_prefix = $options['jsonp_prefix'];
if ($view->override_path) {
// We're inside a live preview where the JSON is pretty-printed.
$json = _views_json_encode_formatted($rows);
if ($jsonp_prefix) $json = "$jsonp_prefix($json)";
print "<code>$json</" . "code>";
}
else {
$json = json_encode($rows);
if ($jsonp_prefix) $json = "$jsonp_prefix($json)";
if ($options['using_views_api_mode']) {
// We're in Views API mode.
print "<script type='text/javascript'>
Drupal.views_data = Drupal.views_data || [];
Drupal.views_data.push($json);
var elems = document.querySelectorAll('body script');
for (var i = 0; i < elems.length; i++)
elems[i].parentNode.removeChild(elems[i]);
</script>";
}
else {
// We want to send the JSON as a server response so switch the content
// type and stop further processing of the page.
$content_type = ($options['content_type'] == 'default') ? 'application/json' : $options['content_type'];
drupal_add_http_header("Content-Type", "$content_type; charset=utf-8");
print $json;
//Don't think this is needed in .tpl.php files: module_invoke_all('exit');
exit;
}
}