public function GeofieldGoogleEmbedMapFormatter::viewElements in Geofield Map 8.2
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 FormatterInterface::viewElements
File
- modules/
geofield_map_extras/ src/ Plugin/ Field/ FieldFormatter/ GeofieldGoogleEmbedMapFormatter.php, line 208
Class
- GeofieldGoogleEmbedMapFormatter
- Plugin implementation of the 'geofield_embed_google_map' formatter.
Namespace
Drupal\geofield_map_extras\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$settings = $this
->getSettings();
$bundle = $items
->getParent()
->getEntity()
->bundle();
foreach ($items as $delta => $item) {
if ($item
->isEmpty()) {
continue;
}
$value = $item
->getValue();
if ($value['geo_type'] !== 'Point') {
continue;
}
$q = urlencode($value['latlon']);
$options_string = '';
$optionals_parameters = Json::decode($settings['optionals_parameters']);
if (count($optionals_parameters)) {
foreach ($optionals_parameters as $k => $option) {
$options_string .= "&" . $k . "=" . $option;
}
}
$elements[$delta] = [
'#theme' => 'geofield_embed_google_map',
'#width' => $settings['width'],
'#height' => $settings['height'],
'#apikey' => (string) $this
->getGmapApiKey(),
'#q' => $q,
'#options_string' => $options_string,
'#title' => $this
->t('Map of @bundle', [
'@bundle' => $bundle,
]),
];
}
return $elements;
}