View source
<?php
namespace Drupal\geocoder_address\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\geocoder_field\Plugin\Field\FieldFormatter\GeocodeFormatter;
use Geocoder\Model\AddressCollection;
use Drupal\Component\Plugin\Exception\PluginException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\geocoder\GeocoderInterface;
use Drupal\geocoder\ProviderPluginManager;
use Drupal\geocoder\DumperPluginManager;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\geocoder_address\AddressService;
class AddressGeocodeFormatter extends GeocodeFormatter {
protected $addressService;
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, AddressService $address_service) {
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->addressService = $address_service;
}
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('geocoder_address.address'));
}
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) {
$value = $item
->getValue();
$address_string = $this->addressService
->addressArrayToGeoString($value);
if ($address_collection = $this->geocoder
->geocode($address_string, $providers)) {
$elements[$delta] = [
'#markup' => $address_collection instanceof AddressCollection && !$address_collection
->isEmpty() ? $dumper
->dump($address_collection
->first()) : "",
];
}
}
return $elements;
}
}