You are here

class LeafletMarkerIcon in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 modules/geolocation_leaflet/src/Plugin/geolocation/MapFeature/LeafletMarkerIcon.php \Drupal\geolocation_leaflet\Plugin\geolocation\MapFeature\LeafletMarkerIcon

Provides marker icon adjustment.

Plugin annotation


@MapFeature(
  id = "leaflet_marker_icon",
  name = @Translation("Marker Icon Adjustment"),
  description = @Translation("Icon properties."),
  type = "leaflet",
)

Hierarchy

Expanded class hierarchy of LeafletMarkerIcon

File

modules/geolocation_leaflet/src/Plugin/geolocation/MapFeature/LeafletMarkerIcon.php, line 18

Namespace

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

  /**
   * {@inheritdoc}
   */
  public static function getDefaultSettings() {
    return [
      'marker_icon_path' => '',
      'icon_size' => [
        'width' => NULL,
        'height' => NULL,
      ],
      'icon_anchor' => [
        'x' => NULL,
        'y' => NULL,
      ],
      'popup_anchor' => [
        'x' => 0,
        'y' => 0,
      ],
      'marker_shadow_path' => '',
      'shadow_size' => [
        'width' => NULL,
        'height' => NULL,
      ],
      'shadow_anchor' => [
        'x' => NULL,
        'y' => NULL,
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array $settings, array $parents) {
    $settings = $this
      ->getSettings($settings);
    $form['marker_icon_path'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Icon path'),
      '#description' => $this
        ->t('Set relative or absolute path to custom marker icon. Tokens supported. Empty for default. Attention: In views contexts, additional icon source options are available in the style settings.'),
      '#default_value' => $settings['marker_icon_path'],
    ];
    $form['icon_size'] = [
      '#type' => 'item',
      '#description' => $this
        ->t('Size of the icon image in pixels.'),
      'width' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Icon Size - Width'),
        '#default_value' => $settings['icon_size']['width'],
        '#min' => 0,
      ],
      'height' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Icon Size - Height'),
        '#default_value' => $settings['icon_size']['height'],
        '#min' => 0,
      ],
    ];
    $form['icon_anchor'] = [
      '#type' => 'item',
      '#description' => $this
        ->t('The coordinates of the "tip" of the icon (relative to its top left corner). The icon will be aligned so that this point is at the marker\'s geographical location. Centered by default if size is specified.'),
      'x' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Icon Anchor - X'),
        '#default_value' => $settings['icon_anchor']['x'],
      ],
      'y' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Icon Anchor - Y'),
        '#default_value' => $settings['icon_anchor']['y'],
      ],
    ];
    $form['popup_anchor'] = [
      '#type' => 'item',
      '#description' => $this
        ->t('The coordinates of the point from which popups will "open", relative to the icon anchor.'),
      'x' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Popup Anchor - X'),
        '#default_value' => $settings['popup_anchor']['x'],
      ],
      'y' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Popup Anchor - Y'),
        '#default_value' => $settings['popup_anchor']['y'],
      ],
    ];
    $form['marker_shadow_path'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Shadow path'),
      '#description' => $this
        ->t('Set relative or absolute path to custom marker shadow. Tokens supported. Empty for default. Attention: In views contexts, additional shadow source options are available in the style settings.'),
      '#default_value' => $settings['marker_shadow_path'],
    ];
    $form['shadow_size'] = [
      '#type' => 'item',
      '#description' => $this
        ->t('Size of the shadow image in pixels.'),
      'width' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Shadow Size - Width'),
        '#default_value' => $settings['shadow_size']['width'],
        '#min' => 0,
      ],
      'height' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Shadow Size - Height'),
        '#default_value' => $settings['shadow_size']['height'],
        '#min' => 0,
      ],
    ];
    $form['shadow_anchor'] = [
      '#type' => 'item',
      '#description' => $this
        ->t('The coordinates of the "tip" of the shadow (relative to its top left corner) (the same as iconAnchor if not specified).'),
      'x' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Shadow Anchor - X'),
        '#default_value' => $settings['shadow_anchor']['x'],
      ],
      'y' => [
        '#type' => 'number',
        '#title' => $this
          ->t('Shadow Anchor - Y'),
        '#default_value' => $settings['shadow_anchor']['y'],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function alterMap(array $render_array, array $feature_settings, array $context = []) {
    $render_array = parent::alterMap($render_array, $feature_settings, $context);
    $feature_settings = $this
      ->getSettings($feature_settings);
    $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
      'library' => [
        'geolocation_leaflet/mapfeature.' . $this
          ->getPluginId(),
      ],
      'drupalSettings' => [
        'geolocation' => [
          'maps' => [
            $render_array['#id'] => [
              $this
                ->getPluginId() => [
                'enable' => TRUE,
                'iconSize' => [
                  'width' => (int) $feature_settings['icon_size']['width'],
                  'height' => (int) $feature_settings['icon_size']['height'],
                ],
                'iconAnchor' => [
                  'x' => (int) $feature_settings['icon_anchor']['x'],
                  'y' => (int) $feature_settings['icon_anchor']['y'],
                ],
                'popupAnchor' => [
                  'x' => (int) $feature_settings['popup_anchor']['x'],
                  'y' => (int) $feature_settings['popup_anchor']['y'],
                ],
                'shadowSize' => [
                  'width' => (int) $feature_settings['shadow_size']['width'],
                  'height' => (int) $feature_settings['shadow_size']['height'],
                ],
                'shadowAnchor' => [
                  'x' => (int) $feature_settings['shadow_anchor']['x'],
                  'y' => (int) $feature_settings['shadow_anchor']['y'],
                ],
              ],
            ],
          ],
        ],
      ],
    ]);
    if (!empty($feature_settings['marker_icon_path'])) {
      $data = [];
      if (!empty($context['view'])) {
        $data['view'] = $context['view'];
      }
      $iconPath = \Drupal::token()
        ->replace($feature_settings['marker_icon_path'], $data);
      $iconUrl = file_url_transform_relative(file_create_url($iconPath));
      $render_array['#attached']['drupalSettings']['geolocation']['maps'][$render_array['#id']][$this
        ->getPluginId()]['markerIconPath'] = $iconUrl;
    }
    if (!empty($feature_settings['marker_shadow_path'])) {
      $data = [];
      if (!empty($context['view'])) {
        $data['view'] = $context['view'];
      }
      $shadowPath = \Drupal::token()
        ->replace($feature_settings['marker_shadow_path'], $data);
      $shadowUrl = file_url_transform_relative(file_create_url($shadowPath));
      $render_array['#attached']['drupalSettings']['geolocation']['maps'][$render_array['#id']][$this
        ->getPluginId()]['markerShadowPath'] = $shadowUrl;
    }
    return $render_array;
  }

}

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
LeafletMarkerIcon::alterMap public function Alter render array. Overrides MapFeatureBase::alterMap
LeafletMarkerIcon::getDefaultSettings public static function Provide a populated settings array. Overrides MapFeatureBase::getDefaultSettings
LeafletMarkerIcon::getSettingsForm public function Provide a generic map settings form array. Overrides MapFeatureBase::getSettingsForm
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 1
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.