You are here

public function CommonMap::render in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/views/style/CommonMap.php \Drupal\geolocation\Plugin\views\style\CommonMap::render()

Render the display in this style.

Overrides GeolocationStyleBase::render

File

src/Plugin/views/style/CommonMap.php, line 109

Class

CommonMap
Allow to display several field items on a common map.

Namespace

Drupal\geolocation\Plugin\views\style

Code

public function render() {
  $render = parent::render();
  if ($render === FALSE) {
    return [];
  }
  if (!empty($this->options['dynamic_map']['enabled'])) {

    // @todo Not unique enough, but uniqueid() changes on every AJAX request.
    // For the geolocationCommonMapBehavior to work, this has to stay
    // identical.
    $this->mapId = $this->view
      ->id() . '-' . $this->view->current_display;
    $this->mapId = str_replace('_', '-', $this->mapId);
  }
  else {
    $this->mapId = $this->view->dom_id;
  }
  $map_settings = [];
  if (!empty($this->options['map_provider_settings'])) {
    $map_settings = $this->options['map_provider_settings'];
  }
  $build = [
    '#type' => 'geolocation_map',
    '#maptype' => $this->options['map_provider_id'],
    '#id' => $this->mapId,
    '#settings' => $map_settings,
    '#layers' => [],
    '#attached' => [
      'library' => [
        'geolocation/geolocation.commonmap',
      ],
    ],
    '#context' => [
      'view' => $this->view,
    ],
  ];

  /*
   * Dynamic map handling.
   */
  if (!empty($this->options['dynamic_map']['enabled'])) {
    if (!empty($this->options['dynamic_map']['update_target']) && $this->view->displayHandlers
      ->has($this->options['dynamic_map']['update_target'])) {
      $update_view_display_id = $this->options['dynamic_map']['update_target'];
    }
    else {
      $update_view_display_id = $this->view->current_display;
    }
    $build['#attached']['drupalSettings']['geolocation']['commonMap'][$this->mapId]['dynamic_map'] = [
      'enable' => TRUE,
      'hide_form' => $this->options['dynamic_map']['hide_form'],
      'views_refresh_delay' => $this->options['dynamic_map']['views_refresh_delay'],
      'update_view_id' => $this->view
        ->id(),
      'update_view_display_id' => $update_view_display_id,
    ];
    if (substr($this->options['dynamic_map']['update_handler'], 0, strlen('boundary_filter_')) === 'boundary_filter_') {
      $filter_id = substr($this->options['dynamic_map']['update_handler'], strlen('boundary_filter_'));
      $filters = $this->displayHandler
        ->getOption('filters');
      $filter_options = $filters[$filter_id];
      $build['#attached']['drupalSettings']['geolocation']['commonMap'][$this->mapId]['dynamic_map'] += [
        'boundary_filter' => TRUE,
        'parameter_identifier' => $filter_options['expose']['identifier'],
      ];
    }
  }
  $this
    ->renderFields($this->view->result);

  /*
   * Add locations to output.
   */
  foreach ($this->view->result as $row) {
    foreach ($this
      ->getLocationsFromRow($row) as $location) {
      $build['locations'][] = $location;
    }
  }
  $build = $this->mapCenterManager
    ->alterMap($build, $this->options['centre'], $this);
  if ($this->view
    ->getRequest()
    ->get('geolocation_common_map_dynamic_view')) {
    if (empty($build['#attributes'])) {
      $build['#attributes'] = [];
    }
    $build['#attributes'] = array_replace_recursive($build['#attributes'], [
      'data-preserve-map-center' => TRUE,
    ]);
  }
  if ($this->mapProviderManager
    ->hasDefinition($this->options['map_provider_id'])) {
    $build = $this->mapProviderManager
      ->createInstance($this->options['map_provider_id'], $this->options['map_provider_settings'])
      ->alterCommonMap($build, $this->options['map_provider_settings'], [
      'view' => $this,
    ]);
  }
  if (!empty($this->view->geolocationLayers) && !empty($this->view->geolocationLayers[$this->view->current_display])) {
    if (empty($build['#layers'])) {
      $build['#layers'] = [];
    }
    $build['#layers'][] = $this->view->geolocationLayers[$this->view->current_display];
  }
  return $build;
}