public function LatLonFormatter::viewElements in Geofield 8
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides GeofieldDefaultFormatter::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ LatLonFormatter.php, line 84
Class
- LatLonFormatter
- Plugin implementation of the 'geofield_latlon' formatter.
Namespace
Drupal\geofield\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$output = [
'#markup' => '',
];
$geom = $this->geoPhpWrapper
->load($item->value);
if ($geom) {
// If the geometry is not a point, get the centroid.
if ($geom
->getGeomType() != 'Point') {
$geom = $geom
->centroid();
}
/* @var \Point $geom */
if ($this
->getOutputFormat() == 'decimal') {
$output = [
'#theme' => 'geofield_latlon',
'#lat' => $geom
->y(),
'#lon' => $geom
->x(),
];
}
elseif ($this
->getOutputFormat() == 'wkt') {
$output = [
'#markup' => "POINT ({$geom->x()} {$geom->y()})",
];
}
else {
$components = $this
->getDmsComponents($geom);
$output = [
'#theme' => 'geofield_dms',
'#components' => $components,
];
}
}
$elements[$delta] = $output;
}
return $elements;
}