You are here

class Baidu in Geolocation Field 8.3

Provides Baidu Maps API.

Plugin annotation


@MapProvider(
  id = "baidu",
  name = @Translation("Baidu Maps"),
  description = @Translation("Baidu support."),
)

Hierarchy

Expanded class hierarchy of Baidu

File

modules/geolocation_baidu/src/Plugin/geolocation/MapProvider/Baidu.php, line 17

Namespace

Drupal\geolocation_baidu\Plugin\geolocation\MapProvider
View source
class Baidu extends MapProviderBase {

  /**
   * Baidu API Url.
   *
   * @var string
   */
  public static $apiBaseUrl = 'https://api.map.baidu.com/api?v=2.0';

  /**
   * {@inheritdoc}
   */
  public static function getDefaultSettings() {
    return array_replace_recursive(parent::getDefaultSettings(), [
      'zoom' => 10,
      'height' => '400px',
      'width' => '100%',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getSettings(array $settings) {
    $settings = parent::getSettings($settings);
    $settings['zoom'] = (int) $settings['zoom'];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsSummary(array $settings) {
    $settings = array_replace_recursive(self::getDefaultSettings(), $settings);
    $summary = parent::getSettingsSummary($settings);
    $summary[] = $this
      ->t('Zoom level: @zoom', [
      '@zoom' => $settings['zoom'],
    ]);
    $summary[] = $this
      ->t('Height: @height', [
      '@height' => $settings['height'],
    ]);
    $summary[] = $this
      ->t('Width: @width', [
      '@width' => $settings['width'],
    ]);
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array $settings, array $parents = []) {
    $settings += self::getDefaultSettings();
    if ($parents) {
      $parents_string = implode('][', $parents);
    }
    else {
      $parents_string = NULL;
    }
    $form = parent::getSettingsForm($settings, $parents);
    $form['height'] = [
      '#group' => $parents_string,
      '#type' => 'textfield',
      '#title' => $this
        ->t('Height'),
      '#description' => $this
        ->t('Enter the dimensions and the measurement units. E.g. 200px or 100%.'),
      '#size' => 4,
      '#default_value' => $settings['height'],
    ];
    $form['width'] = [
      '#group' => $parents_string,
      '#type' => 'textfield',
      '#title' => $this
        ->t('Width'),
      '#description' => $this
        ->t('Enter the dimensions and the measurement units. E.g. 200px or 100%.'),
      '#size' => 4,
      '#default_value' => $settings['width'],
    ];
    $form['zoom'] = [
      '#group' => $parents_string,
      '#type' => 'select',
      '#title' => $this
        ->t('Zoom level'),
      '#options' => range(0, 20),
      '#description' => $this
        ->t('The initial resolution at which to display the map, where zoom 0 corresponds to a map of the Earth fully zoomed out, and higher zoom levels zoom in at a higher resolution.'),
      '#default_value' => $settings['zoom'],
      '#process' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'processGroup',
        ],
        [
          '\\Drupal\\Core\\Render\\Element\\Select',
          'processSelect',
        ],
      ],
      '#pre_render' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'preRenderGroup',
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function alterRenderArray(array $render_array, array $map_settings, array $context = []) {
    $config = \Drupal::config('baidu_maps.settings');
    $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
      'library' => [
        'geolocation_baidu/baidu_maps',
        'geolocation_baidu/geolocation.baidu',
      ],
      'drupalSettings' => [
        'geolocation' => [
          'baiduKey' => $config
            ->get('key'),
          'maps' => [
            $render_array['#id'] => [
              'settings' => [
                'baidu_settings' => $map_settings,
              ],
            ],
          ],
        ],
      ],
    ]);
    return parent::alterRenderArray($render_array, $map_settings, $context);
  }

  /**
   * {@inheritdoc}
   */
  public static function getControlPositions() {
    return [
      'BMAP_ANCHOR_TOP_LEFT' => t('Top left'),
      'BMAP_ANCHOR_TOP_RIGHT' => t('Top right'),
      'BMAP_ANCHOR_BOTTOM_LEFT' => t('Bottom left'),
      'BMAP_ANCHOR_BOTTOM_RIGHT' => t('Bottom right'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function alterCommonMap(array $render_array, array $map_settings, array $context) {
    $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
      'library' => [
        'geolocation_baidu/commonmap.baidu',
      ],
    ]);
    return $render_array;
  }

  /**
   * Get Baidu API Base URL.
   *
   * @return string
   *   Base Url.
   */
  public function getApiUrl() {
    $config = \Drupal::config('baidu_maps.settings');
    $api_key = $config
      ->get('key');
    return self::$apiBaseUrl . '&ak=' . $api_key;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Baidu::$apiBaseUrl public static property Baidu API Url.
Baidu::alterCommonMap public function Alter common map build array. Overrides MapProviderBase::alterCommonMap
Baidu::alterRenderArray public function Alter render array. Overrides MapProviderBase::alterRenderArray
Baidu::getApiUrl public function Get Baidu API Base URL.
Baidu::getControlPositions public static function Return available control positions. Overrides MapProviderBase::getControlPositions
Baidu::getDefaultSettings public static function Provide a populated settings array. Overrides MapProviderBase::getDefaultSettings
Baidu::getSettings public function Provide map provider specific settings ready to handover to JS. Overrides MapProviderBase::getSettings
Baidu::getSettingsForm public function Provide a generic map settings form array. Overrides MapProviderBase::getSettingsForm
Baidu::getSettingsSummary public function Provide a summary array to use in field formatters. Overrides MapProviderBase::getSettingsSummary
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
MapProviderBase::$mapFeatureManager protected property Map feature manager.
MapProviderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MapProviderBase::validateMapFeatureForms public function Validate form.
MapProviderBase::__construct public function Constructs a new GeocoderBase object. Overrides PluginBase::__construct
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.
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.