View source
<?php
declare (strict_types=1);
namespace Drupal\geocoder_field\Plugin\Field;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\geocoder\DumperPluginManager;
use Drupal\geocoder\Entity\GeocoderProvider;
use Drupal\geocoder\GeocoderInterface;
use Drupal\geocoder\ProviderPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Geocoder\Model\AddressCollection;
use Drupal\Component\Plugin\Exception\PluginException;
abstract class GeocodeFormatterBase extends FormatterBase implements ContainerFactoryPluginInterface {
protected $geocoder;
protected $providerPluginManager;
protected $dumperPluginManager;
protected $renderer;
protected $link;
protected $loggerFactory;
protected $entityTypeManager;
protected $geocoderProviders = [];
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, GeocoderInterface $geocoder, ProviderPluginManager $provider_plugin_manager, DumperPluginManager $dumper_plugin_manager, RendererInterface $renderer, LinkGeneratorInterface $link_generator, LoggerChannelFactoryInterface $logger_factory, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->geocoder = $geocoder;
$this->providerPluginManager = $provider_plugin_manager;
$this->dumperPluginManager = $dumper_plugin_manager;
$this->renderer = $renderer;
$this->link = $link_generator;
$this->loggerFactory = $logger_factory;
$this->entityTypeManager = $entity_type_manager;
try {
$this->geocoderProviders = $this->entityTypeManager
->getStorage('geocoder_provider')
->loadMultiple();
} catch (\Exception $e) {
watchdog_exception('geocoder', $e);
}
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('geocoder'), $container
->get('plugin.manager.geocoder.provider'), $container
->get('plugin.manager.geocoder.dumper'), $container
->get('renderer'), $container
->get('link_generator'), $container
->get('logger.factory'), $container
->get('entity_type.manager'));
}
public static function defaultSettings() {
return parent::defaultSettings() + [
'dumper' => 'wkt',
'providers' => [],
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$providers = !empty($this
->getSetting('providers')) ? $this
->getSetting('providers') : [];
$element['#attached']['library'] = [
'geocoder/general',
];
$enabled_providers = [];
foreach ($providers as $provider_id => $provider_settings) {
if ($provider_settings['checked']) {
$enabled_providers[] = $provider_id;
}
}
$element['providers'] = $this->providerPluginManager
->providersPluginsTableList($enabled_providers);
$element['providers']['#element_validate'] = [
[
get_class($this),
'validateProvidersSettingsForm',
],
];
$element['dumper'] = [
'#type' => 'select',
'#weight' => 25,
'#title' => 'Output format',
'#default_value' => $this
->getSetting('dumper'),
'#options' => $this->dumperPluginManager
->getPluginsAsOptions(),
'#description' => t('Set the output format of the value. Ex, for a geofield, the format must be set to WKT.'),
];
return $element;
}
public function settingsSummary() {
$summary = [];
$dumper_plugins = $this->dumperPluginManager
->getPluginsAsOptions();
$dumper_plugin = $this
->getSetting('dumper');
$provider_labels = array_map(function (GeocoderProvider $provider) : ?string {
return $provider
->label();
}, $this
->getEnabledGeocoderProviders());
$summary['providers'] = $this
->t('Geocoder providers(s): @provider_ids', [
'@provider_ids' => !empty($provider_labels) ? implode(', ', $provider_labels) : $this
->t('Not set'),
]);
$summary['dumper'] = $this
->t('Output format: @format', [
'@format' => !empty($dumper_plugin) ? $dumper_plugins[$dumper_plugin] : $this
->t('Not set'),
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
try {
$dumper = $this->dumperPluginManager
->createInstance($this
->getSetting('dumper'));
} catch (PluginException $e) {
$this->loggerFactory
->get('geocoder')
->error('No Dumper has been set');
}
$providers = $this
->getEnabledGeocoderProviders();
foreach ($items as $delta => $item) {
if ($address_collection = $this->geocoder
->geocode($item->value, $providers)) {
$elements[$delta] = [
'#markup' => $address_collection instanceof AddressCollection && !$address_collection
->isEmpty() ? $dumper
->dump($address_collection
->first()) : "",
];
}
}
return $elements;
}
public function getEnabledGeocoderProviders() : array {
$formatter_settings = $this
->getSetting('providers');
$providers = array_filter($this->geocoderProviders, function (GeocoderProvider $provider) use ($formatter_settings) : bool {
return !empty($formatter_settings[$provider
->id()]) && $formatter_settings[$provider
->id()]['checked'] == TRUE;
});
uasort($providers, function (GeocoderProvider $a, GeocoderProvider $b) use ($formatter_settings) : int {
if ((int) $formatter_settings[$a
->id()]['weight'] === (int) $formatter_settings[$b
->id()]['weight']) {
return 0;
}
return (int) $formatter_settings[$a
->id()]['weight'] < (int) $formatter_settings[$b
->id()]['weight'] ? -1 : 1;
});
return $providers;
}
public function getGeocoderProviders() : array {
return $this->geocoderProviders;
}
public static function validateProvidersSettingsForm(array $element, FormStateInterface &$form_state) {
$providers = !empty($element['#value']) ? array_filter($element['#value'], function ($value) {
return isset($value['checked']) && TRUE == $value['checked'];
}) : [];
if (empty($providers)) {
$form_state
->setError($element, t('The selected Geocode operation needs at least one provider.'));
}
}
}