You are here

public function YaMaps::render in Yandex.Maps 8

Render the display in this style.

Overrides StylePluginBase::render

File

src/Plugin/views/style/YaMaps.php, line 82

Class

YaMaps
Allow to display several field items on a yandex map.

Namespace

Drupal\yamaps\Plugin\views\style

Code

public function render() {
  $placemarks = [];
  $prepared_lines = [];
  $prepared_polygons = [];
  $prepared_route = [];
  $this->options['coords'] = $this
    ->getCoordinates();
  $prepared_params = $this->geocoding
    ->decodeParams($this->options);
  if (empty($this->options['yandex_map_field'])) {
    return $this
      ->t('Please add "Yandex Maps" field and chose it in views settings form.');
  }
  $yandexmap_field_type = $this->view->field[$this->options['yandex_map_field']]->options['type'];
  if ($yandexmap_field_type === static::YAMAPS_DEFAULT_FORMATTER && (!isset($prepared_params['coords']['center']) || isset($prepared_params['coords']['center']) && !is_array($prepared_params['coords']['center']))) {
    return $this
      ->t('The Static Yandex Maps style cannot be used without coordinates of center.');
  }
  $yandexmap_field_name = $this->options['yandex_map_field'];
  $yandexmap_field_settings = $this->view->field[$this->options['yandex_map_field']]->options['settings'];
  foreach ($this->view->result as $row_index => $row) {

    // Fix yandex cart.
    $yandexmap_field = $this->view->field[$yandexmap_field_name];
    $yandexmap_field_entity = $yandexmap_field
      ->getEntity($row);
    if (isset($yandexmap_field_entity->{$yandexmap_field->definition['field_name']})) {
      $yandexmap_field_value = $yandexmap_field_entity->{$yandexmap_field->definition['field_name']}
        ->getValue();
      foreach ($yandexmap_field_value as $yandexmap_field_coords) {
        if (isset($yandexmap_field_coords['placemarks'])) {

          // Preparing placemarks.
          $decoded_placemarks = Json::decode($yandexmap_field_coords['placemarks']);
          if (is_array($decoded_placemarks)) {
            foreach ($decoded_placemarks as $placemark) {

              // Override placemark title.
              $this
                ->overridePlacemarkTitle($placemark, $row);

              // Prepare Balloon title.
              if ($this->options['balloon_title'] && $this->options['balloon_title'] !== static::PLACEMARK_DEFAULT_FIELD) {
                $balloon_title = '';
                $balloon_title_field = $this->view->field[$this->options['balloon_title']];
                if ($balloon_title_field != NULL) {
                  $balloon_title_field_entity = $balloon_title_field
                    ->getEntity($row);
                  $balloon_title_field_values = $balloon_title_field_entity->{$balloon_title_field->definition['field_name']}
                    ->getValue();
                  $balloon_title = !empty($balloon_title_field_values[0]['value']) ? $balloon_title_field_values[0]['value'] : '';
                }
                $placemark['params'][static::PLACEMARK_BALLON_HEADER] = $balloon_title;
              }

              // Prepare Balloon body.
              if (isset($this->options['balloon_body']) && is_array($this->options['balloon_body'])) {
                $balloon_body = [];
                foreach ($this->options['balloon_body'] as $bval) {
                  if (!empty($this->view->field[$bval])) {
                    $balloon_body_field = $this->view->field[$bval];
                    $balloon_body_field_entity = $balloon_body_field
                      ->getEntity($row);
                    $balloon_body_field_values = $balloon_body_field_entity->{$balloon_body_field->definition['field_name']}
                      ->getValue();
                    $balloon_body[] = !empty($balloon_body_field_values[0]['value']) ? $balloon_body_field_values[0]['value'] : '';
                  }
                }
                $placemark['params'][static::PLACEMARK_BALLON_BODY] = $this
                  ->prepareBody($balloon_body);
              }
              $this->view->row_index = $row_index;
              unset($balloon_body);
              $placemarks[] = $placemark;
            }
          }
        }

        // Preparing lines.
        if (isset($yandexmap_field_coords['lines'])) {
          $decoded_lines = Json::decode($yandexmap_field_coords['lines']);
          if (is_array($decoded_lines)) {
            foreach ($decoded_lines as $lines) {
              $prepared_lines[] = $lines;
            }
          }
        }

        // Preparing polygons.
        if (isset($yandexmap_field_coords['polygons'])) {
          $decoded_polygons = Json::decode($yandexmap_field_coords['polygons']);
          if (is_array($decoded_polygons)) {
            foreach ($decoded_polygons as $polygons) {
              $prepared_polygons[] = $polygons;
            }
          }
        }

        // Preparing routes.
        if (isset($yandexmap_field_coords['routes'])) {
          $decoded_routes = Json::decode($yandexmap_field_coords['routes']);
          if (is_array($decoded_routes)) {
            foreach ($decoded_routes as $route) {
              $prepared_route[] = $route;
            }
          }
        }
      }
    }
  }
  unset($this->view->row_index);
  $array_of_unique_params = [
    $this
      ->getPluginId(),
    $this->view
      ->getDisplay()
      ->getType(),
    $this->view->current_display,
  ];
  if (isset($this->view->dom_id)) {
    $array_of_unique_params[] = $this->view->dom_id;
  }

  // Unique map id.
  $id = Html::getUniqueId(implode('-', $array_of_unique_params));
  switch ($this->options['yamaps_center_options']['map_center_type']) {
    case 'geolocation':
      $prepared_params['coords']['center'] = NULL;
      $parameters = $this->geocoding
        ->geocode($this->options['yamaps_center_options']['map_center_geolocation']);
      if (isset($parameters) && $parameters !== FALSE) {
        $prepared_params['coords']['center'] = $parameters['map_center'];
      }
      $prepared_params['coords']['zoom'] = ++$this->options['yamaps_center_options']['zoom'];
      $prepared_params['type'] = 'yandex#map';
      break;
    case 'mini_map':

      // Merging placemark.
      if (is_array($prepared_params['placemarks'])) {
        foreach ($prepared_params['placemarks'] as $p) {
          $placemarks[] = $p;
        }
      }

      // Merging lines.
      if (is_array($prepared_params['lines'])) {
        foreach ($prepared_params['lines'] as $lines) {
          $prepared_lines[] = $lines;
        }
      }

      // Merging polygons.
      if (is_array($prepared_params['polygons'])) {
        foreach ($prepared_params['polygons'] as $polygon) {
          $prepared_polygons[] = $polygon;
        }
      }

      // Merging routes.
      if (is_array($prepared_params['routes'])) {
        foreach ($prepared_params['routes'] as $route) {
          $prepared_route[] = $route;
        }
      }
      break;
      $map_behaviors = [];
      if ($yandexmap_field_settings['enable_zoom']) {
        $map_behaviors[] = 'scrollZoom';
        $map_behaviors[] = 'dblClickZoom';
      }
      if ($yandexmap_field_settings['enable_drag']) {
        $map_behaviors[] = 'drag';
      }
  }
  $build = [];

  // Map initialization parameters.
  $map = [
    'id' => $id,
    'init' => [
      'center' => $prepared_params['coords']['center'] ?? NULL,
      'zoom' => $prepared_params['coords']['zoom'] ?? NULL,
      'type' => $prepared_params['type'],
      'behaviors' => $map_behaviors,
    ],
    'display_options' => [
      'display_type' => 'map',
      'width' => isset($yandexmap_field_settings['width']) ? $yandexmap_field_settings['width'] : self::YAMAPS_DEFAULT_ADMIN_UI_MAP_WIDTH,
      'height' => isset($yandexmap_field_settings['height']) ? $yandexmap_field_settings['height'] : self::YAMAPS_DEFAULT_ADMIN_UI_MAP_HEIGHT,
    ],
    'edit' => FALSE,
    'controls' => 1,
    'placemarks' => empty($placemarks) ? NULL : $placemarks,
    'lines' => empty($prepared_lines) ? NULL : $prepared_lines,
    'polygons' => empty($prepared_polygons) ? NULL : $prepared_polygons,
    'routes' => empty($prepared_route) ? NULL : $prepared_route,
  ];
  $map_class = [
    'yamaps-map-container',
  ];
  $build[] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#attributes' => [
      'style' => ' width: ' . $yandexmap_field_settings['width'] . '; height:' . $yandexmap_field_settings['height'] . ';',
      'id' => $id,
      'class' => $map_class,
    ],
    '#value' => '',
  ];
  $build['#attached']['library'][] = 'yamaps/yandex-map-api';
  $build['#attached']['library'][] = 'yamaps/yamaps-placemark';
  $build['#attached']['library'][] = 'yamaps/yamaps-line';
  $build['#attached']['library'][] = 'yamaps/yamaps-polygon';
  $build['#attached']['library'][] = 'yamaps/yamaps-map';
  $build['#attached']['drupalSettings']['yamaps'] = [
    $id => $map,
  ];
  return $build;
}