You are here

public function MapCenterManager::alterMap in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 src/MapCenterManager.php \Drupal\geolocation\MapCenterManager::alterMap()

Alter map element.

Parameters

array $map: Map render array.

array $settings: Center option settings.

mixed $context: Context.

Return value

array Altered map render array.

File

src/MapCenterManager.php, line 179

Class

MapCenterManager
Search plugin manager.

Namespace

Drupal\geolocation

Code

public function alterMap(array $map, array $settings, $context = NULL) {
  $map = array_replace_recursive($map, [
    '#attached' => [
      'drupalSettings' => [
        'geolocation' => [
          'maps' => [
            $map['#id'] => [
              'map_center' => [],
            ],
          ],
        ],
      ],
    ],
  ]);

  /*
   * Centre handling.
   */
  foreach ($settings as $option_id => $option) {
    if (!empty($map['#centre'])) {
      continue;
    }

    // Ignore if not enabled.
    if (empty($option['enable'])) {
      continue;
    }

    // Compatibility to v1.
    if (empty($option['map_center_id'])) {
      $option['map_center_id'] = $option_id;
    }
    if (!$this
      ->hasDefinition($option['map_center_id'])) {
      continue;
    }
    if (empty($option['weight'])) {
      $option['weight'] = 1;
    }

    /** @var \Drupal\geolocation\MapCenterInterface $map_center_plugin */
    $map_center_plugin = $this
      ->createInstance($option['map_center_id']);
    $map_center_plugin_settings = $option['settings'] ?? [];
    $map_center_plugin_settings = $map_center_plugin
      ->getSettings($map_center_plugin_settings);
    $map['#attached']['drupalSettings']['geolocation']['maps'][$map['#id']]['map_center'][$option['map_center_id']] = [
      'map_center_id' => $option['map_center_id'],
      'option_id' => $option_id,
      'settings' => $map_center_plugin_settings,
      'weight' => $option['weight'],
    ];
    $map = $map_center_plugin
      ->alterMap($map, $option_id, $map_center_plugin_settings, $context);
  }
  if (empty($map['#centre'])) {
    $map['#centre'] = [
      'lat' => 0,
      'lng' => 0,
    ];
  }
  return $map;
}