You are here

function _openlayers_cck_render_element_map in Openlayers 6

Same name and namespace in other branches
  1. 6.2 modules/openlayers_cck/openlayers_cck.module \_openlayers_cck_render_element_map()

Render Map for Widget

Parameters

$field_name: CCK name of field

$values: Current default values

$field: Array of field data

Return value

Rendered map array

1 call to _openlayers_cck_render_element_map()
openlayers_cck_wkt_element_process in modules/openlayers_cck/openlayers_cck.module
Process an individual element.

File

modules/openlayers_cck/openlayers_cck.module, line 713
This file holds the main Drupal hook functions and private functions for the openlayers_cck module.

Code

function _openlayers_cck_render_element_map($field_name = '', $field_label = '', $values = array(), $field = array()) {
  $field_name_html = str_replace('_', '-', $field_name);
  $map_id = OPENLAYERS_CCK_WIDGET_MAP_ID_PREFIX . '-' . $field_name;
  $field_type = $field['type'];
  $widget = $field['widget'];
  $field_container = 'edit-' . $field_name_html . '-openlayers-wkt-wrapper';
  $field_id = 'edit-' . $field_name_html . '-openlayers-wkt';
  $hidden_field_id = 'edit-' . $field_name_html . '-openlayers-wkt-hidden';

  // Get map to use for field
  if (isset($widget['openlayers_cck_preset_map'])) {
    $map = openlayers_get_map($widget['openlayers_cck_preset_map']);
  }
  else {
    $map = openlayers_get_default_map();
  }

  // Put together map
  $map['id'] = $map_id;

  // Add main event for CCK processing
  $map['events']['beforeBehaviors'] = array(
    'CCKProcess',
  );

  // Make sure that our display projection matches the database projection
  $map['options']['displayProjection'] = $field['srid'] != 0 ? $field['srid'] : '4326';

  // Define a vector layer for our features
  $map['layers']['openlayers_cck_vector'] = array(
    'id' => 'openlayers_cck_vector',
    'type' => 'Vector',
    'name' => $field_label,
    'options' => array(),
    'events' => array(
      'featureselected' => array(
        'CCKFeaturesSelected',
      ),
      'featureunselected' => array(
        'CCKFeaturesUnselected',
      ),
    ),
  );

  // Create base behavior array
  $behavior_base = array(
    'type' => 'openlayers_behaviors_draw_features',
    'layer' => 'openlayers_cck_vector',
    'featureadded_handler' => array(
      'OL.CCK.featureAdded',
    ),
    'featuremodified_handler' => array(
      'OL.CCK.featureModified',
    ),
    'featureremoved_handler' => array(
      'OL.CCK.featureRemoved',
    ),
  );

  // Look for geo type
  if ($field_type == 'geo' && isset($field['geo_type'])) {

    // Feature types are named slightly different for lines ('linestring' vs. 'path') so we need to check.
    $type = $field['geo_type'] == 'linestring' ? 'path' : $field['geo_type'];
    $map['behaviors']['openlayers_cck_edit_' . $type] = $behavior_base + array(
      'id' => 'openlayers_cck_edit_' . $type,
      'feature_type' => $type,
    );
  }
  else {

    // Check which feature types are available
    if ($field['openlayers_cck_feature_types']) {
      foreach ($field['openlayers_cck_feature_types'] as $type) {
        if (!empty($type)) {
          $map['behaviors']['openlayers_cck_edit_' . $type] = $behavior_base + array(
            'id' => 'openlayers_cck_edit_' . $type,
            'feature_type' => $type,
          );
        }
      }
    }
    else {

      // @@TODO: Allow default values using a map
      // We are in the widget settings screen and it wants default values
      return FALSE;
    }
  }

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

  // Check errors
  if (!$rendered_map['errors']) {

    // Add JS
    drupal_add_js(drupal_get_path('module', 'openlayers_cck') . '/js/openlayers_cck.js');

    //Add CSS
    drupal_add_css(drupal_get_path('module', 'openlayers_cck') . '/openlayers_cck.css', 'module');

    // Put together array for JS
    $openlayers_cck = array(
      'openlayers_cck' => array(
        'maps' => array(
          $map_id => array(
            'field_name' => $field_name,
            'field_data' => $field,
            'field_name_js' => $field_name_html,
            'field_container' => $field_container,
            'field_items' => $items,
            'field_id' => $field_id,
            'hidden_field_id' => $hidden_field_id,
            'map_container' => 'openlayers-cck-map-container-' . $map_id,
          ),
        ),
      ),
    );

    // Add JS settings
    drupal_add_js($openlayers_cck, 'setting');
  }
  return $rendered_map;
}