| 1 |
<?php
|
| 2 |
//$Id $
|
| 3 |
/**
|
| 4 |
* @file views_json.views.inc
|
| 5 |
* Views style plugin to render nodes in the JSON data format.
|
| 6 |
* @see views-view-json.tpl.php views-view-row-unformatted.tpl.php
|
| 7 |
* @ingroup views_plugins
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_views_plugin
|
| 12 |
*
|
| 13 |
*/
|
| 14 |
function views_json_views_plugins() {
|
| 15 |
return array(
|
| 16 |
'style' => array( //declare the views_json style plugin
|
| 17 |
'views_json' => array(
|
| 18 |
'title' => t('JSON data document'),
|
| 19 |
'theme' => 'views_view_json',
|
| 20 |
'help' => t('Displays nodes in the JSON data format.'),
|
| 21 |
'handler' => 'views_plugin_style_json',
|
| 22 |
'uses row plugin' => TRUE,
|
| 23 |
'uses fields' => TRUE,
|
| 24 |
'uses options' => TRUE,
|
| 25 |
'type' => 'normal',
|
| 26 |
),
|
| 27 |
),
|
| 28 |
'row' => array( //declare the unformatted row plugin
|
| 29 |
'unformatted' => array(
|
| 30 |
'title' => t('Unformatted'),
|
| 31 |
'help' => t('(Displays the unformatted data for each row from the views query with each row on a new line. Set as | for views_json.'),
|
| 32 |
'handler' => 'views_plugin_row_unformatted',
|
| 33 |
'theme' => 'views_view_row_unformatted',
|
| 34 |
'uses fields' => TRUE,
|
| 35 |
'uses options' => TRUE,
|
| 36 |
'type' => 'normal',
|
| 37 |
)
|
| 38 |
)
|
| 39 |
);
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Implementation of views_plugin_style
|
| 44 |
*
|
| 45 |
*/
|
| 46 |
|
| 47 |
class views_plugin_style_json extends views_plugin_style {
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Set default options
|
| 51 |
*/
|
| 52 |
function options(&$options) {
|
| 53 |
$options['format'] = 'Exhibit';
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Provide a form for setting options.
|
| 58 |
*
|
| 59 |
* @param array $form
|
| 60 |
* @param array $form_state
|
| 61 |
*/
|
| 62 |
function options_form(&$form, &$form_state) {
|
| 63 |
$form['format'] = array(
|
| 64 |
'#type' => 'radios',
|
| 65 |
'#title' => t('JSON data format'),
|
| 66 |
'#options' => array('Exhibit' => t('MIT Simile/Exhibit'), 'Canonical' => t('Canonical'), 'JSONP' => t('JSONP')),
|
| 67 |
'#default_value' => $this->options['format'],
|
| 68 |
);
|
| 69 |
}
|
| 70 |
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Theme preprocess function for views-view-json.tpl.php
|
| 75 |
*
|
| 76 |
* @param array $vars
|
| 77 |
*/
|
| 78 |
function template_preprocess_views_view_json(&$vars) {
|
| 79 |
$view = &$vars['view'];
|
| 80 |
$options = $view->style_handler->options;
|
| 81 |
$handler = $view->style_handler;
|
| 82 |
}
|
| 83 |
|
| 84 |
|
| 85 |
/**
|
| 86 |
* Implementation of views_row_plugin
|
| 87 |
*
|
| 88 |
*/
|
| 89 |
class views_plugin_row_unformatted extends views_plugin_row {
|
| 90 |
|
| 91 |
/**
|
| 92 |
* Set default options
|
| 93 |
*
|
| 94 |
* @param array $options
|
| 95 |
*/
|
| 96 |
function options(&$options) {
|
| 97 |
$options['separator'] = '|';
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Provide a form for setting options.
|
| 102 |
*/
|
| 103 |
function options_form(&$form, &$form_state) {
|
| 104 |
$fields = $this->display->handler->get_option('fields');
|
| 105 |
$options = array();
|
| 106 |
foreach ($fields as $field => $info) {
|
| 107 |
$handler = views_get_handler($info['table'], $info['field'], 'field');
|
| 108 |
if ($handler) {
|
| 109 |
$options[$field] = $handler->ui_name();
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
| 113 |
$form['separator'] = array(
|
| 114 |
'#title' => t('Separator'),
|
| 115 |
'#type' => 'textfield',
|
| 116 |
'#size' => 10,
|
| 117 |
'#default_value' => isset($this->options['separator']) ? $this->options['separator'] : ',',
|
| 118 |
'#description' => t('The separator is placed between fields.'),
|
| 119 |
);
|
| 120 |
}
|
| 121 |
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
| 125 |
* Theme preprocess function for views-view-row-unformatted.tpl.php
|
| 126 |
*/
|
| 127 |
function template_preprocess_views_view_row_unformatted(&$vars) {
|
| 128 |
$view = $vars['view'];
|
| 129 |
//print('preprocess');
|
| 130 |
// Loop through the fields for this view.
|
| 131 |
foreach ($view->field as $id => $field) {
|
| 132 |
if (!empty($field['handler']) && is_object($field['handler'])) {
|
| 133 |
$object = new stdClass();
|
| 134 |
$object->content = $field['handler']->theme($vars['row']);
|
| 135 |
if (isset($field['handler']->field_alias) && isset($vars['row']->{$field['handler']->field_alias})) {
|
| 136 |
$object->raw = $vars['row']->{$field['handler']->field_alias};
|
| 137 |
}
|
| 138 |
else {
|
| 139 |
$object->raw = NULL; // make sure it exists to reduce NOTICE
|
| 140 |
}
|
| 141 |
if (!empty($vars['options']['separator']) && $object->content) {
|
| 142 |
$object->separator = filter_xss_admin($vars['options']['separator']);
|
| 143 |
}
|
| 144 |
|
| 145 |
$object->handler = $field['handler'];
|
| 146 |
$object->class = views_css_safe($id);
|
| 147 |
$object->label = check_plain($field['handler']->label());
|
| 148 |
$vars['fields'][$id] = $object;
|
| 149 |
}
|
| 150 |
}
|
| 151 |
|
| 152 |
}
|