You are here

public function CommonMapBase::render in Geolocation Field 8.2

Render the display in this style.

Overrides StylePluginBase::render

File

src/Plugin/views/style/CommonMapBase.php, line 123

Class

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

Namespace

Drupal\geolocation\Plugin\views\style

Code

public function render() {
  if (empty($this->options['geolocation_field'])) {
    \Drupal::messenger()
      ->addMessage('The geolocation common map ' . $this->view
      ->id() . ' views style was called without a geolocation field defined in the views style settings.', 'error');
    return [];
  }
  if (!empty($this->options['title_field']) && $this->options['title_field'] != 'none') {
    $this->titleField = $this->options['title_field'];
  }
  if (!empty($this->options['icon_field']) && $this->options['icon_field'] != 'none') {
    $this->iconField = $this->options['icon_field'];
  }

  // 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);
  $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,
    '#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_number => $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,
    ]);
  }
  return $build;
}