You are here

public function GeoJson::render in Views GeoJSON 8

Render the display in this style.

Overrides StylePluginBase::render

File

src/Plugin/views/style/GeoJson.php, line 373

Class

GeoJson
Style plugin to render view as GeoJSON code.

Namespace

Drupal\views_geojson\Plugin\views\style

Code

public function render() {
  $features = [
    'type' => 'FeatureCollection',
    'features' => [],
  ];

  // Get the excluded fields array, common for all rows.
  $excluded_fields = $this
    ->getExcludedFields();

  // Render each row.
  foreach ($this->view->result as $i => $row) {
    $this->view->row_index = $i;
    if ($feature = $this
      ->renderRow($row, $excluded_fields)) {
      $features['features'][] = $feature;
    }
  }
  $this->moduleHandler
    ->alter('geojson_view', $features, $this->view);
  unset($this->view->row_index);
  if (!empty($this->view->live_preview)) {

    // Pretty-print the JSON.
    $json = json_encode($features, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRETTY_PRINT);
  }
  else {

    // Render the collection to JSON using Drupal standard renderer.
    $json = Json::encode($features);
  }
  if (!empty($this->options['jsonp_prefix'])) {
    $json = $this->options['jsonp_prefix'] . "({$json})";
  }

  // Everything else returns output.
  return $json;
}