You are here

public function openlayers_views_plugin_style_source_vector::map_features in Openlayers 7.3

Extract features.

Parameters

array $sets: The array of sets.

Return value

array The array of features.

2 calls to openlayers_views_plugin_style_source_vector::map_features()
openlayers_views_plugin_map_views::render in modules/openlayers_views/views/openlayers_views_plugin_map_views.inc
Render the display in this style.
openlayers_views_plugin_style_source_vector::render in modules/openlayers_views/views/openlayers_views_plugin_style_source_vector.inc
Render the display in this style.

File

modules/openlayers_views/views/openlayers_views_plugin_style_source_vector.inc, line 245
Style handler that provides vector features.

Class

openlayers_views_plugin_style_source_vector
Class openlayers_views_plugin_style_source_vector.

Code

public 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] = $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']];
            if (is_object($handler)) {
              $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]);
          }
        }
      }
      $result = array_map(array(
        $this,
        'minify_data',
      ), $attributes);

      // Create features array.
      $features[] = array(
        'projection' => 'EPSG:4326',
        'attributes' => $result,
        'wkt' => $wkt,
      );
    }
  }
  return $features;
}