View source
<?php
namespace Drupal\ip_geoloc\Services;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Renderer;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\field\Entity\FieldConfig;
define('IP_GEOLOC_MAP_AUTOBOX_IGNORE_VISITOR', -1);
define('IP_GEOLOC_MAP_CENTER_FIXED', 0);
define('IP_GEOLOC_MAP_CENTER_ON_FIRST_LOCATION', 1);
define('IP_GEOLOC_MAP_CENTER_ON_VISITOR', 2);
define('IP_GEOLOC_MAP_CENTER_OF_LOCATIONS', 3);
define('IP_GEOLOC_MAP_CENTER_OF_LOCATIONS_WEIGHTED', 4);
define('IP_GEOLOC_MAP_CENTER_ON_LOCATION_FROM_ARGUMENT', 5);
class IpGeoLocViewsPluginStyle {
protected $ipGeolocGlobal;
protected $stringTranslation;
protected $messenger;
protected $config;
protected $renderer;
public function __construct(TranslationInterface $stringTranslation, MessengerInterface $messenger, IpGeoLocGlobal $ipGeolocGlobal, ConfigFactoryInterface $config_factory, Renderer $renderer) {
$this->stringTranslation = $stringTranslation;
$this->messenger = $messenger;
$this->ipGeolocGlobal = $ipGeolocGlobal;
$this->config = $config_factory
->getEditable('ip_geoloc.settings');
$this->renderer = $renderer;
}
public function pluginStyleBulkOfForm($views_plugin_style, &$form, FormStateInterface &$form_state) {
if (!empty($form['grouping']) && count($form['grouping']) > 1) {
$form['grouping'] = array_slice($form['grouping'], 0, 1);
}
$fields = ip_geoloc_get_display_fields($views_plugin_style->displayHandler, TRUE);
$field_options = $fields;
$field_options['---'] = '[' . t('Type a field name') . ']';
$selected_option = $views_plugin_style->options['ip_geoloc_views_plugin_latitude'];
if (!isset($field_options[$selected_option])) {
$selected_option = '---';
}
$form['ip_geoloc_views_plugin_latitude'] = [
'#title' => t('Name of latitude field in Views query'),
'#type' => 'select',
'#options' => $field_options,
'#default_value' => $selected_option,
'#weight' => 10,
];
$form['ip_geoloc_views_plugin_latitude_text'] = [
'#type' => 'textfield',
'#default_value' => $views_plugin_style->options['ip_geoloc_views_plugin_latitude'],
'#states' => [
'visible' => [
':input[name="style_options[ip_geoloc_views_plugin_latitude]"]' => [
'value' => '---',
],
],
],
'#weight' => 11,
];
$form['ip_geoloc_views_plugin_latitude_desc'] = [
'#type' => 'item',
'#markup' => t('Select the field holding the latitude from the dropdown above.<br/><strong>Special cases</strong> If the View has relationships and lat/lon are accessed via the relationships, select <em>[Type field name]</em> and enter the field name in the additional text box presented. If you use a view based on the <strong>Location</strong> module, enter <strong>location_latitude</strong>. If the latitude is stored in a <strong>field</strong>, then enter the field\'s machine name. For instance, when using the <strong>Geofield</strong>, <strong>Geolocation field</strong> or <strong>Get Locations</strong> modules enter <strong>field_<em>yourfieldname</em></strong>. If necessary, check the <a target="_field_names" href="!url">list of all field names</a>. <br/>For IP Geolocation Views & Maps <em>visitor</em> data, enter <strong>ip_geoloc_latitude</strong>.', [
'!url' => 'admin/reports/fields',
]),
'#weight' => 12,
];
$selected_option = $views_plugin_style->options['ip_geoloc_views_plugin_longitude'];
if (!isset($field_options[$selected_option])) {
$selected_option = '---';
}
$form['ip_geoloc_views_plugin_longitude'] = [
'#title' => t('Name of longitude field in Views query'),
'#type' => 'select',
'#options' => $field_options,
'#default_value' => $selected_option,
'#weight' => 15,
];
$form['ip_geoloc_views_plugin_longitude_text'] = [
'#type' => 'textfield',
'#default_value' => $views_plugin_style->options['ip_geoloc_views_plugin_longitude'],
'#states' => [
'visible' => [
':input[name="style_options[ip_geoloc_views_plugin_longitude]"]' => [
'value' => '---',
],
],
],
'#weight' => 16,
];
$form['ip_geoloc_views_plugin_longitude_desc'] = [
'#type' => 'item',
'#markup' => t('See comments above. When using the <strong>Geofield</strong>, <strong>Geolocation field</strong> or <strong>Get Locations</strong> modules select <em><none></em>.'),
'#weight' => 17,
];
$differentiator = $form_state
->getValue('differentiator') ? $form_state
->getValue('differentiator') : $views_plugin_style->options['differentiator']['differentiator_field'];
$user_input = $form_state
->getUserInput();
if (isset($user_input["color_table"])) {
$form_state
->set('num_associations', count($user_input["color_table"]));
}
if (isset($user_input["_triggering_element_name"]) && $user_input["_triggering_element_name"] == "style_options[differentiator][differentiator_field]") {
$differentiator = $user_input['style_options']['differentiator']['differentiator_field'];
}
$form_state
->set('differentiator', $differentiator);
$form_state
->disableCache();
$is_openlayers = TRUE;
$form_state
->set('renderer', 'openlayers');
$form['default_marker_color'] = [
'#title' => t('Default location marker'),
'#type' => 'select',
'#default_value' => $views_plugin_style->options['default_marker_color'],
'#options' => $is_openlayers ? $this->ipGeolocGlobal
->openlayersMarkerLayers() : $this->ipGeolocGlobal
->markerColors(),
'#description' => t('Select an image to use for all location markers whose images are not overridden by the <strong>location differentiator</strong> below.'),
'#attributes' => [
'class' => $is_openlayers ? [
'marker-color-ol',
] : [
'marker-color',
],
],
'#weight' => 20,
];
if (!$is_openlayers) {
$path = drupal_get_path('module', 'ip_geoloc');
$css_file = strpos($this->config
->get('ip_geoloc_marker_directory'), 'amarkers') ? 'ip_geoloc_admin_a.css' : 'ip_geoloc_admin.css';
$form['default_marker_color']['#attached']['css'] = [
"{$path}/css/{$css_file}",
];
}
$desc = t('You may designate one field from your view as a <strong>location differentiator</strong>. Locations that match the same corresponding differentiator value will have the same marker image on the map. Examples of location differentiators are content type and taxonomy term. You can enter numeric and alphabetic <em>ranges</em> too, e.g. price ranges, like 100--125.<br/><strong>Leaflet</strong> only: you may input a special character, eg. from <a target="_fsymbols" href="!url1">fsymbols</a> or <a target="_font_awesome" href="!url2">Font Awesome</a>, to be superimposed as a font-icon on the marker image. For <em>fsymbols</em> copy and paste the desired font-icon into the Special Char field. For <em>Font Awesome</em> icons leave the Special Char field empty. Instead type the <em>Font Awesome</em> icon name in the Special Char CSS Class field, optionally appending a color class. Example: <strong>icon-beer icon-light</strong>. <br/>All this works best with the markers from the <em>/amarkers</em> directory, configurable <a target="_ip_geoloc_config" href="!url3">here</a>.', [
'!url1' => 'http://fsymbols.com',
'!url2' => 'http://fortawesome.github.io/Font-Awesome/cheatsheet',
'!url3' => 'admin/config/system/ip_geoloc',
]);
$form['differentiator'] = [
'#type' => 'fieldset',
'#title' => t('Location differentiator and associated markers'),
'#description' => $desc,
'#prefix' => '<div id="differentiator-wrapper">',
'#suffix' => '</div>',
'#weight' => 30,
];
if (empty($differentiator)) {
$description = t('Optionally select a location differentiator.');
}
else {
$field = FieldStorageConfig::loadByName('node', $differentiator);
$description = t('Below associate %differentiator values with marker images.', [
'%differentiator' => $field
->getLabel(),
]) . '<br/>';
if (!$field || $field
->getType() == 'text') {
$description .= t('You may enter a range of values by separating "from" and "to" by a <strong>double hyphen</strong>.<br/>Example: <strong>A--ZZ</strong><br/>You may omit "from" or "to" to create open-ended ranges.');
}
elseif ($field
->getType() == 'number') {
$description .= t('You may enter a numeric range of by separating "from" and "to" by a <strong>double hyphen</strong>.<br/>Example: <strong>2.5--7.95</strong><br/>You may omit "from" or "to" to create open-ended ranges.');
}
}
$form['differentiator']['differentiator_field'] = [
'#title' => t('Location differentiator'),
'#type' => 'select',
'#default_value' => $differentiator,
'#options' => $fields,
'#description' => $description,
'#ajax' => [
'callback' => [
$this,
'pluginStyleRefreshColorTableJs',
],
'wrapper' => 'differentiator-wrapper',
'prevent' => 'submit',
],
];
if (!empty($differentiator)) {
$field = FieldStorageConfig::loadByName('node', $differentiator);
$differentiator_label = $field
->getLabel();
$header = [
$differentiator_label,
t('Associated marker image'),
t('Special char'),
t('Special char CSS class'),
];
$form['differentiator']['color_table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => [],
'#tree' => TRUE,
'#attributes' => [
'id' => 'differentiator-color-table',
],
];
if (isset($user_input["_triggering_element_value"]) && !empty($user_input["_triggering_element_value"])) {
if ($user_input["_triggering_element_value"] == t("Add another association") || $user_input["_triggering_element_value"] == t("Add an association")) {
$this
->pluginStyleAddAssociationSubmit($form, $form_state);
$this
->pluginStyleDifferentiatorColorAssociationsSubmit($form, $form_state);
}
if ($user_input["_triggering_element_value"] == t("Remove bottom association")) {
$this
->pluginStyleRemoveAssociationSubmit($form, $form_state);
$this
->pluginStyleDifferentiatorColorAssociationsSubmit($form, $form_state);
}
}
if ($user_input["op"] == t("Apply")) {
$this
->pluginStyleDifferentiatorColorAssociationsSubmit($form, $form_state);
}
$this
->pluginStyleDifferentiatorColorTableForm($form, $form_state);
$form['differentiator']['add-another'] = [
'#type' => 'submit',
'#value' => empty($form_state
->get('num_associations')) ? t('Add an association') : t('Add another association'),
'#weight' => 1,
'#ajax' => [
'callback' => [
$this,
'pluginStyleRefreshColorTableJs',
],
'wrapper' => 'differentiator-wrapper',
'effect' => 'fade',
'speed' => 'fast',
'prevent' => 'submit',
],
];
if (!empty($form_state
->get('num_associations'))) {
$form['differentiator']['remove'] = [
'#type' => 'submit',
'#value' => t('Remove bottom association'),
'#weight' => 2,
'#ajax' => [
'callback' => [
$this,
'pluginStyleRefreshColorTableJs',
],
'wrapper' => 'differentiator-wrapper',
'effect' => 'none',
'speed' => 'fast',
'prevent' => 'submit',
],
];
}
}
if ($is_openlayers) {
$desc = t('*) If you want a fixed center, visit the "Center & Bounds" vertical tab on the <a target="_ol_map_edit" href="@url">map edit page</a>. This is also where you set the initial zoom level.', [
'@url' => 'admin/structure/openlayers/maps/' . $views_plugin_style->options['map'] . '/edit',
]);
}
else {
$desc = t('*) If you choose the first option you may center the map via the special <strong>Map options</strong> <em>"centerLat"</em> and <em>"centerLng"</em> for example:<br/>%center_example', [
'%center_example' => '{ "mapTypeId":"terrain", "centerLat":-37.8, "centerLng":145 }',
]);
}
$form['center_option'] = [
'#title' => t('Map centering options'),
'#type' => 'radios',
'#default_value' => $views_plugin_style->options['center_option'],
'#options' => [
IP_GEOLOC_MAP_CENTER_FIXED => t('Fixed center; see note below *)'),
IP_GEOLOC_MAP_CENTER_ON_FIRST_LOCATION => t('Use the first location returned by the view as the center of the map.'),
IP_GEOLOC_MAP_CENTER_ON_VISITOR => t("Center the map on the visitor's current location."),
IP_GEOLOC_MAP_CENTER_OF_LOCATIONS => t('Use the center of the rectangle whose sides are defined by the left-most, right-most, top and bottom locations (this option is insensitive to location clusters).'),
IP_GEOLOC_MAP_CENTER_OF_LOCATIONS_WEIGHTED => t('Use the center of gravity based on all locations (this option is sensitive to location clusters)'),
],
'#description' => $desc,
'#weight' => 40,
];
$form['empty_map_center'] = [
'#title' => t('No locations behaviour'),
'#type' => 'textfield',
'#size' => 30,
'#default_value' => $views_plugin_style->options['empty_map_center'],
'#description' => t('a) Show empty map: enter <em>latitude, longitude</em> of the map center.<br/>b) Show empty map centered on visitor location: enter the word %visitor.<br/>c) Show nothing: if this field is left blank, the map canvas will be suppressed when there are no locations to show. If you wish, you may use the <strong>No results behavior</strong> panel in the Advanced section of the Views UI to show an alternative text or content.', [
'%visitor' => t('visitor'),
]),
'#weight' => 110,
];
return $form;
}
public function pluginStyleBulkOfFormValidate(&$form, FormStateInterface &$form_state) {
$style_options = $form_state
->getValue('style_options');
if ($style_options['ip_geoloc_views_plugin_latitude'] == '---') {
$style_options['ip_geoloc_views_plugin_latitude'] = $style_options['ip_geoloc_views_plugin_latitude_text'];
}
if ($style_options['ip_geoloc_views_plugin_longitude'] == '---') {
$style_options['ip_geoloc_views_plugin_longitude'] = $style_options['ip_geoloc_views_plugin_longitude_text'];
}
}
public function pluginStyleAddAssociationSubmit($form, FormStateInterface &$form_state) {
$form_state
->set('num_associations', $form_state
->get('num_associations') + 1);
$form_state
->setRebuild();
}
public function pluginStyleRemoveAssociationSubmit($form, FormStateInterface &$form_state) {
$form_state
->set('num_associations', $form_state
->get('num_associations') - 1);
$form_state
->setRebuild();
}
public function pluginStyleRefreshColorTableJs(&$form, &$form_state) {
return $form['options']['style_options']['differentiator'];
}
public function pluginStyleDifferentiatorColorAssociationsSubmit(&$form, &$form_state) {
if (empty($form_state
->get('view')) || empty($form_state
->get('display_id')) || empty($form_state
->get('differentiator'))) {
return;
}
$view_id = $form_state
->get('view')
->get('storage')
->get('id');
$display_id = $form_state
->get('display_id');
$differentiator = $form_state
->get('differentiator');
$differentiator_color_associations = $this->config
->get('ip_geoloc_' . $view_id . '_color_mappings', []);
unset($differentiator_color_associations[$display_id][$differentiator]);
$user_input = $form_state
->getUserInput();
if (!empty($user_input['color_table'])) {
$i = 0;
foreach ($user_input['color_table'] as $association) {
if (is_array($association[$differentiator])) {
foreach ($association[$differentiator][LANGUAGE_NONE] as $values) {
$differentiator_value = [];
if (is_array($values)) {
foreach ($values as $name => $value) {
if (isset($value) && strpos($value, '|') === FALSE) {
$differentiator_value[$name] = $value;
}
}
}
else {
$differentiator_value[] = $values;
}
if (!empty($differentiator_value)) {
$differentiator_color_associations[$display_id][$differentiator][$i]['differentiator_value'] = $differentiator_value;
$differentiator_color_associations[$display_id][$differentiator][$i]['color'] = $association['color'];
$differentiator_color_associations[$display_id][$differentiator][$i]['special char'] = $association['special char'];
$differentiator_color_associations[$display_id][$differentiator][$i]['special char class'] = $association['special char class'];
$i++;
}
}
}
else {
$differentiator_value = trim($association[$differentiator]);
if (!empty($differentiator_value)) {
$differentiator_color_associations[$display_id][$differentiator][$i]['differentiator_value'] = [
$differentiator_value,
];
$differentiator_color_associations[$display_id][$differentiator][$i]['color'] = $association['color'];
$differentiator_color_associations[$display_id][$differentiator][$i]['special char'] = $association['special char'];
$differentiator_color_associations[$display_id][$differentiator][$i]['special char class'] = $association['special char class'];
$i++;
}
}
}
}
$this->config
->set('ip_geoloc_' . $view_id . '_color_mappings', $differentiator_color_associations)
->save();
}
public function pluginStyleDifferentiatorColorTableForm(&$form, FormStateInterface &$form_state) {
$is_openlayers = $form_state
->get('renderer') == 'openlayers';
$view_id = $form_state
->get('view')
->get('storage')
->get('id');
$display_id = $form_state
->get('display_id');
$differentiator = $form_state
->get('differentiator');
$differentiator_color_associations = $this->config
->get('ip_geoloc_' . $view_id . '_color_mappings', []);
if (empty($differentiator_color_associations[$display_id][$differentiator])) {
$differentiator_color_associations[$display_id] = [
$differentiator => [],
];
}
$row = 0;
foreach ($differentiator_color_associations[$display_id][$differentiator] as $association) {
if (!is_array($association)) {
break;
}
if ($form_state
->has('num_associations') && $row >= $form_state
->get('num_associations')) {
break;
}
$form['differentiator']['color_table'][$row] = $this
->pluginStyleDiffColorTableRowForm($is_openlayers, $row, $differentiator, $association);
$row++;
}
if (!$form_state
->has('num_associations')) {
if (empty($differentiator_color_associations[$display_id][$differentiator])) {
$form_state
->set('num_associations', count($differentiator_color_associations[$display_id]));
}
else {
$form_state
->set('num_associations', count($differentiator_color_associations[$display_id][$differentiator]));
}
}
while ($row < $form_state
->get('num_associations')) {
$form['differentiator']['color_table'][$row] = $this
->pluginStyleDiffColorTableRowForm($is_openlayers, $row, $differentiator);
$row++;
}
}
public function pluginStyleDiffColorTableRowForm($is_openlayers, $row, $differentiator, $association = NULL) {
$differentiator_value = empty($association) ? NULL : $association['differentiator_value'];
$attr_class = $is_openlayers ? [
'differentiator ol',
] : [
'differentiator',
];
if (strpos($differentiator, 'field_') === 0) {
$field = FieldStorageConfig::loadByName('node', $differentiator);
$widget = $field
->getSettings();
if (strpos($widget['target_type'], 'taxonomy') !== FALSE) {
$vid = '';
$bundle = 'page';
$vocs = [];
$field_confing = FieldConfig::loadByName('node', $bundle, $differentiator);
$vocabulary_name = reset($field_confing
->getSettings()['handler_settings']['target_bundles']);
$vocabularies = Vocabulary::loadMultiple();
foreach ($vocabularies as $vid => $vocabulary) {
if ($vid == $vocabulary_name) {
break;
}
}
$options = [];
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadTree($vid);
foreach ($terms as $term) {
$options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
}
$form[$differentiator] = [
'#type' => empty($options) ? 'textfield' : 'select',
'#options' => $options,
'#default_value' => $differentiator_value,
'#attributes' => [
'class' => $attr_class,
],
'#title' => $differentiator,
];
}
else {
$instance['label'] = '';
$instance['required'] = FALSE;
if (isset($instance['widget']['settings']['size']) && $instance['widget']['settings']['size'] < 15) {
$instance['widget']['settings']['size'] = 15;
}
$items[0] = empty($differentiator_value) ? [] : is_array($differentiator_value) ? $differentiator_value : [
$differentiator_value,
];
if (isset($instance['default_value'])) {
$items[0] += is_array($instance['default_value']) && !empty($instance['default_value']) ? reset($instance['default_value']) : $instance['default_value'];
}
$form['#parents'] = [];
$form_state = [];
$form = field_default_form($instance['entity_type'], NULL, $field, $instance, LANGUAGE_NONE, $items, $form, $form_state);
if ($field['module'] == 'number' || $field['module'] == 'text') {
$form[$differentiator][LANGUAGE_NONE][0][key($field['columns'])]['#element_validate'] = [
'ip_geoloc_range_widget_validate',
];
}
$form[$differentiator]['#attributes']['class'][] = $attr_class;
}
}
else {
$form[$differentiator] = [
'#type' => 'textfield',
'#size' => 40,
'#default_value' => $differentiator_value,
'#element_validate' => [
'ip_geoloc_range_widget_validate',
],
'#attributes' => [
'class' => $attr_class,
],
'#title' => $differentiator,
];
}
$form['color'] = [
'#type' => 'select',
'#default_value' => empty($association) ? NULL : $association['color'],
'#options' => $is_openlayers ? ip_geoloc_openlayers_marker_layers() : ip_geoloc_marker_colors(),
'#attributes' => [
'class' => $is_openlayers ? [
'marker-color-ol',
] : [
'marker-color',
],
],
];
$form['special char'] = [
'#type' => 'textfield',
'#size' => 8,
'#default_value' => empty($association) ? NULL : $association['special char'],
];
$form['special char class'] = [
'#type' => 'textfield',
'#size' => 25,
'#default_value' => empty($association) ? NULL : $association['special char class'],
];
$form[$differentiator]['#parents'] = [
'color_table',
$row,
$differentiator,
];
$form['color']['#parents'] = [
'color_table',
$row,
'color',
];
$form['special char']['#parents'] = [
'color_table',
$row,
'special char',
];
$form['special char class']['#parents'] = [
'color_table',
$row,
'special char class',
];
return $form;
}
public function pluginStyleExtractLocations($views_plugin_style) {
$latitude = trim($views_plugin_style->options['ip_geoloc_views_plugin_latitude']);
$longitude = trim($views_plugin_style->options['ip_geoloc_views_plugin_longitude']);
$view =& $views_plugin_style->view;
$view_id = $view->storage
->getOriginalId();
$display_id = $view
->getDisplay()->display['id'];
$differentiator = FALSE;
if (!empty($views_plugin_style->options['differentiator']['differentiator_field'])) {
$differentiator = $views_plugin_style->options['differentiator']['differentiator_field'];
$differentiator_color_associations = $this->config
->get('ip_geoloc_' . $view_id . '_color_mappings') ? $this->config
->get('ip_geoloc_' . $view_id . '_color_mappings') : [];
if (empty($differentiator_color_associations[$display_id])) {
$differentiator_color_associations[$display_id] = [
$differentiator => [],
];
}
}
$tooltip_field = FALSE;
if (isset($views_plugin_style->options['tooltips']['marker_tooltip'])) {
$marker_tooltip = $views_plugin_style->options['tooltips']['marker_tooltip'];
if (isset($view->field[$marker_tooltip])) {
$tooltip_field = $view->field[$marker_tooltip];
}
}
$tag_field = FALSE;
if (isset($views_plugin_style->options['tags']['marker_tag'])) {
$marker_tag = $views_plugin_style->options['tags']['marker_tag'];
if (isset($view->field[$marker_tag])) {
$tag_field = $view->field[$marker_tag];
}
}
$locations = [];
$error_count = $first_error = 0;
$loc_field_name = $loc_field_alias = NULL;
if (isset($view->field[$latitude])) {
$loc_field_name = $view->field[$latitude]->definition['field_name'];
if ($view->field[$latitude]->field_alias == 'unknown') {
$loc_field_alias = $loc_field_name;
}
else {
$loc_field_alias = $view->field[$latitude]->field_alias;
}
}
$sets = $views_plugin_style
->renderGrouping($views_plugin_style->view->result, $views_plugin_style->options['grouping'], TRUE);
if (!empty($views_plugin_style->options['grouping'])) {
$group_field = $views_plugin_style->options['grouping'][0]['field'];
}
$rendered_fields = $views_plugin_style
->getRenderedFields();
foreach ($sets as $set) {
if (!empty($set['group'])) {
$view->row_index = $i = key($set['rows']);
$row = reset($set['rows']);
$base = $this
->pluginStyleGetBase1($row, $loc_field_name, $loc_field_alias);
if ($location = $this
->pluginStyleExtractLatLng($row, $latitude, $longitude, $base)) {
if (!empty($rendered_fields[$i])) {
$location->balloon_text = $rendered_fields[$i][$group_field];
foreach ($set['rows'] as $i => $row) {
$rendered_field = $rendered_fields[$i];
unset($rendered_field[$group_field]);
$location->balloon_text .= '<br />' . implode('<br/>', $rendered_field);
}
}
if (!empty($differentiator_color_associations[$display_id][$differentiator])) {
$this
->pluginStyleSetMarkerColor($location, $row, $differentiator, $differentiator_color_associations[$display_id][$differentiator]);
}
$this
->pluginStyleDecorate($location, $row, $tooltip_field, $tag_field);
$locations[] = $location;
}
elseif ($error_count++ == 0) {
$first_error = $i;
}
}
else {
foreach ($view->result as $i => $row) {
$view->row_index = $i;
$base = $this
->pluginStyleGetBase1($row, $loc_field_name, $loc_field_alias);
if ($location = $this
->pluginStyleExtractLatLng($row, $latitude, $longitude, $base)) {
if (!empty($rendered_fields[$i])) {
$location->balloon_text = implode('<br/>', $rendered_fields[$i]);
}
if (!empty($differentiator_color_associations[$display_id][$differentiator])) {
$this
->pluginStyleSetMarkerColor($location, $row, $differentiator, $differentiator_color_associations[$display_id][$differentiator]);
}
$this
->pluginStyleDecorate($location, $row, $tooltip_field, $tag_field);
$locations[] = $location;
}
elseif ($error_count++ == 0) {
$first_error = $i;
}
}
}
}
$user = \Drupal::currentUser();
if ($error_count > 0 && $user
->id() == 1) {
$row_count = count($view->result);
$title = empty($views_plugin_style->display->display_options['title']) ? $view
->getTitle() . ' (' . $views_plugin_style->display->display_title . ')' : $views_plugin_style->display->display_options['title'];
$t_args = [
'%view_title' => $title,
'@total' => $row_count,
'@error_count' => $error_count,
'@first' => $first_error + 1,
'%latitude' => $latitude,
];
$msg = $error_count >= $row_count ? t('None of the @total result rows in view %view_title had their %latitude lat/lng set. Therefore the map could not be displayed. Is %latitude the correct field name?', $t_args) : t('Out of a total of @total result rows in view %view_title, @error_count rows did not have their %latitude lat/lng set and therefore could not be shown on the map. The first row that could not be located was row #@first. You can improve execution efficiency by using the Views UI to apply an "is not empty" filter criterion to %latitude.', $t_args);
$row_contents = t('First error row:') . '<br/>';
foreach ((array) $view->result[$first_error] as $field_name => $value) {
}
$this->messenger
->addMessage($msg, 'warning');
$this->messenger
->addMessage($row_contents, 'warning');
$this->messenger
->addMessage(t('The above messages are shown only to user 1.'), 'warning');
}
\Drupal::moduleHandler()
->alter('ip_geoloc_marker_locations', $locations, $view);
return $locations;
}
public function pluginStyleGetBase1($row, $location_field_name, $location_field_alias) {
$base = NULL;
if (isset($row->_entity)) {
$entity = $row->_entity;
$location_field_value = $entity
->get($location_field_name)
->getValue();
if ($location_field_value) {
$base = is_array($location_field_value) ? reset($location_field_value) : $location_field_value;
}
}
return $base;
}
public function pluginStyleGetBase2($row, $field_name, $delta) {
if (!isset($row) || !isset($field_name) || !isset($delta)) {
return NULL;
}
$field = $row
->get($field_name);
$field_value = $field
->getValue();
if (!$row
->get($field_name)
->isEmpty() && is_array($field_value)) {
if (!empty($field_value[$delta])) {
return $field_value[$delta];
}
$lang = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
if ($translated_entity = $row
->getTranslation($lang)) {
$field_value = $translated_entity
->getValue();
return $field_value[$delta];
}
}
return NULL;
}
public function pluginStyleExtractLatLng($row, $latitude, $longitude, $loc_field_value = NULL) {
$delta =& drupal_static(__FUNCTION__);
if (isset($delta)) {
$delta++;
}
$row = $row->_entity;
$field_name = $latitude;
$field_value = $row
->get($field_name)
->getValue();
if (!isset($delta) || !$row
->get($field_name)
->isEmpty() || $delta >= count($field_value)) {
$delta = 0;
}
$location = new \StdClass();
$base = $this
->pluginStyleGetBase2($row, $field_name, $delta);
if (empty($base)) {
$base = $this
->pluginStyleGetBase2($row, $latitude, $delta);
}
if (empty($base)) {
$base = $loc_field_value;
}
if (empty($base)) {
}
else {
if (is_string($base)) {
$parts = explode(':', $base);
if (isset($parts[2])) {
$location->latitude = (double) $parts[1];
$location->longitude = (double) $parts[2];
}
}
elseif (isset($base['geo_type'])) {
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler
->moduleExists('leaflet')) {
$leafletSevice = \Drupal::service('leaflet.service');
if (!isset($base['wkt'])) {
$base['wkt'] = $base['value'];
}
$location = $leafletSevice
->leafletProcessGeofield([
$base,
]);
$location = (object) reset($location);
if (isset($location->lat)) {
$location->latitude = $location->lat;
unset($location->lat);
}
if (isset($location->lon)) {
$location->longitude = $location->lon;
unset($location->lon);
}
}
else {
$is_point = $base['geo_type'] == 'point';
if ($is_point) {
$point = empty($base['geom']) ? $base['wkt'] : $base['geom'];
$is_point = Unicode::substr(trim($point), 0, 7) == 'POINT (';
if ($is_point) {
$parts = explode(' ', Unicode::substr($point, 7));
$is_point = count($parts) > 1;
}
}
$location->longitude = $is_point ? (double) $parts[0] : $base['lon'];
$location->latitude = $is_point ? (double) $parts[1] : $base['lat'];
}
}
elseif (isset($base['lng'])) {
$location->latitude = $base['lat'];
$location->longitude = $base['lng'];
}
elseif (isset($base['longitude'])) {
$location->latitude = $base['latitude'];
$location->longitude = $base['longitude'];
}
elseif (isset($row->{'field_' . $latitude}[$delta]['raw']['value'])) {
$location->latitude = $row->{'field_' . $latitude}[$delta]['raw']['value'];
$location->longitude = $row->{'field_' . $longitude}[$delta]['raw']['value'];
}
}
return isset($location->type) || isset($location->latitude) ? $location : NULL;
}
public function pluginStyleSetMarkerColor(&$location, $row, $differentiator, $differentiator_color_associations) {
if (!empty($differentiator)) {
if (!empty($row->{$differentiator})) {
$differentiator_value = $row->{$differentiator};
}
elseif (!empty($row->{'field_' . $differentiator})) {
$differentiator_values = $row->{'field_' . $differentiator};
if (is_array($differentiator_values)) {
$num_values = count($differentiator_values);
if (isset($differentiator_values[0]['raw'])) {
$differentiator_value = $differentiator_values[$num_values - 1]['raw'];
}
elseif (isset($differentiator_values[0]['rendered']['#markup'])) {
$differentiator_value = $differentiator_values[$num_values - 1]['rendered']['#markup'];
}
}
}
else {
$differentiator_value = '';
if (!isset($row->{'field_' . $differentiator})) {
$this->messenger
->addMessage($this->stringTranslation
->translate('IPGV&M: no differentiator values found for %diff. Cannot set marker images.', [
'%diff' => $differentiator,
]), 'warning', FALSE);
}
}
if (!empty($differentiator_value)) {
foreach ($differentiator_color_associations as $association) {
$match = TRUE;
if (is_array($differentiator_value)) {
if (empty($differentiator_value['tid'])) {
foreach ($association['differentiator_value'] as $name => $subvalue) {
if (!isset($differentiator_value[$name]) || !$this->ipGeolocGlobal
->isInRange($differentiator_value[$name], $subvalue)) {
$match = FALSE;
break;
}
}
}
else {
$range = reset($association['differentiator_value']);
$match = $this->ipGeolocGlobal
->isInRange($differentiator_value['tid'], $range);
}
}
else {
$range = $association['differentiator_value'];
$match = $this->ipGeolocGlobal
->isInRange($differentiator_value, $range);
}
if ($match) {
$location->marker_color = $association['color'];
if (!empty($association['special char'])) {
$location->marker_special_char = $association['special char'];
}
if (!empty($association['special char class'])) {
$location->marker_special_char_class = $association['special char class'];
}
}
}
}
}
if (isset($location->marker_color) && is_array($location->marker_color)) {
$location->marker_color = reset($location->marker_color);
}
}
public function pluginStyleDecorate(&$location, $row, $tooltip_field, $tag_field) {
if ($tooltip_field) {
$tooltip = $tooltip_field
->theme($row);
$location->marker_tooltip = strip_tags($this
->pluginStyleAddLabel($tooltip_field, $tooltip));
}
if ($tag_field) {
$tag = $tag_field
->theme($row);
$location->marker_tag = $this
->pluginStyleAddLabelAndStyling($tag_field, $tag);
}
if (isset($row->nid)) {
$location->id = $row->nid;
}
elseif (isset($row->tid)) {
$location->id = $row->tid;
}
}
public function pluginStyleRenderFields($views_plugin_style) {
if (!$views_plugin_style
->usesFields() || isset($views_plugin_style->rendered_fields)) {
return;
}
$rendered_fields = [];
$field_ids = array_keys($views_plugin_style->view->field);
$differentiator = $views_plugin_style->options['differentiator']['differentiator_field'];
$latitude = trim($views_plugin_style->options['ip_geoloc_views_plugin_latitude']);
$longitude = trim($views_plugin_style->options['ip_geoloc_views_plugin_longitude']);
foreach ($views_plugin_style->view->result as $i => &$row) {
foreach ($field_ids as $field_id) {
$field = $views_plugin_style->view->field[$field_id];
$field_value = $field
->theme($row);
$special_field_id = 'views_' . $field_id;
if (($special_field_id == $differentiator || $special_field_id == $latitude || $special_field_id == $longitude) && empty($row->{$special_field_id})) {
$row->{$special_field_id}[] = $field_value;
}
if (!$field->options['exclude']) {
$styled_field_value = $this
->pluginStyleAddLabelAndStyling($field, $field_value);
$rendered_fields[$i][$field_id] = $styled_field_value;
}
}
}
$views_plugin_style
->setRenderedFields($rendered_fields);
return $rendered_fields;
}
public function pluginStyleAddLabel($field, $field_content) {
$label = $field
->label();
if ($label && $field->options['element_label_colon']) {
$label .= ': ';
}
return $label . $field_content;
}
public function pluginStyleAddLabelAndStyling($field, $field_content) {
if ($field_content_type = $field
->elementType(TRUE, TRUE)) {
$element = '<' . $field_content_type;
if ($field_content_class = $field
->elementClasses(NULL)) {
$element .= ' class="' . $field_content_class . '"';
}
$element .= '>';
$close_element = '</' . $field_content_type . '>';
$field_content = $element . $field_content . $close_element;
}
$label = $field
->label();
if ($label && $field->options['element_label_colon']) {
$label .= ': ';
}
$label_and_field_content = $label . $field_content;
if ($label_type = $field
->elementLabelType(TRUE, TRUE)) {
$element = '<' . $label_type;
if ($label_class = $field
->elementLabelClasses(NULL)) {
$element .= ' class="' . $label_class . '"';
}
$element .= '>';
$close_element = '</' . $label_type . '>';
$label_and_field_content = $element . $label . $close_element . $field_content;
}
if ($wrapper_type = $field
->elementWrapperType(TRUE, TRUE)) {
$element = '<' . $wrapper_type;
if ($wrapper_class = $field
->elementWrapperClasses(NULL)) {
$element .= ' class="' . $wrapper_class . '"';
}
$element .= '>';
$close_element = '</' . $wrapper_type . '>';
return $element . $label_and_field_content . $close_element;
}
return $label_and_field_content;
}
function getViewResult($views_plugin_style, $field, $index) {
$field_name = $field
->getName();
if (!isset($views_plugin_style->view->result[$index])) {
$this
->messenger($this->stringTranslation
->translate("Field %name: no value found amongst view results.", array(
'%name' => $field_name,
)), 'warning', FALSE);
return NULL;
}
$row = $views_plugin_style->view->result[$index];
$row = $row->_entity
->getFields();
if (isset($row->{"field_{$field_name}"}) && $row->{"field_{$field_name}"} != array()) {
$values = $row->{"field_{$field_name}"};
}
elseif (isset($row->{"views_{$field_name}"})) {
$values = $row->{"views_{$field_name}"};
}
elseif (isset($row->{$field_name})) {
$values = $row->{$field_name};
}
elseif (isset($views_plugin_style->view->field[$field_name])) {
$values = $views_plugin_style
->getFieldValue($index, $field_name);
}
elseif (!empty($row->_entity_properties)) {
$values = $this
->pluginStyleGetSearchApiValue($row, $field_name);
}
else {
return array(
'',
);
}
if (!is_array($values)) {
return array(
$values,
);
}
$single_values = array();
foreach ($values as $value) {
if (isset($value['raw'])) {
$value = $value['raw'];
}
if (isset($field['type'])) {
if ($field['type'] == 'addressfield') {
return $value;
}
if (is_array($value)) {
$value = $field['type'] == 'taxonomy_term_reference' ? $value['tid'] : reset($value);
}
}
elseif (is_array($value)) {
$value = reset($value);
}
$single_values[] = $value;
}
return $single_values;
}
function pluginStyleGetSearchApiValue($row, $field_name) {
$field_value = null;
if (isset($row->_entity_properties[$field_name])) {
$field_value = $row->_entity_properties[$field_name];
}
elseif ($last_ = strrpos($field_name, '_')) {
$alt_field_name = Unicode::substr($field_name, $last_ + 1);
if (isset($row->_entity_properties[$alt_field_name])) {
$field_value = $row->_entity_properties[$alt_field_name];
}
}
else {
$alt_field_name = 'field_' . $field_name;
if (isset($row->_entity_properties[$alt_field_name])) {
$field_value = $row->_entity_properties[$alt_field_name];
}
}
if (isset($field_value)) {
}
else {
$this
->messenger($this->stringTranslation
->translate('IPGV&M: Could not retrieve value of field %name, as its name does not appear among these Search API keys: %keys', array(
'%name' => $field_name,
'%keys' => implode(', ', array_keys($row->_entity_properties)),
)), 'warning', FALSE);
}
return isset($field_value) ? $field_value : '';
}
}