You are here

function styled_google_map_preprocess_styled_google_map in Styled Google Map 8.2

Implements hook_preprocess_HOOK().

File

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

Code

function styled_google_map_preprocess_styled_google_map(&$variables) {
  $language = \Drupal::languageManager()
    ->getCurrentLanguage();
  $location =& $variables['location'];

  /** @var \Drupal\Core\Entity\FieldableEntityInterface|\Drupal\Core\Entity\TranslatableInterface $entity */
  $entity =& $variables['entity'];
  $settings =& $variables['settings'];
  if (empty($location) || !$location['geohash']) {
    $location = NULL;
    return;
  }

  // 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 ($entity instanceof EntityInterface) {
    switch ($settings['popup']['choice']) {

      // Create popup from field.
      case 1:
        if ($settings['popup']['text'] == 'label') {
          $popup_field = [
            '#markup' => $entity
              ->label(),
          ];
        }
        else {
          $settings['popup']['label'] = $settings['popup']['label'] ? 'inline' : 'hidden';
          $popup_field = $entity->{$settings['popup']['text']}
            ->view([
            '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 = [];
    }
    $location['popup'] = render($popup_field);
  }
  else {

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

  // Add map settings and required libraries.
  $gid = $variables['gid'];

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

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

  // Include the unique div id.
  $map_settings['id'] = 'styled-google-map-' . $gid;
  $variables['#attached']['drupalSettings']['styled_google_map'] = [
    $gid => $gid,
  ];
  $variables['#attached']['drupalSettings']['maps'] = [
    'id' . $gid => $map_settings,
  ];
  if (!empty($settings['directions']['enabled'])) {
    if (!empty($settings['directions']['steps'])) {
      $variables['steps'] = TRUE;
    }
    $variables['directions_form'] = [
      '#theme' => 'styled_google_map_directions',
      '#settings' => $settings['directions'],
      '#id' => $gid,
    ];
    $variables['#attached']['library'][] = 'styled_google_map/geolocation-marker';
    if (\Drupal::service('library.discovery')
      ->getLibraryByName('core', 'drupal.message')) {
      $variables['#attached']['library'][] = 'core/drupal.message';
    }
  }

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