You are here

function geofield_map_field_formatter_view in Geofield 7

Same name and namespace in other branches
  1. 7.2 modules/geofield_map/geofield_map.module \geofield_map_field_formatter_view()

Implements hook_field_formatter_view().

File

modules/geofield_map/geofield_map.module, line 50

Code

function geofield_map_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  list($entity_id) = entity_extract_ids($entity_type, $entity);
  $settings = $display['settings'];
  $map_settings = geofield_map_settings_do($settings);
  geophp_load();
  $data = array();
  foreach ($items as $item) {
    $geometry = geoPHP::load($item['wkt'], 'wkt');
    if (!empty($geometry)) {
      $datum = json_decode($geometry
        ->out('json'));
      $datum->properties = array(
        'description' => entity_label($entity_type, $entity),
      );
      $data[] = $datum;
    }
  }
  if (!empty($data)) {
    $map_id = drupal_html_id("geofield_map_entity_{$entity_type}_{$entity_id}_{$field['field_name']}");
    $js_settings = array(
      $map_id => array(
        'map_settings' => $map_settings,
        'data' => count($data) == 1 ? $data[0] : array(
          'type' => 'GeometryCollection',
          'geometries' => $data,
        ),
      ),
    );
    $container_attributes = array(
      //No need for defaults here - these are populated from the field defaults.
      'style' => "height:{$settings['geofield_map_height']}; width:{$settings['geofield_map_width']}",
      'id' => $map_id,
      'class' => 'geofieldMap',
    );
    $element[0] = array(
      '#attached' => array(
        'js' => array(
          '//maps.googleapis.com/maps/api/js?sensor=false' => array(
            'type' => 'external',
          ),
          drupal_get_path('module', 'geofield_map') . '/js/GeoJSON.js',
          drupal_get_path('module', 'geofield_map') . '/js/geofield_map.js',
          array(
            'data' => array(
              'geofieldMap' => $js_settings,
            ),
            'type' => 'setting',
          ),
        ),
        'css' => array(
          drupal_get_path('module', 'geofield_map') . '/css/geofield_map.css',
        ),
      ),
      '#markup' => '<div' . drupal_attributes($container_attributes) . '></div>',
    );
  }
  return $element;
}