protected function GeoJson::renderRow in Views GeoJSON 8
Render views fields to GeoJSON.
Takes each field from a row object and renders the field as determined by the field's theme.
Parameters
\Drupal\views\ResultRow $row: Row object.
array $excluded_fields: Array containing field keys to be excluded.
Return value
array Array containing all the raw and rendered fields
Throws
Exception
1 call to GeoJson::renderRow()
- GeoJson::render in src/
Plugin/ views/ style/ GeoJson.php - Render the display in this style.
File
- src/
Plugin/ views/ style/ GeoJson.php, line 541
Class
- GeoJson
- Style plugin to render view as GeoJSON code.
Namespace
Drupal\views_geojson\Plugin\views\styleCode
protected function renderRow(ResultRow $row, array $excluded_fields) {
$feature = [
'type' => 'Feature',
];
$data_source = $this->options['data_source'];
// Pre-render fields to handle those rewritten with tokens.
foreach ($this->view->field as $field_idx => $field) {
$field
->advancedRender($row);
}
switch ($data_source['value']) {
case 'latlon':
$latitude = (string) $this->view->field[$data_source['latitude']]
->advancedRender($row);
$longitude = (string) $this->view->field[$data_source['longitude']]
->advancedRender($row);
if (!empty($latitude) && !empty($longitude)) {
$feature['geometry'] = [
'type' => 'Point',
'coordinates' => [
(double) $longitude,
(double) $latitude,
],
];
}
break;
case 'geofield':
$geofield = $this->view->style_plugin
->getFieldValue($row->index, $data_source['geofield']);
if (!empty($geofield)) {
$geometry = Drupal::getContainer()
->get('geofield.geophp')
->load($geofield);
if (is_object($geometry)) {
$feature['geometry'] = Json::decode($geometry
->out('json'));
}
}
break;
case 'geolocation':
$geo_items = $this->view->field[$data_source['geolocation']]
->getItems($row);
if (!empty($geo_items[0]['raw'])) {
$raw_geo_item = $geo_items[0]['raw'];
$wkt = "POINT({$raw_geo_item->lng} {$raw_geo_item->lat})";
$geometry = geoPHP::load($wkt, 'wkt');
if (is_object($geometry)) {
$feature['geometry'] = Json::decode($geometry
->out('json'));
}
}
break;
case 'wkt':
$wkt = (string) $this->view->field[$data_source['wkt']]
->advancedRender($row);
if (!empty($wkt)) {
$wkt = explode(',', $wkt);
$geometry = geoPHP::load($wkt, 'wkt');
if (is_object($geometry)) {
$feature['geometry'] = Json::decode($geometry
->out('json'));
}
}
break;
}
// Only add features with geometry data.
if (empty($feature['geometry'])) {
return;
}
// Add the name and description attributes
// as chosen through interface.
$feature['properties']['name'] = $this
->renderNameField($row);
$feature['properties']['description'] = $this
->renderDescriptionField($row);
// Fill in attributes that are not:
// - Coordinate fields,
// - Name/description (already processed),
// - Views "excluded" fields.
foreach (array_keys($this->view->field) as $id) {
$field = $this->view->field[$id];
if (!isset($excluded_fields[$id]) && !$field->options['exclude']) {
// Allows you to customize the name of the property by setting a label
// to the field.
$key = empty($field->options['label']) ? $id : $field->options['label'];
$value_rendered = $field
->advancedRender($row);
$feature['properties'][$key] = is_numeric($value_rendered) ? (double) $value_rendered : $value_rendered;
}
}
return $feature;
}