You are here

function leaflet_views_plugin_style::render in Leaflet 7

Renders view.

Overrides views_plugin_style::render

File

leaflet_views/leaflet_views_plugin_style.inc, line 281
Extension of the Views Plugin Style for Leaflet Map Adapted from the GeoField Map views module and the OpenLayers Views module.

Class

leaflet_views_plugin_style
@file Extension of the Views Plugin Style for Leaflet Map Adapted from the GeoField Map views module and the OpenLayers Views module.

Code

function render() {
  if (!empty($this->view->live_preview)) {
    return t('No preview available.');
  }
  $data = array();
  $map = leaflet_map_get_info($this->options['map']);

  // Is there a geofield selected?
  if ($this->options['data_source']) {
    $name_field = empty($this->options['name_field']) ? NULL : $this->options['name_field'];
    $this
      ->render_fields($this->view->result);
    foreach ($this->view->result as $id => $result) {
      $geofield = $this
        ->get_field_value($id, $this->options['data_source']);
      if (!empty($geofield)) {
        $entity = FALSE;

        // Render the entity with the selected view mode:
        if (is_object($result)) {
          if (!empty($result->{$this->entity_info['entity keys']['id']})) {
            $entity_id = $result->{$this->entity_info['entity keys']['id']};
          }
          elseif (isset($result->entity)) {
            $entity_id = $result->entity;
          }
          elseif (isset($result->_field_data)) {
            $tmp = $result->_field_data;
            $tmp = array_pop($tmp);
            $entity_id = $tmp['entity']->{$this->entity_info['entity keys']['id']};
          }
          $entity = entity_load_single($this->entity_type, $entity_id);
          if ($this->options['description_field'] === '#rendered_entity') {
            $build = entity_view($this->entity_type, array(
              $entity,
            ), $this->options['view_mode']);
            $description = drupal_render($build);
          }
          else {
            $description = '';
            if ($name_field) {
              $description = $this->rendered_fields[$id][$name_field];
            }
            if ($this->options['description_field']) {
              $description .= $this->rendered_fields[$id][$this->options['description_field']];
            }
          }
        }
        $points = leaflet_process_geofield($geofield);

        // Attach pop-ups if we have rendered into $description:
        if (isset($description)) {
          foreach ($points as &$point) {
            $point['popup'] = $description;
          }
        }

        // Attach also titles & entities, they might be used later on.
        if ($name_field) {
          foreach ($points as &$point) {
            if (isset($this->rendered_fields[$id][$name_field])) {
              $point['label'] = htmlspecialchars_decode(strip_tags($this->rendered_fields[$id][$name_field]));
            }
            if ($entity !== FALSE) {
              $point['entity'] = $entity;
            }
          }
        }
        if ($this->options['icon']['iconType'] == 'html') {
          foreach ($points as &$point) {
            $target_field = $this->options['icon']['html'];
            $point['rendered_html'] = isset($this->rendered_fields[$id][$target_field]) ? $this->rendered_fields[$id][$target_field] : '';
          }
        }

        // Let modules modify the points data.
        drupal_alter('leaflet_views_alter_points_data', $result, $points);

        // Merge these points into the $data array for map rendering:
        $data = array_merge($data, $points);
      }
    }
    $entity_type = isset($this->entity_type) ? $this->entity_type : '';
    leaflet_apply_map_settings($map, $data, $this->options, $entity_type);
    if (empty($data) && !empty($this->options['hide_empty'])) {
      return;
    }
    return leaflet_build_map($map, $data, $this->options['height'] . 'px');
  }
  return;
}