You are here

public function LeafletMap::init in Leaflet 8

Same name and namespace in other branches
  1. 2.1.x modules/leaflet_views/src/Plugin/views/style/LeafletMap.php \Drupal\leaflet_views\Plugin\views\style\LeafletMap::init()
  2. 2.0.x modules/leaflet_views/src/Plugin/views/style/LeafletMap.php \Drupal\leaflet_views\Plugin\views\style\LeafletMap::init()

Overrides \Drupal\views\Plugin\views\PluginBase::init().

The style options might come externally as the style can be sourced from at least two locations. If it's not included, look on the display.

Overrides StylePluginBase::init

File

modules/leaflet_views/src/Plugin/views/style/LeafletMap.php, line 254

Class

LeafletMap
Style plugin to render a View output as a Leaflet map.

Namespace

Drupal\leaflet_views\Plugin\views\style

Code

public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
  parent::init($view, $display, $options);

  // We want to allow view editors to select which entity out of a
  // possible set they want to use to pass to the MapThemer plugin. Long term
  // it would probably be better not to pass an entity to MapThemer plugin and
  // instead pass the result row.
  if (!empty($options['entity_source']) && $options['entity_source'] != '__base_table') {
    $handler = $this->displayHandler
      ->getHandler('relationship', $options['entity_source']);
    $this->entitySource = $options['entity_source'];
    $data = Views::viewsData();
    if (($table = $data
      ->get($handler->definition['base'])) && !empty($table['table']['entity type'])) {
      try {
        $this->entityInfo = $this->entityManager
          ->getDefinition($table['table']['entity type']);
        $this->entityType = $this->entityInfo
          ->id();
      } catch (\Exception $e) {
        watchdog_exception('geofield_map', $e);
      }
    }
  }
  else {
    $this->entitySource = '__base_table';

    // For later use, set entity info related to the View's base table.
    $base_tables = array_keys($view
      ->getBaseTables());
    $base_table = reset($base_tables);
    if ($this->entityInfo = $view
      ->getBaseEntityType()) {
      $this->entityType = $this->entityInfo
        ->id();
      return;
    }

    // Eventually try to set entity type & info from base table suffix
    // (i.e. Search API views).
    if (!isset($this->entityType)) {
      $index_id = substr($base_table, 17);
      $index = Index::load($index_id);
      foreach ($index
        ->getDatasources() as $datasource) {
        if ($datasource instanceof DatasourceInterface) {
          $this->entityType = $datasource
            ->getEntityTypeId();
          try {
            $this->entityInfo = $this->entityManager
              ->getDefinition($this->entityType);
          } catch (\Exception $e) {
            watchdog_exception('leaflet', $e);
          }
        }
      }
    }
  }
}