View source
<?php
namespace Drupal\geofield_map\Plugin\GeofieldMapThemer;
use Drupal\geofield_map\MapThemerBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\geofield_map\Plugin\views\style\GeofieldGoogleMapViewStyle;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Markup;
class CustomIconThemer extends MapThemerBase {
public function buildMapThemerElement(array $defaults, array &$form, FormStateInterface $form_state, GeofieldGoogleMapViewStyle $geofieldMapView) {
$default_element = $this
->getDefaultThemerElement($defaults);
$file_upload_help = $this->markerIcon
->getFileUploadHelp();
$fid = (int) (!empty($default_element['icon_file']['fids'])) ? $default_element['icon_file']['fids'] : NULL;
$element = [
'#markup' => Markup::create($this
->t('<label>Custom Icon Image File</label>')),
'#type' => 'container',
'description' => [
'#markup' => Markup::create($this
->t('The chosen icon file will be used as Marker for all Geofield Map features @file_upload_help', [
'@file_upload_help' => $this->renderer
->renderPlain($file_upload_help),
])),
],
'icon_file' => $this->markerIcon
->getIconFileManagedElement($fid[0]),
'image_style' => [
'#type' => 'select',
'#title' => $this
->t('Image style'),
'#options' => $this->markerIcon
->getImageStyleOptions(),
'#default_value' => isset($default_element['image_style']) ? $default_element['image_style'] : 'geofield_map_default_icon_style',
'#states' => [
'visible' => [
':input[name="style_options[map_marker_and_infowindow][theming][geofieldmap_custom_icon][values][icon_file][is_svg]"]' => [
'checked' => FALSE,
],
],
],
],
'image_style_svg' => [
'#type' => 'container',
'warning' => [
'#markup' => $this
->t("Image style cannot apply to SVG Files,<br>SVG natural dimension will be applied."),
],
'#states' => [
'invisible' => [
':input[name="style_options[map_marker_and_infowindow][theming][geofieldmap_custom_icon][values][icon_file][is_svg]"]' => [
'checked' => FALSE,
],
],
],
],
'label_alias' => [
'#type' => 'textfield',
'#title' => $this
->t('Label alias'),
'#default_value' => isset($default_element['label_alias']) ? $default_element['label_alias'] : '',
'#description' => $this
->t('If not empty, this will be used in the legend.'),
'#size' => 20,
],
];
return $element;
}
public function getIcon(array $datum, GeofieldGoogleMapViewStyle $geofieldMapView, EntityInterface $entity, $map_theming_values) {
if (!empty($map_theming_values['icon_file']['fids'])) {
$image_style = isset($map_theming_values['image_style']) ? $map_theming_values['image_style'] : 'none';
return $this->markerIcon
->getFileManagedUrl($map_theming_values['icon_file']['fids'][0], $image_style);
}
return NULL;
}
public function getLegend(array $map_theming_values, array $configuration = []) {
$legend = $this
->defaultLegendHeader($configuration);
$image_style = isset($configuration['markers_image_style']) ? $configuration['markers_image_style'] : 'none';
if (isset($configuration['markers_image_style']) && $configuration['markers_image_style'] == '_map_theming_image_style_') {
$image_style = isset($map_theming_values['image_style']) ? $map_theming_values['image_style'] : 'none';
}
$fid = (int) (!empty($map_theming_values['icon_file']['fids'])) ? $map_theming_values['icon_file']['fids'][0] : NULL;
$legend['table']['custom-icon'] = [
'value' => [
'#type' => 'container',
'label' => [
'#markup' => !empty($map_theming_values['label_alias']) ? $map_theming_values['label_alias'] : $this
->t('All Markers'),
],
'#attributes' => [
'class' => [
'value',
],
],
],
'marker' => [
'#type' => 'container',
'icon_file' => !empty($fid) ? $this->markerIcon
->getLegendIconFromFid($fid, $image_style) : $this
->getDefaultLegendIcon(),
'#attributes' => [
'class' => [
'marker',
],
],
],
];
$legend['notes'] = [
'#markup' => isset($configuration['legend_notes']) ? $configuration['legend_notes'] : '',
'#attributes' => [
'class' => [
'notes',
],
],
];
return $legend;
}
}