You are here

public function OpenlayersMap::render in Openlayers 8.4

Overrides \Drupal\views\Plugin\views\style\StylePluginBase\StylePluginBase::render().

Overrides StylePluginBase::render

File

src/Plugin/views/style/OpenlayersMap.php, line 113
Definition of Drupal\openlayers\Plugin\views\style\OpenlayersMap.

Class

OpenlayersMap
Style plugin to render geodata on an Openlayers map.

Namespace

Drupal\openlayers\Plugin\views\style

Code

public function render() {
  $results = [];
  $features = [];
  $this
    ->renderFields($this->view->result);
  $geofield_name = $this->options['data_source'];
  $map_id = $this->options['map'];
  $map_height = $this->options['height'] . $this->options['height_unit'];

  /* @var \Drupal\views\ResultRow $result */
  foreach ($this->view->result as $id => $view_result) {

    // Ensure $geofield_values is an array, for both single value and multi-value fields
    $geofield_values = (array) $this
      ->getFieldValue($view_result->index, $geofield_name);
    foreach ($geofield_values as $geofield_value) {
      $features[] = [
        'type' => 'wkt',
        'value' => $geofield_value,
      ];
    }
  }

  //  Now prepare to display the map using the Views result
  $map_settings = new MapSettings($map_id);
  $map = $map_settings->settings;
  $map['id'] = $map_id;
  $js_settings = [
    'map' => $map,
    'features' => $features,
  ];
  $results[] = $this->openlayersService
    ->openlayersRenderMap($js_settings['map'], $js_settings['features'], $map_height);
  return $results;
}