You are here

function _views_json_render_fields in Views Datasource 6

Same name and namespace in other branches
  1. 7 views_json.module \_views_json_render_fields()

Takes each field from a row object and renders the field as determined by the field's theme

Parameters

$view: View the row belongs to

$row: Row object

Return value

array Object containing all the raw and rendered fields

1 call to _views_json_render_fields()
views_plugin_style_json::render in ./views_plugin_style_json.inc
Implementation of view_style_plugin::render()

File

./views_json.module, line 110
Module definition for views_json

Code

function _views_json_render_fields($view, $row) {
  $field_ids = array_keys($view->field);
  $rendered_fields = array();
  foreach ($field_ids as $id) {
    $field = $view->field[$id];
    $field_is_multiple = FALSE;
    $field_raw = array();
    if (isset($field->options['multiple']['group']) && isset($field->field_values)) {
      $field_output = _views_json_render_multiple_field($field, $row);
      $n = 0;
      if (is_array($field_output)) {
        foreach ($field->field_values[$row->{$field->field_alias}] as $item) {
          $field_raw[++$n] = $item["value"];
        }
        $field_is_multiple = TRUE;
      }
      else {
        $field_raw = $view->field[$field->options['id']]
          ->theme($row);
      }
    }
    else {
      $field_output = $view->field[$field->options['id']]
        ->theme($row);
      $field_raw = isset($view->field[$id]->field_alias) && isset($row->{$view->field[$id]->field_alias}) ? $row->{$view->field[$id]->field_alias} : NULL;
    }
    $img_match = array();
    $src_match = array();
    if (is_array($field_output)) {
      foreach ($field_output as $i => $f) {
        if (preg_match("/<img[^>]+>/i", $f, $img_match)) {
          if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match)) {
            $field_output[$i] = $src_match[2];
          }
        }
      }
    }
    else {
      if (preg_match("/<img[^>]+>/i", $field_output, $img_match)) {
        if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match)) {
          $field_output = $src_match[2];
        }
      }
    }
    if (empty($field->options['exclude']) && !($field->options['hide_empty'] && empty($field_output))) {
      $object = new stdClass();
      $object->id = $id;

      // Respect the 'empty' value if empty and "No results text" is given.
      if (empty($field_output) && $field->options['empty']) {
        $object->content = $field->options['empty'];
      }
      else {
        $object->content = $field_output;
      }
      $object->raw = $field_raw;
      $object->class = views_css_safe($id);
      $object->label = check_plain($view->field[$id]
        ->label());
      $object->is_multiple = $field_is_multiple;
      $rendered_fields[$id] = $object;
    }
  }
  return $rendered_fields;
}