You are here

class views_plugin_style_json in Views Datasource 7

Same name and namespace in other branches
  1. 6 views_plugin_style_json.inc \views_plugin_style_json

Implements views_plugin_style

Hierarchy

Expanded class hierarchy of views_plugin_style_json

1 string reference to 'views_plugin_style_json'
views_json_views_plugins in views/views_json.views.inc
Implements hook_views_plugins().

File

views/plugins/views_plugin_style_json.inc, line 10
Implements views_plugin_style for views_json

View source
class views_plugin_style_json extends views_plugin_style {

  /**
   * Implements views_plugin_style::option_definition
   */
  function option_definition() {
    $options = parent::option_definition();
    $options['root_object'] = array(
      'default' => 'nodes',
      'translatable' => FALSE,
    );
    $options['top_child_object'] = array(
      'default' => 'node',
      'translatable' => FALSE,
    );
    $options['field_output'] = array(
      'default' => 'normal',
      'translatable' => FALSE,
    );
    $options['plaintext_output'] = array(
      'default' => TRUE,
      'translatable' => FALSE,
    );
    $options['remove_newlines'] = array(
      'default' => NULL,
      'translatable' => FALSE,
    );
    $options['format'] = array(
      'default' => 'simple',
      'translatable' => FALSE,
    );
    $options['jsonp_prefix'] = array(
      'default' => NULL,
      'translatable' => FALSE,
    );
    $options['content_type'] = array(
      'default' => 'default',
      'translatable' => FALSE,
    );
    $options['using_views_api_mode'] = array(
      'default' => FALSE,
      'translatable' => FALSE,
    );
    $options['translate_labels'] = array(
      'default' => FALSE,
      'translatable' => FALSE,
    );

    // Encoding options, to be used by json_encode().
    $options['encoding'] = array(
      'contains' => array(
        'object_arrays' => array(
          'default' => NULL,
          'translatable' => FALSE,
          'bitmask' => 'JSON_FORCE_OBJECT',
        ),
        'numeric_strings' => array(
          'default' => NULL,
          'translatable' => FALSE,
          'bitmask' => 'JSON_NUMERIC_CHECK',
        ),
        'bigint_string' => array(
          'default' => NULL,
          'translatable' => FALSE,
          'bitmask' => 'JSON_BIGINT_AS_STRING',
        ),
        'pretty_print' => array(
          'default' => NULL,
          'translatable' => FALSE,
          'bitmask' => 'JSON_PRETTY_PRINT',
        ),
        'unescaped_slashes' => array(
          'default' => NULL,
          'translatable' => FALSE,
          'bitmask' => 'JSON_UNESCAPED_SLASHES',
        ),
        'unescaped_unicode' => array(
          'default' => NULL,
          'translatable' => FALSE,
          'bitmask' => 'JSON_UNESCAPED_UNICODE',
        ),
        'char_encoding' => array(
          'default' => NULL,
          'translatable' => FALSE,
          'bitmasks' => array(
            'JSON_HEX_TAG' => 'JSON_HEX_TAG',
            'JSON_HEX_APOS' => 'JSON_HEX_APOS',
            'JSON_HEX_QUOT' => 'JSON_HEX_QUOT',
            'JSON_HEX_AMP' => 'JSON_HEX_AMP',
          ),
        ),
      ),
    );

    // Provide Views a flatted copy of 'encoding' in a format it knows how to save and retrieve user data to.
    foreach ($options['encoding']['contains'] as $key => $encoding_option) {
      $options[$key] = $encoding_option;
    }
    return $options;
  }

  /**
   * Provide a form for setting options.
   */
  function options_form(&$form, &$form_state) {

    // Grouping options
    $options = array(
      '' => t('- None -'),
    );
    $field_labels = $this->display->handler
      ->get_field_labels(TRUE);
    $options += $field_labels;

    // If there are no fields, we can't group on them.
    if (count($options) > 1) {

      // This is for backward compability, when there was just a single select form.
      if (is_string($this->options['grouping'])) {
        $grouping = $this->options['grouping'];
        $this->options['grouping'] = array();
        $this->options['grouping'][0]['field'] = $grouping;
      }
      if (isset($this->options['group_rendered']) && is_string($this->options['group_rendered'])) {
        $this->options['grouping'][0]['rendered'] = $this->options['group_rendered'];
        unset($this->options['group_rendered']);
      }
      $i = 0;
      $grouping = !empty($this->options['grouping'][$i]) ? $this->options['grouping'][$i] : array();
      $grouping += array(
        'field' => '',
        'rendered' => TRUE,
        'rendered_strip' => FALSE,
      );
      $form['grouping'][$i]['field'] = array(
        '#type' => 'select',
        '#title' => t('Grouping field'),
        '#options' => $options,
        '#default_value' => $grouping['field'],
        '#description' => t('You may optionally specify a field by which to group the records. Leave blank to not group.'),
      );
    }
    $form['root_object'] = array(
      '#type' => 'textfield',
      '#title' => t('Root object name'),
      '#default_value' => $this->options['root_object'],
      '#description' => t('The name of the root object in the JSON document. e.g nodes or users or forum_posts'),
    );
    $form['top_child_object'] = array(
      '#type' => 'textfield',
      '#title' => t('Top-level child object'),
      '#default_value' => $this->options['top_child_object'],
      '#description' => t('The name of each top-level child object in the JSON document. e.g node or user or forum_post'),
    );
    $form['field_output'] = array(
      '#type' => 'radios',
      '#title' => t('Field output'),
      '#description' => t('For each row in the view, fields can be output as either the field rendered by Views, or by the raw content of the field.'),
      '#options' => array(
        'normal' => t('Normal'),
        'raw' => t('Raw'),
      ),
      '#default_value' => $this->options['field_output'],
    );
    $form['plaintext_output'] = array(
      '#type' => 'checkbox',
      '#title' => t('Plaintext output'),
      '#default_value' => $this->options['plaintext_output'],
      '#description' => t('For each row in the view, strip all markup from the field output.'),
    );
    $form['remove_newlines'] = array(
      '#type' => 'checkbox',
      '#title' => t('Remove newlines'),
      '#default_value' => $this->options['remove_newlines'],
      '#description' => t('Strip newline characters from the field output.'),
    );
    $form['format'] = array(
      '#type' => 'radios',
      '#title' => t('JSON data format'),
      '#options' => views_json_views_formats(),
      '#default_value' => $this->options['format'],
      '#description' => t('What object format will be used for JSON output.'),
    );

    // Hide "simple_object" option when view returns more than one row.
    $pager = $this->view->display_handler
      ->get_option('pager');
    if ($pager['options']['items_per_page'] != 1) {
      unset($form['format']['#options']['simple_object']);
      $form['format']['#default_value'] = 'simple';
    }
    $form['jsonp_prefix'] = array(
      '#type' => 'textfield',
      '#title' => t('JSONP prefix'),
      '#default_value' => $this->options['jsonp_prefix'],
      '#description' => t('If used the JSON output will be enclosed with parentheses and prefixed by this label, as in the JSONP format.'),
    );
    $form['content_type'] = array(
      '#type' => 'radios',
      '#title' => t('Content-Type'),
      '#options' => array(
        'default' => t('Default: application/json'),
        'text/json' => t('text/json'),
        'application/javascript' => t('application/javascript'),
      ),
      '#default_value' => $this->options['content_type'],
      '#description' => t('The Content-Type header that will be sent with the JSON output.'),
    );
    $form['using_views_api_mode'] = array(
      '#type' => 'checkbox',
      '#title' => t('Views API mode'),
      '#default_value' => $this->options['using_views_api_mode'],
      '#description' => t('With Views API mode the JSON will embedded as normal content so normal page processing is used. Leave it unchecked when JSON should be printed directly to the client.'),
    );
    $form['translate_labels'] = array(
      '#type' => 'checkbox',
      '#title' => t('Translate labels'),
      '#default_value' => $this->options['translate_labels'],
      '#description' => t('Allow labels to be translated'),
    );

    // JSON encoding options.
    $form['object_arrays'] = array(
      '#type' => 'checkbox',
      '#title' => t('Object arrays'),
      '#default_value' => $this->options['object_arrays'],
      '#description' => t('Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty.'),
    );
    $form['numeric_strings'] = array(
      '#type' => 'checkbox',
      '#title' => t('Numeric strings'),
      '#default_value' => $this->options['numeric_strings'],
      '#description' => t('Encodes numeric strings as numbers.'),
    );
    $form['bigint_string'] = array(
      '#type' => 'checkbox',
      '#title' => t('Numeric strings'),
      '#default_value' => $this->options['bigint_string'],
      '#description' => t('Encodes large integers as their original string value.'),
    );
    $form['pretty_print'] = array(
      '#type' => 'checkbox',
      '#title' => t('Pretty print'),
      '#default_value' => $this->options['pretty_print'],
      '#description' => t('Use whitespace in returned data to format it.'),
    );
    $form['unescaped_slashes'] = array(
      '#type' => 'checkbox',
      '#title' => t('Unescaped slashes'),
      '#default_value' => $this->options['unescaped_slashes'],
      '#description' => t("Don't escape forward slashes <b>/</b>."),
    );
    $form['unescaped_unicode'] = array(
      '#type' => 'checkbox',
      '#title' => t('Unescaped unicode'),
      '#default_value' => $this->options['unescaped_unicode'],
      '#description' => t('Encode multibyte Unicode characters literally (default is to escape as \\uXXXX). '),
    );
    $form['char_encoding'] = array(
      '#type' => 'select',
      '#title' => t('Hexadecimal (base 16) encoding'),
      '#options' => array(
        'JSON_HEX_TAG' => t('Encode tags'),
        'JSON_HEX_APOS' => t('Encode apostrophe'),
        'JSON_HEX_QUOT' => t('Encode quotes'),
        'JSON_HEX_AMP' => t('Encode ampersand'),
      ),
      '#multiple' => TRUE,
      '#default_value' => $this->options['char_encoding'],
      '#description' => t('You can combine multiple options.'),
    );

    // Only enable options supported by the current PHP version.
    if (!(PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 4)) {
      $php5_4_ops = array(
        'bigint_string',
        'pretty_print',
        'unescaped_slashes',
        'unescaped_unicode',
      );
      foreach ($php5_4_ops as $op) {
        $form[$op]['#disabled'] = TRUE;
        $form[$op]['#description'] .= ' <b>Requires PHP 5.4 or greater</b>.';
      }
    }
  }

  /**
   * Implementation of view_style_plugin::theme_functions(). Returns an array of theme functions to use
   * for the current style plugin
   * @return array
   */
  function theme_functions() {
    $options = $this->options;
    $hook = 'views_views_json_style_' . $options['format'];
    return views_theme_functions($hook, $this->view, $this->display);
  }

  /**
   * Implementation of views_style_plugin::additional_theme_functions(). Returns empty array.
   * @return array
   */
  function additional_theme_functions() {
    return array();
  }

  /**
   * Implementation of view_style_plugin::render()
   */
  function render() {
    $view = $this->view;
    $options = $this->options;
    $field = $view->field;
    $rows = array();
    foreach ($view->result as $count => $row) {
      $view->row_index = $count;
      $field_output = _views_json_render_fields($view, $row);
      drupal_alter('views_json_render_row', $field_output, $view, $row);
      $rows[] = $field_output;
    }
    unset($view->row_index);
    return theme($this
      ->theme_functions(), array(
      'view' => $view,
      'options' => $options,
      'rows' => $rows,
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::options_submit public function Handle any special handling on the validate form. 9
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::summary_title public function Returns the summary of the settings in the display. 8
views_plugin_style::$row_plugin public property The row plugin, if it's initialized and the style itself supports it.
views_plugin_style::$row_tokens public property Store all available tokens row rows.
views_plugin_style::build_sort public function Called by the view builder to see if this style handler wants to interfere with the sorts. If so it should build; if it returns any non-TRUE value, normal sorting will NOT be added to the query. 1
views_plugin_style::build_sort_post public function Called by the view builder to let the style build a second set of sorts that will come after any other sorts in the view. 1
views_plugin_style::destroy public function Destructor. Overrides views_object::destroy
views_plugin_style::even_empty public function Should the output of the style plugin be rendered even if it's empty. 1
views_plugin_style::get_field public function Get a rendered field.
views_plugin_style::get_field_value public function Get the raw field value.
views_plugin_style::get_row_class public function Return the token replaced row class for the specified row.
views_plugin_style::init public function Initialize a style plugin.
views_plugin_style::options_validate public function Validate the options form. Overrides views_plugin::options_validate
views_plugin_style::pre_render public function Allow the style to do stuff before each row is rendered.
views_plugin_style::query public function Add anything to the query that we might need to. Overrides views_plugin::query 2
views_plugin_style::render_fields public function Render all of the fields for a given style and store them on the object.
views_plugin_style::render_grouping public function Group records as needed for rendering.
views_plugin_style::render_grouping_sets public function Render the grouping sets.
views_plugin_style::tokenize_value public function Take a value and apply token replacement logic to it.
views_plugin_style::uses_fields public function Return TRUE if this style also uses fields.
views_plugin_style::uses_row_class public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_row_plugin public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_tokens public function Return TRUE if this style uses tokens.
views_plugin_style::validate public function Validate that the plugin is correct and can be saved. Overrides views_plugin::validate
views_plugin_style_json::additional_theme_functions function Implementation of views_style_plugin::additional_theme_functions(). Returns empty array. Overrides views_plugin::additional_theme_functions
views_plugin_style_json::options_form function Provide a form for setting options. Overrides views_plugin_style::options_form
views_plugin_style_json::option_definition function Implements views_plugin_style::option_definition Overrides views_plugin_style::option_definition
views_plugin_style_json::render function Implementation of view_style_plugin::render() Overrides views_plugin_style::render
views_plugin_style_json::theme_functions function Implementation of view_style_plugin::theme_functions(). Returns an array of theme functions to use for the current style plugin Overrides views_plugin::theme_functions