You are here

function _views_json_render_fields in Views Datasource 7

Same name and namespace in other branches
  1. 6 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/plugins/views_plugin_style_json.inc
Implementation of view_style_plugin::render()

File

./views_json.module, line 154

Code

function _views_json_render_fields($view, $row) {
  $field_ids = array_keys($view->field);
  $fields_info = $view->display_handler
    ->get_option('fields');
  $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']]
          ->advanced_render($row);
      }
    }
    else {
      $field_output = $view->field[$field->options['id']]
        ->advanced_render($row);
      $handler = $view->field[$id]->handler_type;
      $fieldname = $handler . '_' . $id;

      // For fields with result content, we need to use field-specific logic.
      if ($handler == 'field' && isset($row->{$fieldname})) {
        $field_id = "field_{$id}";
        switch ($field->field_info['type']) {
          case 'taxonomy_term_reference':
            $field_raw = $row->{$field_id}[0]['raw']['taxonomy_term']->name;
            break;
          case 'link_field':
            $field_raw = $row->{$field_id}[0]['raw']['url'];
            break;
          case 'email':
            $field_raw = $row->{$field_id}[0]['raw']['email'];
            break;
          case 'video':
          case 'file':
          case 'image':
            $field_raw = file_create_url($row->{$field_id}[0]['raw']['uri']);
            break;
          default:
            $field_raw = $row->{$field_id}[0]['raw']['value'];
            break;
        }
      }
      else {

        // Otherwise it's an internal value.
        $field_raw = $row->{$view->field[$id]->field_alias};
      }

      // Check if this is field is the entity's label.
      if (empty($field->base_table)) {
        $entity_info = entity_get_info($field->table);
        if (isset($entity_info['entity keys']['label'])) {
          $label_field = $entity_info['entity keys']['label'];
          if ($field->real_field == $label_field) {

            // Decode html entities in label field as views does this by default for those fields.
            $field_output = html_entity_decode($field_output, ENT_QUOTES);
            $field_output = mb_check_encoding($field_output, 'UTF-8') ? $field_output : utf8_encode($field_output);
          }
        }
      }
    }
    $img_match = array();
    $src_match = array();
    if (isset($field->options['type'])) {
      if (is_array($field_output) && $field->options['type'] == 'image') {
        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'] = $src_match[2];
              $field_output[$i]['alt'] = '';
              $field_output[$i]['title'] = '';
              if (preg_match('/(alt)="([^"]*)"/i', $img_match[0], $alt_match)) {
                $field_output[$i]['alt'] = $alt_match[2];
              }
              if (preg_match('/(title)="([^"]*)"/i', $img_match[0], $alt_match)) {
                $field_output[$i]['title'] = $alt_match[2];
              }
            }
          }
        }
      }
      else {
        if ($field->options['type'] == 'image') {

          // if field's type was an image. we just get the path.
          if (preg_match_all("/<img[^>]+>/i", $field_output, $img_matches)) {
            $field_output = array();
            if (sizeof($img_matches[0]) > 1) {
              $field_is_multiple = TRUE;
              foreach ($img_matches[0] as $i => $img_match) {
                if (preg_match('/(src)="([^"]*)"/i', $img_match, $src_match)) {
                  $field_output[$i]['src'] = $src_match[2];
                  $field_output[$i]['alt'] = '';
                  $field_output[$i]['title'] = '';
                  if (preg_match('/(alt)="([^"]*)"/i', $img_match, $alt_match)) {
                    $field_output[$i]['alt'] = $alt_match[2];
                  }
                  if (preg_match('/(title)="([^"]*)"/i', $img_match, $alt_match)) {
                    $field_output[$i]['title'] = $alt_match[2];
                  }
                }
              }
            }
            else {
              if (preg_match('/(src)="([^"]*)"/i', $img_matches[0][0], $src_match)) {
                $field_output['src'] = $src_match[2];
                $field_output['alt'] = '';
                $field_output['title'] = '';
                if (preg_match('/(alt)="([^"]*)"/i', $img_matches[0][0], $alt_match)) {
                  $field_output['alt'] = $alt_match[2];
                }
                if (preg_match('/(title)="([^"]*)"/i', $img_matches[0][0], $alt_match)) {
                  $field_output['title'] = $alt_match[2];
                }
              }
            }
          }
        }
      }
    }
    if (empty($field->options['exclude'])) {
      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 = drupal_clean_css_identifier(strtolower($id));

        //views_css_safe($id);
        if (isset($view->style_options['translate_labels']) && $view->style_options['translate_labels']) {
          $label = $view->field[$id]
            ->label();
        }
        elseif (isset($fields_info) && isset($fields_info[$id]['label'])) {
          $label = $fields_info[$id]['label'];
        }
        else {
          $label = $field
            ->label();
        }
        $object->label = check_plain($label);
        $object->is_multiple = $field_is_multiple;
        $rendered_fields[$id] = $object;
      }
    }
  }
  return $rendered_fields;
}