View source
<?php
namespace Drupal\leaflet_views\Plugin\views\style;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Leaflet\LeafletService;
class Leaflet extends StylePluginBase implements ContainerFactoryPluginInterface {
protected $usesGrouping = FALSE;
protected $entityManager;
protected $entityFieldManager;
protected $entityDisplay;
protected $renderer;
protected $leafletService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_manager, EntityFieldManagerInterface $entity_field_manager, EntityDisplayRepositoryInterface $entity_display, RendererInterface $renderer, LeafletService $leaflet_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entity_manager;
$this->entityFieldManager = $entity_field_manager;
$this->entityDisplay = $entity_display;
$this->renderer = $renderer;
$this->leafletService = $leaflet_service;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('entity_display.repository'), $container
->get('renderer'), $container
->get('leaflet.service'));
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$map_options = [];
foreach (leaflet_map_get_info() as $key => $map) {
$map_options[$key] = $map['label'];
}
$form['map'] = [
'#title' => $this
->t('Map'),
'#type' => 'select',
'#options' => $map_options,
'#default_value' => $this->options['map'] ?: '',
'#required' => TRUE,
];
$form['height'] = [
'#title' => $this
->t('Map height'),
'#type' => 'textfield',
'#field_suffix' => $this
->t('px'),
'#size' => 4,
'#default_value' => $this->options['height'],
'#required' => TRUE,
];
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
parent::validateOptionsForm($form, $form_state);
$height = $form_state
->getValue([
'style_options',
'height',
]);
if (!empty($style_options['height']) && (!is_numeric($height) || $height <= 0)) {
$form_state
->setError($form['height'], $this
->t('Map height needs to be a positive number.'));
}
}
public function evenEmpty() {
return TRUE;
}
public function query() {
$this->built = TRUE;
}
public function render() {
$features = [];
foreach ($this->view->attachment_before as $id => $attachment) {
if (!empty($attachment['#leaflet-attachment'])) {
$features = array_merge($features, $attachment['rows']);
$this->view->element['#attached'] = NestedArray::mergeDeep($this->view->element['#attached'], $attachment['#attached']);
unset($this->view->attachment_before[$id]);
}
}
$map_info = leaflet_map_get_info($this->options['map']);
if (self::hasFeatureGroups($features)) {
$map_info['settings'] += [
'layerControl' => TRUE,
];
}
$element = $this->leafletService
->leafletRenderMap($map_info, $features, $this->options['height'] . 'px');
$this->view->element['#attached'] = NestedArray::mergeDeep($this->view->element['#attached'], $element['#attached']);
$element['#attached'] =& $this->view->element['#attached'];
return $element;
}
protected static function hasFeatureGroups(array $features) {
foreach ($features as $feature) {
if (!empty($feature['group'])) {
return TRUE;
}
}
return FALSE;
}
public function validate() {
$errors = parent::validate();
if (empty($this->options['map'])) {
$errors[] = $this
->t('Style @style requires a leaflet map to be configured.', [
'@style' => $this->definition['title'],
]);
}
return $errors;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['map'] = [
'default' => '',
];
$options['height'] = [
'default' => '400',
];
return $options;
}
}