View source
<?php
namespace Drupal\geofield_map;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\geofield_map\Plugin\views\style\GeofieldGoogleMapViewStyle;
use Drupal\geofield_map\Services\MarkerIconService;
use Drupal\Core\Entity\EntityType as ViewEntityType;
abstract class MapThemerBase extends PluginBase implements MapThemerInterface, ContainerFactoryPluginInterface {
use StringTranslationTrait;
protected $config;
protected $translationManager;
protected $entityManager;
protected $renderer;
protected $markerIcon;
protected function buildTableHeader(array $table_settings) {
return [
'#type' => 'table',
'#header' => [
$table_settings['header']['label'],
$this
->t('Weight'),
$table_settings['header']['label_alias'],
$table_settings['header']['marker_icon'],
$table_settings['header']['image_style'],
$this
->t('Hide from Legend'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => $table_settings['tabledrag_group'],
],
],
'#caption' => $this->renderer
->renderPlain($table_settings['caption']),
];
}
protected function buildDefaultMapThemerRow(array $row) {
$element = [
'label' => [
'#type' => 'value',
'#value' => $row['label']['value'],
'markup' => [
'#markup' => $row['label']['markup'],
],
],
'weight' => [
'#type' => 'weight',
'#title' => '',
'#title_display' => 'invisible',
'#default_value' => $row['weight']['value'],
'#delta' => 20,
'#attributes' => [
'class' => [
$row['weight']['class'],
],
],
],
'label_alias' => [
'#type' => 'textfield',
'#default_value' => $row['label_alias']['value'],
'#size' => 30,
'#maxlength' => 128,
],
'icon_file' => array_key_exists('icon_file_id', $row) ? $this->markerIcon
->getIconFileManagedElement($row['icon_file_id'], $row['id']) : (array_key_exists('icon_file_uri', $row) ? $this->markerIcon
->getIconFileSelectElement($row['icon_file_uri'], $row['id']) : NULL),
'image_style' => array_key_exists('icon_file_id', $row) ? [
'#type' => 'select',
'#title' => $this
->t('Image style'),
'#title_display' => 'invisible',
'#options' => $row['image_style']['options'],
'#default_value' => $row['image_style']['value'],
'#states' => [
'visible' => [
':input[name="style_options[map_marker_and_infowindow][theming]' . $row['id'] . '[icon_file][is_svg]"]' => [
'checked' => FALSE,
],
],
],
] : [],
'legend_exclude' => [
'#type' => 'checkbox',
'#default_value' => $row['legend_exclude']['value'],
'#return_value' => 1,
],
'image_style_svg' => array_key_exists('icon_file_id', $row) ? [
'#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]' . $row['id'] . '[icon_file][is_svg]"]' => [
'checked' => FALSE,
],
],
],
] : [],
'#attributes' => $row['attributes'],
];
return $element;
}
protected function getDefaultLegendIcon() {
return [
'#type' => 'container',
'markup' => [
'#markup' => $this
->t('[default-icon]'),
],
'#attributes' => [
'class' => [
'default-icon',
],
],
];
}
protected function defaultLegendHeader(array $configuration) {
$legend_default_settings = $this
->defaultSettings('legend');
return [
'#type' => 'container',
'#attributes' => [
'class' => [
'geofield-map-legend',
$legend_default_settings['class'],
],
],
'table' => [
'#type' => 'table',
'#caption' => isset($configuration['legend_caption']) ? $configuration['legend_caption'] : '',
'#header' => [
isset($configuration['values_label']) ? $configuration['values_label'] : '',
isset($configuration['markers_label']) ? $configuration['markers_label'] : '',
],
],
];
}
protected function defaultLegendFooter(array $configuration) {
return [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => isset($configuration['legend_notes']) ? $configuration['legend_notes'] : '',
'#attributes' => [
'class' => [
'notes',
],
],
];
}
protected function getMapThemerEntityBundles(GeofieldGoogleMapViewStyle $geofieldMapView, $entity_type, array $entity_bundles) : array {
$view_bundles = $entity_type instanceof ViewEntityType && $geofieldMapView
->getViewEntityType() == $entity_type
->id() && !empty($geofieldMapView
->getViewFilteredBundles()) ? $geofieldMapView
->getViewFilteredBundles() : array_keys($entity_bundles);
return $view_bundles;
}
public function getLabelAliasHelp() {
return [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('If not empty, this will be used in the legend.'),
'#attributes' => [
'style' => [
'style' => 'font-size:0.8em; color: gray; font-weight: normal',
],
],
];
}
protected function renderDefaultLegendIcon() {
return TRUE;
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translation_manager, RendererInterface $renderer, EntityTypeManagerInterface $entity_manager, MarkerIconService $marker_icon_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configuration = $configuration;
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
$this
->setStringTranslation($translation_manager);
$this->renderer = $renderer;
$this->entityManager = $entity_manager;
$this->markerIcon = $marker_icon_service;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('string_translation'), $container
->get('renderer'), $container
->get('entity_type.manager'), $container
->get('geofield_map.marker_icon'));
}
public function getName() {
return $this->pluginDefinition['name'];
}
public function getDescription() {
return $this->pluginDefinition['description'];
}
public function defaultSettings($k = NULL) {
$default_settings = $this->pluginDefinition['defaultSettings'];
if (!empty($k)) {
return $default_settings[$k];
}
return $default_settings;
}
public function getDefaultThemerElement(array $defaults) {
$default_value = !empty($defaults['map_marker_and_infowindow']['theming'][$this->pluginId]['values']) ? $defaults['map_marker_and_infowindow']['theming'][$this->pluginId]['values'] : $this
->defaultSettings('values');
return $default_value;
}
}