View source
<?php
namespace Drupal\geocoder_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\geocoder\DumperPluginManager;
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 Drupal\geocoder_field\Plugin\Field\GeocodeFormatterBase;
use Drupal\geocoder_field\PreprocessorPluginManager;
use Drupal\geocoder\Entity\GeocoderProvider;
class FileGeocodeFormatter extends GeocodeFormatterBase {
protected $preprocessorManager;
protected $formatterPlugin = 'file';
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, PreprocessorPluginManager $preprocessor_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $geocoder, $provider_plugin_manager, $dumper_plugin_manager, $renderer, $link_generator, $logger_factory, $entity_type_manager);
$this->preprocessorManager = $preprocessor_manager;
}
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'), $container
->get('plugin.manager.geocoder.preprocessor'));
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['intro'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this->pluginDefinition['description'],
];
$element += parent::settingsForm($form, $form_state);
$compatible_providers = array_filter($element['providers'], function ($e) {
$geocoder_providers = $this->geocoderProviders;
if (isset($geocoder_providers[$e]) && ($geocoder_provider = $geocoder_providers[$e])) {
$plugin = $geocoder_provider
->getPlugin();
return $plugin
->getPluginId() == $this->formatterPlugin;
}
return TRUE;
}, ARRAY_FILTER_USE_KEY);
if (count($element['providers']) - count($compatible_providers) == count($this->geocoderProviders)) {
$element['warning'] = [
'#markup' => $this
->t('Any "@plugin" Geocoder Provider available for this Formatter.', [
'@plugin' => $this->formatterPlugin,
]),
];
}
$element['providers'] = $compatible_providers;
return $element;
}
public function settingsSummary() {
$summary['intro'] = $this->pluginDefinition['description'];
$summary += parent::settingsSummary();
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
try {
$dumper = $this->dumperPluginManager
->createInstance($this
->getSetting('dumper'));
$preprocessor = $this->preprocessorManager
->createInstance('file');
$preprocessor
->setField($items)
->preprocess();
$providers = $this
->getEnabledGeocoderProviders();
foreach ($items as $delta => $item) {
if ($address_collection = $this->geocoder
->geocode($item->value, $providers)) {
$elements[$delta] = [
'#markup' => $dumper
->dump($address_collection
->first()),
];
}
}
} catch (\Exception $e) {
watchdog_exception('geocoder', $e);
}
return $elements;
}
public function getEnabledGeocoderProviders() : array {
$providers = array_filter(parent::getEnabledGeocoderProviders(), function (GeocoderProvider $geocoder_provider) {
$plugin = $geocoder_provider
->getPlugin();
return $plugin
->getPluginId() == $this->formatterPlugin;
});
return $providers;
}
}