You are here

function openlayers_views_style_map::render in Openlayers 6

Same name and namespace in other branches
  1. 6.2 modules/openlayers_views/views/openlayers_views_style_map.inc \openlayers_views_style_map::render()
  2. 7.2 modules/openlayers_views/views/openlayers_views_style_map.inc \openlayers_views_style_map::render()

Renders views (map)

File

modules/openlayers_views/views/openlayers_views_style_map.inc, line 490
This file holds style plugin for OpenLayers Views

Class

openlayers_views_style_map
@class Extension of the Views Plugin Syle for OpenLayers

Code

function render() {
  $output = '';

  // Check for live preview.
  if (!empty($this->view->live_preview)) {
    return t('OpenLayers views are not compatible with live preview.');
  }

  // Check row plugin if using it.
  if (empty($this->row_plugin)) {
    drupal_set_message(t('Missing Row Plug-in'), 'error');
    vpr('views_plugin_style_default: Missing row plugin');
    return;
  }

  // Get selected preset
  $preset_name = $this->options['presets']['map_preset'];
  $preset = openlayers_get_preset($preset_name);
  $map = $preset['preset_data'];

  // Define ID
  $map['id'] = OPENLAYERS_VIEWS_MAP_ID_PREFIX . '-' . $this->view->name;

  // Get the name of the JS callback that generates style contexts
  if ($this->options['style']['style_context_callback']) {
    $map['styleContextCallback'] = $this->options['style']['style_context_callback'];
  }

  // Group the rows according to the grouping field, if specified.
  $sets = $this
    ->render_grouping($this->view->result, $this->options['grouping_set']['grouping']);
  $grouped = !empty($this->options['grouping_set']['grouping']) ? TRUE : FALSE;

  // Render each group separately and concatenate.  Plugins may override this
  // method if they wish some other way of handling grouping.
  foreach ($sets as $group_title => $records) {

    // Make 'machine readable' format
    $group_name = _openlayers_views_clean($group_title);

    // @@TODO: Reduce and sort duplicates
    // Style the features based on group (can override this with the theme hook)
    $features = $this
      ->map_features($records, $group_name);

    // Check if plugin wants to display empty map and if features
    if ($this->options['presets']['not_display_empty_map'] && count($features) == 0) {

      // Ensure regular views ops
      $output .= theme($this
        ->theme_functions(), $this->view, $this->options, '', $group_name);
      return $output;
    }

    // Create name of layer and id
    $layer_name = $grouped ? $group_title : $this->display->display_title;
    $layer_id = 'openlayers_views_layer_' . $this->view->name;
    $layer_id .= $grouped ? '_' . $group_name : '';

    // Define a layer for the features
    $map['layers'] = is_array($map['layers']) ? $map['layers'] : array();
    $map['layers'][$layer_id] = array(
      'id' => $layer_id,
      'type' => 'Vector',
      'name' => $layer_name,
      'options' => array(),
      'events' => array(),
      'features' => $features,
    );

    // Set up per-layer behaviors
    if (module_exists('openlayers_behaviors')) {

      // Set up tooltip behavior
      if (!empty($this->options['behaviors']['tooltip'])) {
        $map['behaviors'] = is_array($map['behaviors']) ? $map['behaviors'] : array();
        $field_id = $this->view->display_handler
          ->get_handler('field', $this->options['behaviors']['tooltip'])->field_alias;
        $map['behaviors']['openlayers_views_tooltip_' . $layer_id] = array(
          'id' => 'openlayers_views_tooltip_' . $layer_id,
          'type' => 'openlayers_behaviors_tooltip',
          'layer' => $layer_id,
          'attribute' => 'openlayers_tooltip',
        );
      }

      // Set up declutter behaviors
      if ($this->options['behaviors']['declutter']) {
        $map['behaviors'] = is_array($map['behaviors']) ? $map['behaviors'] : array();
        $map['behaviors']['openlayers_views_declutter_' . $layer_id] = array(
          'id' => 'openlayers_views_declutter_' . $layer_id,
          'type' => 'openlayers_behaviors_declutter',
          'layer' => $layer_id,
        );
        if ($this->options['behaviors']['declutter_adjustment']) {
          $map['behaviors']['openlayers_views_declutter_' . $layer_id]['adjustment'] = intval($this->options['behaviors']['declutter_adjustment']);
        }
      }

      // Set up clustering behaviors
      if ($this->options['behaviors']['cluster']) {
        $map['behaviors'] = is_array($map['behaviors']) ? $map['behaviors'] : array();
        $map['behaviors']['openlayers_views_cluster_' . $layer_id] = array(
          'id' => 'openlayers_views_cluster_' . $layer_id,
          'type' => 'openlayers_behaviors_cluster',
          'layer' => $layer_id,
        );

        // Add cluster values
        if ($this->options['behaviors']['cluster_distance']) {
          $map['behaviors']['openlayers_views_cluster_' . $layer_id]['cluster_distance'] = $this->options['behaviors']['cluster_distance'];
        }
        if ($this->options['behaviors']['cluster_threshold']) {
          $map['behaviors']['openlayers_views_cluster_' . $layer_id]['cluster_threshold'] = $this->options['behaviors']['cluster_threshold'];
        }
        if ($this->options['behaviors']['cluster_popup']) {
          $map['behaviors']['openlayers_views_cluster_' . $layer_id]['cluster_popup'] = $this->options['behaviors']['cluster_popup'];
        }
        if ($this->options['behaviors']['cluster_popup_callback']) {
          $map['behaviors']['openlayers_views_cluster_' . $layer_id]['cluster_popup_callback'] = $this->options['behaviors']['cluster_popup_callback'];
        }
        if ($this->options['behaviors']['cluster_tooltip']) {
          $map['behaviors']['openlayers_views_cluster_' . $layer_id]['cluster_tooltip'] = $this->options['behaviors']['cluster_tooltip'];
        }
        if ($this->options['behaviors']['cluster_tooltip_callback']) {
          $map['behaviors']['openlayers_views_cluster_' . $layer_id]['cluster_tooltip_callback'] = $this->options['behaviors']['cluster_tooltip_callback'];
        }
      }
    }
  }

  // end foreach views-group/layer
  // Sort layers by id
  ksort($map['layers']);

  // Check other behavior options
  if (module_exists('openlayers_behaviors')) {
    $map['behaviors'] = is_array($map['behaviors']) ? $map['behaviors'] : array();

    // Popups, which apply to all Vector layers when activated
    // @@TODO: Put checkboxes in the style plugin interface to choose layers to use popups with?
    if ($this->options['behaviors']['popup']) {
      $map['behaviors'] = is_array($map['behaviors']) ? $map['behaviors'] : array();
      $map['behaviors']['openlayers_views_popup'] = array(
        'id' => 'openlayers_views_popup',
        'type' => 'openlayers_behaviors_popup',
        'attribute' => 'openlayers_popup',
      );
    }

    // Zoom to layer
    if ($this->options['behaviors']['zoom_to_layer']) {
      $map['behaviors']['openlayers_views_zoom_to_layer'] = array(
        'id' => 'openlayers_views_zoom_to_layer',
        'type' => 'openlayers_behaviors_zoom_to_layer',
      );
      if ($grouped) {
        $map['behaviors']['openlayers_views_zoom_to_layer']['layer'] = array();
        foreach ($sets as $group_title => $records) {
          $group_name = _openlayers_views_clean($group_title);
          $layer_id = 'openlayers_views_layer_' . $this->view->name;
          $layer_id .= $grouped ? '_' . $group_name : '';
          $map['behaviors']['openlayers_views_zoom_to_layer']['layer'][] = $layer_id;
        }
      }
      else {
        $map['behaviors']['openlayers_views_zoom_to_layer']['layer'] = 'openlayers_views_layer_' . $this->view->name;
      }
    }

    // Full screen
    if ($this->options['behaviors']['fullscreen']) {
      $map['behaviors']['openlayers_views_fullscreen'] = array(
        'id' => 'openlayers_views_fullscreen',
        'type' => 'openlayers_behaviors_fullscreen',
      );
    }
  }

  // Render map
  $map = openlayers_render_map($map);

  // Return map array
  $output .= theme($this
    ->theme_functions(), $this->view, $this->options, $map, $group_name);
  return $output;
}