You are here

function views_plugin_style_json::options_form in Views Datasource 7

Same name and namespace in other branches
  1. 6 views_plugin_style_json.inc \views_plugin_style_json::options_form()

Provide a form for setting options.

Overrides views_plugin_style::options_form

File

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

Class

views_plugin_style_json
Implements views_plugin_style

Code

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>.';
    }
  }
}