You are here

public function GeofieldGoogleMapViewStyle::render in Geofield Map 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/style/GeofieldGoogleMapViewStyle.php \Drupal\geofield_map\Plugin\views\style\GeofieldGoogleMapViewStyle::render()

Renders the View.

Overrides StylePluginBase::render

File

src/Plugin/views/style/GeofieldGoogleMapViewStyle.php, line 348

Class

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

Namespace

Drupal\geofield_map\Plugin\views\style

Code

public function render() {
  $map_settings = $this->options;
  $element = [];

  // Performs some preprocess on the maps settings before sending to js.
  $this
    ->preProcessMapSettings($map_settings);
  $js_settings = [
    'mapid' => Html::getUniqueId("geofield_map_view_" . $this->view
      ->id() . '_' . $this->view->current_display),
    'map_settings' => $map_settings,
    'data' => [],
  ];
  $data = [];

  // Get the Geofield field.
  $geofield_name = $map_settings['data_source'];

  // If the Geofield field is null, output a warning
  // to the Geofield Map administrator.
  if (empty($geofield_name) && $this->currentUser
    ->hasPermission('configure geofield_map')) {
    $element = [
      '#markup' => '<div class="geofield-map-warning">' . $this
        ->t("The Geofield field hasn't not been correctly set for this View. <br>Add at least one Geofield to the View and set it as Data Source in the Geofield Google Map View Display Settings.") . "</div>",
      '#attached' => [
        'library' => [
          'geofield_map/geofield_map_general',
        ],
      ],
    ];
  }

  // It the Geofield field is not null, and there are results or a not null
  // empty behaviour has been set, render the results.
  if (!empty($geofield_name) && (!empty($this->view->result) || $map_settings['map_empty']['empty_behaviour'] == '1')) {
    $this
      ->renderFields($this->view->result);

    /* @var \Drupal\views\ResultRow  $result */
    foreach ($this->view->result as $id => $result) {
      $geofield_value = $this
        ->getFieldValue($id, $geofield_name);

      // In case the result is not among the raw results, get it from the
      // rendered results.
      if (empty($geofield_value)) {
        $geofield_value = $this->rendered_fields[$id][$geofield_name];
      }

      // In case the result is not null.
      if (!empty($geofield_value)) {

        // If it is a single value field, transform into an array.
        $geofield_value = is_array($geofield_value) ? $geofield_value : [
          $geofield_value,
        ];
        $description_field = isset($map_settings['map_marker_and_infowindow']['infowindow_field']) ? $map_settings['map_marker_and_infowindow']['infowindow_field'] : NULL;
        $description = [];
        $entity = $result->_entity;

        // Render the entity with the selected view mode.
        if (isset($description_field) && $description_field === '#rendered_entity' && is_object($result)) {
          $build = $this->entityManager
            ->getViewBuilder($entity
            ->getEntityTypeId())
            ->view($entity, $map_settings['view_mode'], $entity
            ->language()
            ->getId());
          $description[] = $this->renderer
            ->renderPlain($build);
        }
        elseif (isset($description_field)) {
          $description_field_name = strtolower($map_settings['map_marker_and_infowindow']['infowindow_field']);
          if (isset($entity->{$description_field_name})) {

            // Check if the entity has a $description_field_name field.
            foreach ($entity->{$description_field_name}
              ->getValue() as $value) {
              if ($map_settings['map_marker_and_infowindow']['multivalue_split'] == FALSE) {
                $description[] = $this->rendered_fields[$id][$description_field];
                break;
              }
              $description[] = isset($value['value']) ? $value['value'] : '';
            }
          }
          elseif (isset($this->rendered_fields[$id][$description_field])) {
            $description[] = $this->rendered_fields[$id][$description_field];
          }
        }

        // Add Views fields to the Json output as additional_data property.
        $view_data = [];
        foreach ($this->rendered_fields[$id] as $field_name => $rendered_field) {
          if (!empty($rendered_field) && !$this->view->field[$field_name]->options['exclude']) {

            /* @var \Drupal\Core\Render\Markup $rendered_field */
            $view_data[$field_name] = $rendered_field
              ->__toString();
          }
        }
        $data = array_merge($data, $this
          ->getGeoJsonData($geofield_value, $description, $view_data));
      }
    }
    $js_settings['data'] = [
      'type' => 'FeatureCollection',
      'features' => $data,
    ];
    $element = geofield_map_googlemap_render($js_settings);
  }
  return $element;
}