You are here

class LeafletTileLayerOverlay in Geolocation Field 8.3

Provides map tile layer overlay support.

Plugin annotation


@MapFeature(
  id = "leaflet_tile_layer_overlay",
  name = @Translation("Tile Layer - Overlays"),
  description = @Translation("Select a map tile layer overlay."),
  type = "leaflet",
)

Hierarchy

Expanded class hierarchy of LeafletTileLayerOverlay

File

modules/geolocation_leaflet/src/Plugin/geolocation/MapFeature/LeafletTileLayerOverlay.php, line 21

Namespace

Drupal\geolocation_leaflet\Plugin\geolocation\MapFeature
View source
class LeafletTileLayerOverlay extends MapFeatureBase {

  /**
   * {@inheritdoc}
   */
  public static function getDefaultSettings() {
    return [
      'tile_layer_overlay' => 'OpenInfraMap.Power',
    ];
  }

  /**
   * Return options array for tile overlay.
   *
   * @param string $overlay
   *   Map tile overlay selected.
   *
   * @return array
   *   Options form.
   */
  public static function getOptionsForm($overlay) {
    $form = [
      '#prefix' => '<div id="tile-overlay-settings">',
      '#suffix' => '</div>',
    ];
    if ($overlay == 'OpenWeatherMap') {
      $form['apiKey'] = [
        '#type' => 'textfield',
        '#title' => t('API key'),
        '#default_value' => '',
        '#description' => t('Get your API Key here <a href="@url">@overlay</a>.', [
          '@url' => 'https://openweathermap.org/',
          '@overlay' => $overlay,
        ]),
      ];
    }
    return $form;
  }

  /**
   * Return settings array for tile overlay after select change.
   *
   * @param array $form
   *   Form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Current From State.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   Settings form.
   */
  public static function addTileOverlaySettingsFormAjax(array $form, FormStateInterface $form_state) {
    $ajax_response = new AjaxResponse();
    $triggering_element_value = $form_state
      ->getTriggeringElement()['#value'];
    $overlay = explode('.', $triggering_element_value)[0];
    $form = LeafletTileLayerOverlay::getOptionsForm($overlay);
    $ajax_response
      ->addCommand(new ReplaceCommand('#tile-overlay-settings', $form));
    return $ajax_response;
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array $settings, array $parents) {
    $settings = array_replace_recursive(self::getDefaultSettings(), $settings);
    $form['tile_layer_overlay'] = [
      '#type' => 'select',
      '#options' => $this
        ->getTileOverlays(),
      '#default_value' => $settings['tile_layer_overlay'],
      '#ajax' => [
        'callback' => [
          $this,
          'addTileOverlaySettingsFormAjax',
        ],
        'wrapper' => 'tile-overlay-settings',
        'effect' => 'fade',
      ],
    ];
    $overlay = explode('.', $settings['tile_layer_overlay'])[0];
    $form['tile_overlay_options'] = LeafletTileLayerOverlay::getOptionsForm($overlay);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function alterMap(array $render_array, array $feature_settings, array $context = []) {
    $render_array = parent::alterMap($render_array, $feature_settings, $context);
    $tileLayer = [
      'enable' => TRUE,
      'tileLayerOverlay' => $feature_settings['tile_layer_overlay'],
    ];
    if (isset($feature_settings['tile_overlay_options'])) {
      $tileLayer['tileLayerOptions'] = $feature_settings['tile_overlay_options'];
    }
    $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
      'library' => [
        'geolocation_leaflet/mapfeature.tilelayeroverlay',
      ],
      'drupalSettings' => [
        'geolocation' => [
          'maps' => [
            $render_array['#id'] => [
              $this
                ->getPluginId() => $tileLayer,
            ],
          ],
        ],
      ],
    ]);
    return $render_array;
  }

  /**
   * Provide some available tile overlays.
   *
   * @return array
   *   An array containing tile overlay IDs.
   */
  private function getTileOverlays() {
    return [
      'OpenInfraMap' => [
        'OpenInfraMap.Power' => 'OpenInfraMap Power',
        'OpenInfraMap.Telecom' => 'OpenInfraMap Telecom',
        'OpenInfraMap.Petroleum' => 'OpenInfraMap Petroleum',
        'OpenInfraMap.Water' => 'OpenInfraMap Water',
      ],
      'OpenSeaMap' => [
        'OpenSeaMap' => 'OpenSeaMap',
      ],
      'OpenPtMap' => [
        'OpenPtMap' => 'OpenPtMap',
      ],
      'OpenRailwayMap' => [
        'OpenRailwayMap' => 'OpenRailwayMap',
      ],
      'OpenFireMap' => [
        'OpenFireMap' => 'OpenFireMap',
      ],
      'SafeCast' => [
        'SafeCast' => 'SafeCast',
      ],
      'OpenMapSurfer' => [
        'OpenMapSurfer.AdminBounds' => 'OpenMapSurfer AdminBounds',
      ],
      'Hydda' => [
        'Hydda.RoadsAndLabels' => 'Hydda RoadsAndLabels',
      ],
      'Stamen' => [
        'Stamen.TonerHybrid' => 'Stamen TonerHybrid',
        'Stamen.TonerLines' => 'Stamen TonerLines',
        'Stamen.TonerLabels' => 'Stamen TonerLabels',
        'Stamen.TopOSMFeatures' => 'Stamen TopOSMFeatures',
      ],
      'OpenWeatherMap' => [
        'OpenWeatherMap.Clouds' => 'OpenWeatherMap Clouds',
        'OpenWeatherMap.CloudsClassic' => 'OpenWeatherMap CloudsClassic',
        'OpenWeatherMap.Precipitation' => 'OpenWeatherMap Precipitation',
        'OpenWeatherMap.PrecipitationClassic' => 'OpenWeatherMap PrecipitationClassic',
        'OpenWeatherMap.Rain' => 'OpenWeatherMap Rain',
        'OpenWeatherMap.RainClassic' => 'OpenWeatherMap RainClassic',
        'OpenWeatherMap.Pressure' => 'OpenWeatherMap Pressure',
        'OpenWeatherMap.PressureContour' => 'OpenWeatherMap PressureContour',
        'OpenWeatherMap.Wind' => 'OpenWeatherMap Wind',
        'OpenWeatherMap.Temperature' => 'OpenWeatherMap Temperature',
        'OpenWeatherMap.Snow' => 'OpenWeatherMap Snow',
      ],
      'JusticeMap' => [
        'JusticeMap.income' => 'JusticeMap income',
        'JusticeMap.americanIndian' => 'JusticeMap americanIndian',
        'JusticeMap.asian' => 'JusticeMap asian',
        'JusticeMap.black' => 'JusticeMap black',
        'JusticeMap.hispanic' => 'JusticeMap hispanic',
        'JusticeMap.multi' => 'JusticeMap multi',
        'JusticeMap.nonWhite' => 'JusticeMap nonWhite',
        'JusticeMap.white' => 'JusticeMap white',
        'JusticeMap.plurality' => 'JusticeMap plurality',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
LeafletTileLayerOverlay::addTileOverlaySettingsFormAjax public static function Return settings array for tile overlay after select change.
LeafletTileLayerOverlay::alterMap public function Alter render array. Overrides MapFeatureBase::alterMap
LeafletTileLayerOverlay::getDefaultSettings public static function Provide a populated settings array. Overrides MapFeatureBase::getDefaultSettings
LeafletTileLayerOverlay::getOptionsForm public static function Return options array for tile overlay.
LeafletTileLayerOverlay::getSettingsForm public function Provide a generic map settings form array. Overrides MapFeatureBase::getSettingsForm
LeafletTileLayerOverlay::getTileOverlays private function Provide some available tile overlays.
MapFeatureBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
MapFeatureBase::getSettings public function Provide map feature specific settings ready to handover to JS. Overrides MapFeatureInterface::getSettings
MapFeatureBase::getSettingsSummary public function Provide a summary array to use in field formatters. Overrides MapFeatureInterface::getSettingsSummary 2
MapFeatureBase::validateSettingsForm public function Validate Feature Form. Overrides MapFeatureInterface::validateSettingsForm 5
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.