/** * @file * Default template for the Views GeoJSON style plugin using the simple format * * Variables: * - $view: The View object * - $features: Features data to be serialized as GeoJSON * - $options: Array of options for this style * * @ingroup views_templates */ $jsonp_prefix = $options['jsonp_prefix']; $features_collection = array('type' => 'FeatureCollection', 'features' => $features); if ($view->override_path) { // We're inside a live preview where the GeoJSON is pretty-printed. $json = _views_geojson_encode_formatted($features_collection); if ($jsonp_prefix) { $json = "{$jsonp_prefix}({$json})"; } print "<code>{$json}</code>"; } else { $json = drupal_json_encode($features_collection); if ($jsonp_prefix) { $json = "{$jsonp_prefix}({$json})"; } if ($options['using_views_api_mode']) { // We're in Views API mode. print $json; } else { // We want to send the JSON as a server response so switch the content // type and stop further processing of the page.
/** * {@inheritdoc} */ public function render() { $features = array('type' => 'FeatureCollection', 'features' => array()); // Render each row. foreach ($this->view->result as $i => $row) { $this->view->row_index = $i; if ($feature = _views_geojson_render_fields($this->view, $row, $i)) { $features['features'][] = $feature; } } unset($this->view->row_index); // Render the collection to JSON. $json = \Drupal\Component\Serialization\Json::encode($features); if (!empty($this->options['jsonp_prefix'])) { $json = $this->options['jsonp_prefix'] . "({$json})"; } if (!empty($this->view->live_preview)) { // Pretty-print the JSON. $json = _views_geojson_encode_formatted($features); if (!empty($this->options['jsonp_prefix'])) { $json = $this->options['jsonp_prefix'] . "({$json})"; } } // Everything else returns output. return $json; }