You are here

function template_preprocess_views_views_json_style_exhibit in Views Datasource 7

Same name and namespace in other branches
  1. 6 theme/views_views_json_style.theme.inc \template_preprocess_views_views_json_style_exhibit()

File

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

Code

function template_preprocess_views_views_json_style_exhibit(&$vars) {
  $view = $vars["view"];
  $rows = $vars["rows"];
  $options = $vars["options"];
  $base = $view->base_table;
  $root_object = "Items";
  $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($field->raw) : $field->raw;
        }
        else {
          $content = array();
          foreach ($field->raw as $n => $oc) {
            $content[$n] = $plaintext_output ? strip_tags($oc) : $oc;
          }
        }
      }

      // check if user wants nested arrays
      if (strlen($top_child_object) != 0) {
        $object[$top_child_object][$label] = $content;
      }
      else {
        $object[$label] = $content;
      }
    }
    if (!array_key_exists("label", $object)) {
      $object["label"] = "Item";
    }
    if (!array_key_exists("type", $object)) {
      $object["type"] = $top_child_object;
    }
    $objects[] = $object;
  }

  // check if user wants nested arrays
  $vars["rows"] = strlen($root_object) != 0 ? array(
    $root_object => $objects,
  ) : $objects;
}