You are here

function _views_geojson_render_fields in Views GeoJSON 6

Same name and namespace in other branches
  1. 7 views_geojson.module \_views_geojson_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_geojson_render_fields()
views_plugin_style_geojson::render in views/views_plugin_style_geojson.inc
Implementation of view_style_plugin::render()

File

./views_geojson.module, line 110
Provide a GeoJSON Views feed style.

Code

function _views_geojson_render_fields($view, $row, $index) {
  $excluded_fields = array();
  $feature = array(
    'type' => 'Feature',
  );
  $data_source = $view->style_plugin->options['data_source'];
  $field_ids = array_keys($view->field);

  // Pre-render fields to handle those rewritten with tokens
  foreach ($view->field as $field_idx => $field) {
    $field
      ->advanced_render($row);
  }
  switch ($data_source['value']) {
    case 'latlon':
      $options = array(
        'latitude',
        'longitude',
      );
      $latitude = NULL;
      $longitude = NULL;
      foreach ($view->field as $field_idx => $field) {
        foreach ($options as $option) {
          if ($data_source[$option] == $field_idx) {
            ${$option} = $field
              ->advanced_render($row);
            $excluded_fields[] = $field_idx;
          }
        }
      }
      if (!empty($latitude) && !empty($longitude)) {
        $feature['geometry'] = array(
          'type' => 'Point',
          'coordinates' => array(
            floatval($longitude),
            floatval($latitude),
          ),
        );
      }
      break;
    case 'geofield':
      foreach ($view->field as $field_idx => $field) {
        if ($data_source['geofield'] == $field_idx) {
          $geofield = $view->style_plugin
            ->get_field_value($view->row_index, $field_idx);
          $geofield = $geofield[0]['wkt'];
          $view->row_index = $index;
          $excluded_fields[] = $field_idx;
        }
      }
      if (!empty($geofield)) {
        geophp_load();
        $json = geoPHP::load($geofield, 'wkt');
        if (is_object($json)) {
          $feature['geometry'] = json_decode($json
            ->out('json'));
        }
      }
      break;
    case 'wkt':
      foreach ($view->field as $field_idx => $field) {
        if ($data_source['wkt'] == $field_idx) {
          $wkt = $field
            ->advanced_render($row);
          $excluded_fields[] = $field_idx;
        }
      }
      if (!empty($wkt)) {
        geophp_load();
        $json = geoPHP::load($wkt, 'wkt');
        if (is_object($json)) {
          $feature['geometry'] = json_decode($json
            ->out('json'));
        }
      }
      break;
  }

  // Only add features with geometry data
  if (empty($feature['geometry'])) {
    return;
  }

  // Add the name and description attributes
  // as chosen through interface
  if ($data_source['name_field']) {
    foreach ($view->field as $field_idx => $field) {
      if ($data_source['name_field'] == $field_idx) {
        $name_field = $field
          ->advanced_render($row);
        $excluded_fields[] = $field_idx;
      }
    }
    $feature['properties']['name'] = $name_field;
  }
  else {
    $feature['properties']['name'] = '';
  }
  if ($data_source['description_field']) {
    foreach ($view->field as $field_idx => $field) {
      if ($data_source['description_field'] == $field_idx) {
        $description_field = $field
          ->advanced_render($row);
        $excluded_fields[] = $field_idx;
      }
      if ($data_source['description_field'] === '#node') {
        $view_mode = $data_source['view_mode'] ? $data_source['view_mode'] : 'full';
        $description_field = drupal_render(node_view(node_load($row->nid), $view_mode));
      }
    }
    $feature['properties']['description'] = $description_field;
  }
  else {
    $feature['properties']['description'] = '';
  }

  // Fill in attributes that are not:
  // - Coordinate fields
  // - Name/description (already processed)
  // - Views "excluded" fields
  foreach ($field_ids as $id) {
    $field = $view->field[$id];
    if (!in_array($id, $excluded_fields, TRUE) && !$field->options['exclude']) {
      $value_rendered = $field
        ->advanced_render($row);
      $feature['properties'][$id] = is_numeric($value_rendered) ? floatval($value_rendered) : $value_rendered;
    }
  }
  return $feature;
}