You are here

function theme_styled_google_map in Styled Google Map 8

Same name and namespace in other branches
  1. 7.2 styled_google_map.module \theme_styled_google_map()
  2. 7 styled_google_map.module \theme_styled_google_map()

Returns HTML for the styled google map.

Parameters

array $variables: An associative array containing:

  • location: The location object including longitude and latitude.
  • display: Display array including formatter settings.

Return value

mixed|null Rendered map.

1 call to theme_styled_google_map()
StyledGoogleMapDefaultFormatter::viewElements in src/Plugin/Field/FieldFormatter/StyledGoogleMapDefaultFormatter.php
Builds a renderable array for a field value.

File

./styled_google_map.module, line 83
Contains all hooks and functions for the Styled Google Map module.

Code

function theme_styled_google_map(array $variables) {
  $language = Drupal::languageManager()
    ->getCurrentLanguage();
  $location = $variables['location']
    ->getValue();
  $field_list = $variables['location']
    ->getParent();
  $entity = $field_list
    ->getEntity();
  $settings = $variables['settings'];
  $output = array();

  // TODO: Split this function so logic is out of the theming function.
  if (!empty($location) && $location['geohash']) {

    // Get the pin file url.
    if (isset($settings['style']['pin']) && !empty($settings['style']['pin'])) {
      $settings['style']['pin'] = file_create_url($settings['style']['pin']);
    }

    // Sanitize the output of the style settings.
    foreach ($settings['style'] as $id => $setting) {
      if ($id != 'style') {
        $location[$id] = Xss::filter($settings['style'][$id]);
      }
    }

    // Get the label settings.
    if (isset($entity) && !empty($entity)) {
      switch ($settings['popup']['choice']) {

        // Create popup from label.
        case 1:
          $settings['popup']['label'] = $settings['popup']['label'] ? 'inline' : 'hidden';
          $popup_field = $entity->{$settings['popup']['text']}
            ->view(array(
            'label' => $settings['popup']['label'],
          ), $language
            ->getId());
          break;

        // Create popup from view mode.
        case 2:
          $display = EntityViewDisplay::collectRenderDisplay($entity, $settings['popup']['view_mode']);
          $popup_field = $display
            ->build($entity);
          break;

        // Default to empty.
        default:
          $popup_field = array();
      }
      $location['popup'] = render($popup_field);
    }
    else {

      // Not an entity object.
      $location['popup'] = array();
    }
    if ($settings['map_center']['center_coordinates']) {
      $map_center = $entity
        ->getTranslation($language
        ->getId())
        ->get($settings['map_center']['center_coordinates'])
        ->getValue();
      if ($map_center && isset($map_center[0]['lat']) && isset($map_center[0]['lon'])) {
        $settings['map_center']['center_coordinates'] = $map_center[0];
      }
      else {
        $settings['map_center']['center_coordinates'] = FALSE;
      }
    }
    $gid = uniqid();

    // Include the Google Maps API.
    // Include the map location settings.
    $map_settings['locations'] = array(
      $location,
    );

    // Include the custom map settings.
    $map_settings['settings'] = $settings;

    // Include the unique div id.
    $map_settings['id'] = 'styled-google-map-' . $gid;
    $output['#attached']['drupalSettings']['styled_google_map'] = array(
      $gid => $gid,
    );
    $output['#attached']['drupalSettings']['maps'] = array(
      'id' . $gid => $map_settings,
    );

    // Output a div placeholder for the Styled Google Map.
    $output['styled_google_map']['#markup'] = '<div class="styled_map" id="styled-google-map-' . $gid . '"></div>';

    // Attach the Styled Google Map javascript file.
    $output['#attached']['library'][] = 'styled_google_map/styled-google-map';
  }
  return render($output);
}