View source
<?php
namespace Drupal\geofield_map\Plugin\views\style;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\geofield_map\Controller\GeofieldMapAjaxPopupController;
use Drupal\geofield_map\GeofieldMapFieldTrait;
use Drupal\geofield_map\GeofieldMapFormElementsValidationTrait;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Entity\Index;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\style\DefaultStyle;
use Drupal\views\ViewExecutable;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\geofield\GeoPHP\GeoPHPInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\geofield_map\Services\GoogleMapsService;
use Drupal\geofield_map\MapThemerPluginManager;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\views\Plugin\views\PluginBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
class GeofieldGoogleMapViewStyle extends DefaultStyle implements ContainerFactoryPluginInterface {
use GeofieldMapFieldTrait;
use GeofieldMapFormElementsValidationTrait;
protected $emptyMapOptions = [
'0' => 'View No Results Behaviour',
'1' => 'Empty Map Centered at the Default Center',
];
protected $defaultSettings;
protected $config;
protected $entitySource;
protected $entityType;
protected $entityInfo;
protected $entityManager;
protected $entityFieldManager;
protected $entityDisplay;
protected $link;
protected $geoPhpWrapper;
protected $currentUser;
protected $messenger;
protected $renderer;
protected $moduleHandler;
protected $fieldTypeManager;
protected $googleMapsService;
protected $viewFields = [];
protected $mapThemerManager;
protected $mapThemersList;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_manager, EntityFieldManagerInterface $entity_field_manager, EntityDisplayRepositoryInterface $entity_display, LinkGeneratorInterface $link_generator, GeoPHPInterface $geophp_wrapper, AccountInterface $current_user, MessengerInterface $messenger, RendererInterface $renderer, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager, GoogleMapsService $google_maps_service, MapThemerPluginManager $map_themer_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->defaultSettings = self::getDefaultSettings();
$this->entityManager = $entity_manager;
$this->entityFieldManager = $entity_field_manager;
$this->entityDisplay = $entity_display;
$this->config = $config_factory;
$this->link = $link_generator;
$this->geoPhpWrapper = $geophp_wrapper;
$this->currentUser = $current_user;
$this->messenger = $messenger;
$this->renderer = $renderer;
$this->moduleHandler = $module_handler;
$this->fieldTypeManager = $field_type_manager;
$this->googleMapsService = $google_maps_service;
$this->mapThemerManager = $map_themer_manager;
$this->mapThemersList = $this->mapThemerManager
->getMapThemersList();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('entity_display.repository'), $container
->get('link_generator'), $container
->get('geofield.geophp'), $container
->get('current_user'), $container
->get('messenger'), $container
->get('renderer'), $container
->get('module_handler'), $container
->get('plugin.manager.field.field_type'), $container
->get('geofield_map.google_maps'), $container
->get('plugin.manager.geofield_map.themer'));
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
if (!empty($options['entity_source']) && $options['entity_source'] != '__base_table') {
$handler = $this->displayHandler
->getHandler('relationship', $options['entity_source']);
$this->entitySource = $options['entity_source'];
$data = Views::viewsData();
if (($table = $data
->get($handler->definition['base'])) && !empty($table['table']['entity type'])) {
try {
$this->entityInfo = $this->entityManager
->getDefinition($table['table']['entity type']);
$this->entityType = $this->entityInfo
->id();
} catch (\Exception $e) {
watchdog_exception('geofield_map', $e);
}
}
}
else {
$this->entitySource = '__base_table';
$base_tables = array_keys($view
->getBaseTables());
$base_table = reset($base_tables);
if ($this->entityInfo = $view
->getBaseEntityType()) {
$this->entityType = $this->entityInfo
->id();
return;
}
if (!isset($this->entityType)) {
$index_id = substr($base_table, 17);
$index = Index::load($index_id);
foreach ($index
->getDatasources() as $datasource) {
if ($datasource instanceof DatasourceInterface) {
$this->entityType = $datasource
->getEntityTypeId();
try {
$this->entityInfo = $this->entityManager
->getDefinition($this->entityType);
} catch (\Exception $e) {
watchdog_exception('geofield_map', $e);
}
}
}
}
}
}
protected $usesFields = TRUE;
protected $usesRowPlugin = TRUE;
protected $usesRowClass = FALSE;
protected $usesGrouping = FALSE;
protected $defaultFieldLabels = TRUE;
protected function getAvailableDataSources() {
$fields_geo_data = [];
foreach ($this->displayHandler
->getHandlers('field') as $field_id => $handler) {
$label = $handler
->adminLabel() ?: $field_id;
$this->viewFields[$field_id] = $label;
if (is_a($handler, '\\Drupal\\views\\Plugin\\views\\field\\EntityField')) {
try {
$entity_type = $handler
->getEntityType();
} catch (\Exception $e) {
$entity_type = NULL;
}
$field_storage_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($entity_type);
$field_storage_definition = $field_storage_definitions[$handler->definition['field_name']];
$type = $field_storage_definition
->getType();
$definition = $this->fieldTypeManager
->getDefinition($type);
if (is_a($definition['class'], '\\Drupal\\geofield\\Plugin\\Field\\FieldType\\GeofieldItem', TRUE)) {
$fields_geo_data[$field_id] = $label;
}
}
}
return $fields_geo_data;
}
protected function getAvailableEntitySources() {
if ($base_entity_type = $this->view
->getBaseEntityType()) {
$label = $base_entity_type
->getLabel();
}
else {
$base_tables = array_keys($this->view
->getBaseTables());
$label = $base_tables[0] ?? $this
->t('Unknown');
}
$options = [
'__base_table' => new TranslatableMarkup('View Base Entity (@entity_type)', [
'@entity_type' => $label,
]),
];
$data = Views::viewsData();
foreach ($this->displayHandler
->getHandlers('relationship') as $relationship_id => $handler) {
if (($table = $data
->get($handler->definition['base'])) && !empty($table['table']['entity type'])) {
try {
$entity_type = $this->entityManager
->getDefinition($table['table']['entity type']);
} catch (\Exception $e) {
$entity_type = NULL;
}
$options[$relationship_id] = new TranslatableMarkup('@relationship (@entity_type)', [
'@relationship' => $handler
->adminLabel(),
'@entity_type' => $entity_type
->getLabel(),
]);
}
}
return $options;
}
protected function getEntitySourceEntityInfo($source) {
if (!empty($source) && $source != '__base_table') {
$handler = $this->displayHandler
->getHandler('relationship', $source);
$data = Views::viewsData();
if (($table = $data
->get($handler->definition['base'])) && !empty($table['table']['entity type'])) {
try {
return $this->entityManager
->getDefinition($table['table']['entity type']);
} catch (\Exception $e) {
watchdog_exception('geofield_map', $e);
}
}
}
return $this->view
->getBaseEntityType();
}
public function evenEmpty() {
return TRUE;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
if ($form_state
->get('entity_source')) {
$this->options['entity_source'] = $form_state
->get('entity_source');
$this->entityInfo = $this
->getEntitySourceEntityInfo($this->options['entity_source']);
$this->entityType = $this->entityInfo
->id();
$this->entitySource = $this->options['entity_source'];
}
parent::buildOptionsForm($form, $form_state);
$default_settings = self::defineOptions();
$form['#attached'] = [
'library' => [
'geofield_map/geofield_map_general',
'geofield_map/geofield_map_view_display_settings',
],
];
$fields_geo_data = $this
->getAvailableDataSources();
if (empty($fields_geo_data)) {
$form['error'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('Please add at least one Geofield to the View and come back here to set it as Data Source.'),
'#attributes' => [
'class' => [
'geofield-map-warning',
],
],
];
return;
}
$wrapper_id = 'geofield-map-views-style-options-form-wrapper';
$form['#prefix'] = '<div id="' . $wrapper_id . '">';
$form['#suffix'] = '</div>';
$form['data_source'] = [
'#type' => 'select',
'#title' => $this
->t('Data Source'),
'#description' => $this
->t('Which field contains geodata?'),
'#options' => $fields_geo_data,
'#default_value' => $this->options['data_source'],
'#required' => TRUE,
];
$entity_sources = $this
->getAvailableEntitySources();
if (count($entity_sources) == 1) {
$form['entity_source'] = [
'#type' => 'value',
'#value' => key($entity_sources),
];
}
else {
$form['entity_source'] = [
'#type' => 'select',
'#title' => new TranslatableMarkup('Entity Source'),
'#description' => new TranslatableMarkup('Select which Entity should be used as Geofield Mapping base Entity.<br><u>Leave as "View Base Entity" to rely on default Views behaviour, and don\'t specifically needed otherwise</u>.<br><b>Note:</b> This would affect Map Theming logics and options.'),
'#options' => $entity_sources,
'#default_value' => !empty($this->options['entity_source']) ? $this->options['entity_source'] : '__base_table',
'#ajax' => [
'wrapper' => $wrapper_id,
'callback' => [
static::class,
'optionsFormEntitySourceSubmitAjax',
],
'trigger_as' => [
'name' => 'entity_source_submit',
],
],
];
$form['entity_source_submit'] = [
'#type' => 'submit',
'#value' => new TranslatableMarkup('Update Entity Source'),
'#name' => 'entity_source_submit',
'#submit' => [
[
static::class,
'optionsFormEntitySourceSubmit',
],
],
'#validate' => [],
'#limit_validation_errors' => [
[
'style_options',
'entity_source',
],
],
'#attributes' => [
'class' => [
'js-hide',
],
],
'#ajax' => [
'wrapper' => $wrapper_id,
'callback' => [
static::class,
'optionsFormEntitySourceSubmitAjax',
],
],
];
}
$desc_options = array_merge([
'0' => $this
->t('- Any - No Infowindow'),
], $this->viewFields);
if ($this->entityType) {
$desc_options += [
'#rendered_entity' => $this
->t('- Rendered @entity entity -', [
'@entity' => $this->entityType,
]),
'#rendered_entity_ajax' => $this
->t('- Rendered @entity entity via Ajax (Quicker Map start / Slower Infowindow show) -', [
'@entity' => $this->entityType,
]),
'#rendered_view_fields' => $this
->t('# Rendered View Fields (with field label, format, classes, etc)'),
];
}
$this->options['infowindow_content_options'] = $desc_options;
$form += $this
->generateGmapSettingsForm($form, $form_state, $this->options, $default_settings);
$form['map_marker_and_infowindow']['infowindow_field']['#description'] .= $this
->t("<br>'Rendered @entity entity' option: quick infowindow, but slower map start in case of many map features (> 50 / 100) and large infowindows content.<br>'Rendered @entity entity via ajax' option: quicker map start in case of many map features and large infowindows content.", [
'@entity' => $this->entityType,
]);
$form['map_marker_and_infowindow']['theming'] = [
'#type' => 'fieldset',
'#title' => 'Map Theming Options',
'#weight' => isset($form['map_marker_and_infowindow']['icon_image_path']['#weight']) ? $form['map_marker_and_infowindow']['icon_image_path']['#weight'] - 5 : -15,
'#attributes' => [
'id' => 'map-theming-container',
],
];
$map_themers_definitions = $this->mapThemerManager
->getDefinitions();
uasort($map_themers_definitions, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
$map_themers_options = array_merge([
'none' => 'None',
], $this->mapThemerManager
->getMapThemersList('ViewStyle'));
$user_input = $form_state
->getUserInput();
$map_themer_id = isset($user_input['style_options']['map_marker_and_infowindow']['theming']['plugin_id']) ? $user_input['style_options']['map_marker_and_infowindow']['theming']['plugin_id'] : NULL;
$default_map_themer = isset($this->options['map_marker_and_infowindow']['theming']['plugin_id']) ? $this->options['map_marker_and_infowindow']['theming']['plugin_id'] : 'none';
$selected_map_themer = !empty($map_themer_id) ? $map_themer_id : $default_map_themer;
$plugin_id_warning = [
'deprecated' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('Deprecated.'),
'#attributes' => [
'class' => [
'geofield-map-warning',
],
],
],
'message' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->t('Note: This Map Themer is not compatible with D8 configuration management & sync (deprecated). Use the correspondent Image Select version instead (!).'),
'#attributes' => [
'class' => [
'geofield-map-warning',
],
],
],
];
$form['map_marker_and_infowindow']['theming']['plugin_id'] = [
'#type' => 'select',
'#title' => $this
->t('Map Theming'),
'#default_value' => $selected_map_themer,
'#options' => $map_themers_options,
'#ajax' => [
'callback' => [
get_class($this),
'mapThemingOptionsUpdate',
],
'effect' => 'fade',
],
];
if ($selected_map_themer != 'none') {
$form['map_marker_and_infowindow']['theming']['plugin_id_info'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $map_themers_definitions[$selected_map_themer]['description'],
];
if (!$map_themers_definitions[$selected_map_themer]['markerIconSelection']['configSyncCompatibility']) {
$form['map_marker_and_infowindow']['theming']['plugin_id_warning'] = $plugin_id_warning['message'];
}
try {
$map_themer_plugin = $this->mapThemerManager
->createInstance($selected_map_themer);
$form['map_marker_and_infowindow']['theming'][$map_themer_plugin->pluginId] = [
'#type' => 'container',
'id' => [
'#type' => 'value',
'#value' => $map_themer_plugin
->getPluginId(),
],
'values' => $map_themer_plugin
->buildMapThemerElement($this->options, $form, $form_state, $this),
'description' => [
'#type' => 'value',
'#value' => $map_themer_plugin
->getDescription(),
],
];
} catch (PluginException $e) {
$form['map_marker_and_infowindow']['theming']['plugin_id']['#default_value'] = $map_themers_options['none'];
}
}
$form['map_marker_and_infowindow']['theming']['plugins_descriptions'] = [
'#type' => 'container',
'table' => [
'#type' => 'table',
'#caption' => $this
->t('Available Map Themers & Descriptions:'),
'#attributes' => [
'class' => 'map-theming-options',
],
],
];
foreach ($map_themers_definitions as $k => $map_themer) {
$form['map_marker_and_infowindow']['theming']['plugins_descriptions']['table'][$k] = [
'td1' => [
'label' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $map_themers_options[$k],
],
'warning' => !$map_themers_definitions[$k]['markerIconSelection']['configSyncCompatibility'] ? $plugin_id_warning['deprecated'] : [],
],
'td2' => [
'#type' => 'container',
'description' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $map_themer['description'],
],
'warning' => !$map_themers_definitions[$k]['markerIconSelection']['configSyncCompatibility'] ? $plugin_id_warning['message'] : [],
],
'#attributes' => !$map_themers_definitions[$k]['markerIconSelection']['configSyncCompatibility'] ? [
'class' => [
'deprecated',
],
] : [],
];
}
if ('none' != $selected_map_themer) {
$form['map_marker_and_infowindow']['theming']['plugins_descriptions']['#attributes']['class'] = [
'hidden',
];
$form['map_marker_and_infowindow']['icon_image_path']['#prefix'] = '<div id="icon-image-path" class="visually-hidden">';
$form['map_marker_and_infowindow']['icon_image_path']['#suffix'] = '</div>';
}
}
public static function optionsFormEntitySourceSubmit(array $form, FormStateInterface $form_state) {
$parents = $form_state
->getTriggeringElement()['#parents'];
array_pop($parents);
array_push($parents, 'entity_source');
$form_state
->set('entity_source', $form_state
->getValue($parents));
$form_state
->setRebuild(TRUE);
}
public static function optionsFormEntitySourceSubmitAjax(array $form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
$array_parents = $triggering_element['#array_parents'];
array_pop($array_parents);
return NestedArray::getValue($form, $array_parents);
}
public static function mapThemingOptionsUpdate(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand('#map-marker-and-infowindow-wrapper', $form['options']['style_options']['map_marker_and_infowindow']));
return $response;
}
public function render() {
$map_settings = $this->options;
$element = [];
$this
->preProcessMapSettings($map_settings);
$js_settings = [
'mapid' => Html::getUniqueId("geofield_map_view_" . $this->view
->id() . '_' . $this->view->current_display),
'map_settings' => $map_settings,
'data' => [],
];
$data = [];
$build_for_bubbleable_metadata = [];
$geofield_name = $map_settings['data_source'];
if (empty($geofield_name) && $this->currentUser
->hasPermission('configure geofield_map')) {
$element = [
'#markup' => '<div class="geofield-map-warning">' . $this
->t("The Geofield field hasn't not been correctly set for this View. <br>Add at least one Geofield to the View and set it as Data Source in the Geofield Google Map View Display Settings.") . "</div>",
'#attached' => [
'library' => [
'geofield_map/geofield_map_general',
],
],
];
}
if (!empty($geofield_name) && (!empty($this->view->result) || $map_settings['map_empty']['empty_behaviour'] == '1')) {
$this
->renderFields($this->view->result);
foreach ($this->view->result as $id => $result) {
$geofield_value = (array) $this
->getFieldValue($id, $geofield_name);
if (!empty($geofield_value)) {
if (empty($this->options['entity_source']) || $this->options['entity_source'] == '__base_table') {
if (!empty($result->_entity)) {
$entity = $result->_entity;
}
elseif (isset($result->_object)) {
$entity_adapter = $result->_object;
if ($entity_adapter instanceof EntityAdapter) {
$entity = $entity_adapter
->getValue();
}
}
}
else {
if (!empty($result->_relationship_entities[$this->options['entity_source']])) {
$entity = $result->_relationship_entities[$this->options['entity_source']];
}
}
$description = [];
if (isset($entity)) {
if (!isset($js_settings['map_settings']['geofield_cardinality'])) {
try {
$geofield_entity = $entity
->get($geofield_name);
$js_settings['map_settings']['geofield_cardinality'] = $geofield_entity
->getFieldDefinition()
->getFieldStorageDefinition()
->getCardinality();
} catch (\Exception $e) {
$js_settings['map_settings']['geofield_cardinality'] = -1;
}
}
$entity_type = $entity
->getEntityTypeId();
$entity_type_langcode_attribute = $entity_type . '_field_data_langcode';
$view = $this->view;
$rendering_language = $view->display_handler
->getOption('rendering_language');
$dynamic_renderers = [
'***LANGUAGE_entity_translation***' => 'TranslationLanguageRenderer',
'***LANGUAGE_entity_default***' => 'DefaultLanguageRenderer',
];
if (isset($dynamic_renderers[$rendering_language])) {
$langcode = isset($result->{$entity_type_langcode_attribute}) ? $result->{$entity_type_langcode_attribute} : $entity
->language()
->getId();
}
else {
if (strpos($rendering_language, '***LANGUAGE_') !== FALSE) {
$langcode = PluginBase::queryLanguageSubstitutions()[$rendering_language];
}
else {
$langcode = $rendering_language;
}
}
$description_field = isset($map_settings['map_marker_and_infowindow']['infowindow_field']) ? $map_settings['map_marker_and_infowindow']['infowindow_field'] : NULL;
if (isset($description_field)) {
$description_field_entity = $entity->{$description_field};
$default_view_mode = !empty($this->options['view_mode']) ? $this->options['view_mode'] : (!empty($this->options['map_marker_and_infowindow']['view_mode']) ? $this->options['map_marker_and_infowindow']['view_mode'] : NULL);
switch ($description_field) {
case '#rendered_entity':
$build = $this->entityManager
->getViewBuilder($entity
->getEntityTypeId())
->view($entity, $default_view_mode, $langcode);
$render_context = new RenderContext();
$description[] = $this->renderer
->executeInRenderContext($render_context, function () use (&$build) {
return $this->renderer
->render($build, TRUE);
});
if (!$render_context
->isEmpty()) {
$render_context
->update($build_for_bubbleable_metadata);
}
break;
case '#rendered_view_fields':
$renderedRow = [
$this->view->rowPlugin
->render($result),
];
$description[] = $this->renderer
->renderPlain($renderedRow);
break;
case '#rendered_entity_ajax':
$parameters = [
'entity_type' => $entity_type,
'entity' => $entity
->id(),
'view_mode' => $default_view_mode,
'langcode' => $langcode,
];
$url = Url::fromRoute('geofield_map.ajax_popup', $parameters);
$description[] = sprintf('<div class="geofield-google-map-ajax-popup" data-geofield-google-map-ajax-popup="%s" %s></div>', $url
->toString(), GeofieldMapAjaxPopupController::getPopupIdentifierAttribute($entity_type, $entity
->id(), $default_view_mode, $langcode));
$js_settings['map_settings']['ajaxPoup'] = TRUE;
break;
default:
if (isset($description_field_entity)) {
$description_field_cardinality = $description_field_entity
->getFieldDefinition()
->getFieldStorageDefinition()
->getCardinality();
foreach ($description_field_entity
->getValue() as $value) {
if ($description_field_cardinality == 1 || $map_settings['map_marker_and_infowindow']['multivalue_split'] == FALSE) {
$description[] = $this->rendered_fields[$id][$description_field];
break;
}
$description[] = isset($value['value']) ? $value['value'] : NULL;
}
}
elseif (isset($this->rendered_fields[$id][$description_field])) {
$description[] = $this->rendered_fields[$id][$description_field];
}
}
}
$view_data = [];
foreach ($this->rendered_fields[$id] as $field_name => $rendered_field) {
if (!empty($rendered_field) && !$this->view->field[$field_name]->options['exclude']) {
$view_data[$field_name] = $rendered_field
->__toString();
}
}
$tooltip_field = isset($map_settings['map_marker_and_infowindow']['tooltip_field']) ? $map_settings['map_marker_and_infowindow']['tooltip_field'] : NULL;
$tooltip = isset($entity) && !empty($tooltip_field) ? trim(html_entity_decode(strip_tags($this->rendered_fields[$id][$tooltip_field]), ENT_QUOTES)) : NULL;
$tokens = [];
foreach ($this->rendered_fields[$result->index] as $field_name => $field_value) {
$tokens[$field_name] = $field_value;
}
$geojson_data = $this
->getGeoJsonData($geofield_value, $entity
->id(), $description, $tooltip, $view_data);
$theming = NULL;
if (isset($map_settings['map_marker_and_infowindow']['theming']) && $map_settings['map_marker_and_infowindow']['theming']['plugin_id'] != 'none') {
$theming = $map_settings['map_marker_and_infowindow']['theming'];
try {
$map_themer = $this->mapThemerManager
->createInstance($theming['plugin_id'], [
'geofieldMapView' => $this,
]);
$map_theming = $theming[$map_themer
->getPluginId()]['values'];
foreach ($geojson_data as $k => $datum) {
if ($datum['geometry']->type === 'Point') {
$geojson_data[$k]['properties']['icon'] = $map_themer
->getIcon($datum, $this, $entity, $map_theming);
$geojson_data[$k]['properties']['theming'] = TRUE;
}
}
} catch (PluginException $e) {
watchdog_exception('geofield_map', $e);
}
}
elseif ($map_settings['map_marker_and_infowindow']['icon_image_mode'] == 'icon_file' && strlen($map_settings['map_marker_and_infowindow']['icon_image_path']) > 0) {
foreach ($geojson_data as $k => $datum) {
if ($datum['geometry']->type === 'Point') {
$geojson_data[$k]['properties']['icon'] = $this
->viewsTokenReplace($this->options['map_marker_and_infowindow']['icon_image_path'], $tokens);
}
}
}
foreach ($geojson_data as $k => $datum) {
if ($datum['geometry']->type !== 'Point') {
$geojson_data[$k]['properties']['path_options'] = str_replace([
"\n",
"\r",
], "", $this
->viewsTokenReplace($this->options['map_geometries_options'], $tokens));
}
}
$data = array_merge($data, $geojson_data);
}
}
}
$js_settings['data'] = [
'type' => 'FeatureCollection',
'features' => $data,
];
$this->moduleHandler
->alter('geofield_map_googlemap_view_style', $js_settings, $this);
$element = geofield_map_googlemap_render($js_settings);
if (isset($js_settings['map_settings']['ajaxPoup']) && $js_settings['map_settings']['ajaxPoup'] == TRUE) {
$build_for_bubbleable_metadata['#attached']['library'][] = 'core/drupal.ajax';
}
BubbleableMetadata::createFromRenderArray($element)
->merge(BubbleableMetadata::createFromRenderArray($build_for_bubbleable_metadata))
->applyTo($element);
}
return $element;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['data_source'] = [
'default' => '',
];
$options['entity_source'] = [
'default' => '__base_table',
];
$geofield_google_map_default_settings = [];
foreach (self::getDefaultSettings() as $k => $setting) {
$geofield_google_map_default_settings[$k] = [
'default' => $setting,
];
if ($k == 'map_marker_and_infowindow') {
$geofield_google_map_default_settings[$k]['default']['theming']['plugin_id'] = 'none';
foreach ($this->mapThemersList as $id => $map_themer) {
$geofield_google_map_default_settings[$k]['default']['theming'][$id]['values'] = [];
}
}
}
return $options + $geofield_google_map_default_settings;
}
public function getViewFields() {
return $this->viewFields;
}
public function getViewEntityType() {
return $this->entityType;
}
public function getEntityFieldManager() {
return $this->entityFieldManager;
}
public function getViewFilteredBundles() {
$bundles = [];
$views_filters = $this->view->display_handler
->getOption('filters');
if (!empty($views_filters['type'])) {
$bundles = array_keys($views_filters['type']['value']);
}
return $bundles;
}
}