You are here

function openlayers_views_style_data::map_features in Openlayers 7.2

Same name and namespace in other branches
  1. 6.2 modules/openlayers_views/views/openlayers_views_style_data.inc \openlayers_views_style_data::map_features()

Parameters

$records ...:

openlayers_views_style_data specific

1 call to openlayers_views_style_data::map_features()
openlayers_views_style_data::render in modules/openlayers_views/views/openlayers_views_style_data.inc
Render the map features.

File

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

Class

openlayers_views_style_data
@class Extension of the Views Plugin Style for OpenLayers

Code

function map_features($sets = array()) {
  $features = $excluded_fields = array();
  $handlers = $this->display->handler
    ->get_handlers('field');
  foreach ($sets as $title => $records) {
    foreach ($records as $id => $record) {
      $this->view->row_index = $id;
      $attributes = array();
      $wkt = NULL;
      $field_exclude = array();

      // Save the 'exclude' options of fields as backup.
      foreach ($this->view->field as $fid => $field) {
        $field_exclude[$fid] = $field->options['exclude'];
      }

      // Loop through each fields and render it if there were only one
      // field in the row, so we have all the goodness of Views's field
      // wrapping, class customizations and label.
      foreach ($handlers as $hid => $handler) {
        $field_id = $handler->options['id'];

        // Exclude all the field from rendering
        foreach ($this->view->field as $field) {
          $field->options['exclude'] = TRUE;
        }

        // Enable only the field we want to render the row.
        $this->view->field[$field_id]->options['exclude'] = FALSE;

        // Render the row.
        $attributes[$hid] = trim($this->row_plugin
          ->render($record));
      }

      // Restore the state of the exclude options in the fields.
      foreach ($this->view->field as $fid => $field) {
        $this->view->field[$fid]->options['exclude'] = $field_exclude[$fid];
      }

      // Add the 'name' attribute.
      if (isset($this->options['data_source']['name_field'])) {
        if (isset($attributes[$this->options['data_source']['name_field']])) {
          $attributes['name'] = $attributes[$this->options['data_source']['name_field']];
        }
      }

      // Add the 'description' attribute.
      if (isset($this->options['data_source']['description_field'])) {

        // Handle rendering the whole record.
        if ($this->options['data_source']['description_field'] === '#row') {
          $attributes['description'] = trim($this->row_plugin
            ->render($record));
        }
        else {
          if (isset($attributes[$this->options['data_source']['description_field']])) {
            $attributes['description'] = $attributes[$this->options['data_source']['description_field']];
          }
        }
      }

      // We do not need the rendered value for these fields,
      // just the raw value.
      // This is why we use $this->rendered_fields.
      if (isset($this->options['data_source']['value'])) {
        switch ($this->options['data_source']['value']) {
          case 'wkt':
            $handler = $handlers[$this->options['data_source']['wkt']];
            $wkt = $handler
              ->allow_advanced_render() ? $handler
              ->advanced_render($record) : $handler
              ->render($record);
            break;
          case 'other_latlon':
            $handler = $handlers[$this->options['data_source']['other_lon']];
            $other_lon = $handler
              ->allow_advanced_render() ? $handler
              ->advanced_render($record) : $handler
              ->render($record);
            $handler = $handlers[$this->options['data_source']['other_lat']];
            $other_lat = $handler
              ->allow_advanced_render() ? $handler
              ->advanced_render($record) : $handler
              ->render($record);
            $wkt = 'POINT(' . $other_lon . ' ' . $other_lat . ')';
            break;
        }
      }

      // Only render features that has been enabled in the configuration
      // of the display.
      if (isset($this->options['attributes']['display'])) {
        foreach ($this->options['attributes']['display'] as $fid => $value) {
          if ($this->options['attributes']['display'][$fid] === 0) {
            unset($attributes[$fid]);
          }
        }
      }

      // Create features array.
      $feature = array(
        'projection' => $this->options['data_source']['projection'],
        'attributes' => $attributes,
        'wkt' => $wkt,
      );
      $features = $this
        ->reduce_features($this->options['grouping'], $features, $feature, $title);
    }
  }

  // For grouping, handle a bit differently.
  if ($this->options['grouping']) {
    $features = $this
      ->coalesce_groups($features, $handlers, $this->options['data_source']);
  }
  return $features;
}