View source
<?php
namespace Drupal\geolocation_address\Plugin\geolocation\DataProvider;
use Drupal\address\Plugin\Field\FieldType\AddressItem;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\geolocation\GeocoderManager;
use Drupal\views\Plugin\views\field\EntityField;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\geolocation\DataProviderInterface;
use Drupal\geolocation\DataProviderBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use CommerceGuys\Addressing\AddressFormat\AddressFormatRepositoryInterface;
class AddressFieldProvider extends DataProviderBase implements DataProviderInterface {
protected $geocoderManager = NULL;
protected $geocoder = NULL;
protected $addressFormatRepository;
protected $countryRepository;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager, GeocoderManager $geocoder_manager, AddressFormatRepositoryInterface $address_format_repository, CountryRepositoryInterface $country_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager);
$this->geocoderManager = $geocoder_manager;
if (!empty($configuration['geocoder'])) {
$this->geocoder = $this->geocoderManager
->createInstance($configuration['geocoder']);
}
$this->addressFormatRepository = $address_format_repository;
$this->countryRepository = $country_repository;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_field.manager'), $container
->get('plugin.manager.geolocation.geocoder'), $container
->get('address.address_format_repository'), $container
->get('address.country_repository'));
}
public function isViewsGeoOption(FieldPluginBase $views_field) {
if ($views_field instanceof EntityField) {
$entityFieldManager = \Drupal::service('entity_field.manager');
$field_map = $entityFieldManager
->getFieldMap();
if (!empty($field_map) && !empty($views_field->configuration['entity_type']) && !empty($views_field->configuration['field_name']) && !empty($field_map[$views_field->configuration['entity_type']]) && !empty($field_map[$views_field->configuration['entity_type']][$views_field->configuration['field_name']])) {
if ($field_map[$views_field->configuration['entity_type']][$views_field->configuration['field_name']]['type'] == 'address') {
return TRUE;
}
}
}
return FALSE;
}
public function isFieldGeoOption(FieldDefinitionInterface $fieldDefinition) {
return $fieldDefinition
->getType() == 'address';
}
public function getPositionsFromItem(FieldItemInterface $item) {
if (!$item instanceof AddressItem) {
return [];
}
if (empty($this->geocoder)) {
return [];
}
$address_format = str_replace([
"\r",
"\n",
], ' ', $this->addressFormatRepository
->get($item
->getCountryCode())
->getFormat());
$formatted_address = new FormattableMarkup(str_replace('%', ':', $address_format), [
':givenName' => $item
->getGivenName(),
':familyName' => $item
->getFamilyName(),
':organization' => $item
->getOrganization(),
':addressLine1' => $item
->getAddressLine1(),
':addressLine2' => $item
->getAddressLine2(),
':dependentLocality' => $item
->getDependentLocality(),
':locality' => $item
->getLocality(),
':administrativeArea' => $item
->getAdministrativeArea(),
':postalCode' => $item
->getPostalCode(),
':sortingCode' => $item
->getSortingCode(),
]);
$address = (string) $formatted_address;
$address = trim($address);
$address = $address . ' ' . $this->countryRepository
->get($item
->getCountryCode())
->getName();
$coordinates = $this->geocoder
->geocode($address);
return [
$coordinates['location'],
];
}
public function getSettingsForm(array $settings, array $parents = []) {
$element = parent::getSettingsForm($settings, $parents);
$geocoder_options = [];
foreach ($this->geocoderManager
->getDefinitions() as $geocoder_id => $geocoder_definition) {
if (empty($geocoder_definition['locationCapable'])) {
continue;
}
$geocoder_options[$geocoder_id] = $geocoder_definition['name'];
}
if (empty($geocoder_options)) {
return [
'#markup' => $this
->t('No geocoder option found'),
];
}
$element['geocoder'] = [
'#type' => 'select',
'#title' => $this
->t('Geocoder'),
'#options' => $geocoder_options,
'#default_value' => empty($settings['geocoder']) ? key($geocoder_options) : $settings['geocoder'],
'#description' => $this
->t('Choose plugin to geocode address into coordinates.'),
'#weight' => -1,
];
return $element;
}
}