You are here

function template_preprocess_views_views_json_style_simple in Views Datasource 6

Same name and namespace in other branches
  1. 7 views/theme/views_views_json_style.theme.inc \template_preprocess_views_views_json_style_simple()

@file Views theme to render view fields as JSON.

  • $view: The view in use.
  • $rows: Array of row objects as rendered by _views_json_render_fields
  • $attachment: Not used currently
  • $options: The options for the style passed in from the UI.

See also

views_json.views.inc

File

theme/views_views_json_style.theme.inc, line 16
Views theme to render view fields as JSON.

Code

function template_preprocess_views_views_json_style_simple(&$vars) {
  $view = $vars["view"];
  $rows = $vars["rows"];
  $options = $vars["options"];
  $base = $view->base_table;
  $root_object = $options["root_object"];
  $top_child_object = $options["top_child_object"];
  $plaintext_output = $options["plaintext_output"];
  $objects = array();
  foreach ($rows as $row) {
    $object = array(
      $top_child_object => array(),
    );

    /*Convert the $rows into a hierachial key=>value array*/
    foreach ($row as $field) {
      if ($options["field_output"] == "normal") {
        if ($field->label) {
          $label = $plaintext_output ? strip_tags($field->label) : $field->label;
        }
        else {
          $label = $plaintext_output ? strip_tags($field->id) : $field->id;
        }
        if (!$field->is_multiple) {
          $content = $plaintext_output ? strip_tags(html_entity_decode($field->content, ENT_QUOTES)) : $field->content;
          $content = mb_check_encoding($content, 'UTF-8') ? $content : utf8_encode($content);
        }
        else {
          $content = array();
          foreach ($field->content as $n => $oc) {
            $content[$n] = $plaintext_output ? strip_tags($oc) : $oc;
          }
        }
      }
      elseif ($options["field_output"] == "raw") {
        $label = $plaintext_output ? strip_tags($field->id) : $field->id;
        if (!$field->is_multiple) {
          $content = $plaintext_output ? strip_tags(html_entity_decode($field->content, ENT_QUOTES)) : $field->content;
          $content = mb_check_encoding($content, 'UTF-8') ? $content : utf8_encode($content);
        }
        else {
          $content = array();
          foreach ($field->raw as $n => $oc) {
            $content[$n] = $plaintext_output ? strip_tags($oc) : $oc;
          }
        }
      }
      $object[$top_child_object][$label] = $content;
    }
    $objects[] = $object;
  }
  $vars["rows"] = array(
    $root_object => $objects,
  );
}