PreprocessorBase.php in Geocoder 8.2
File
modules/geocoder_field/src/PreprocessorBase.php
View source
<?php
namespace Drupal\geocoder_field;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Locale\CountryManagerInterface;
abstract class PreprocessorBase extends PluginBase implements PreprocessorInterface, ContainerFactoryPluginInterface {
protected $field;
protected $countryManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CountryManagerInterface $country_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->countryManager = $country_manager;
}
public function setField(FieldItemListInterface $field) {
$this->field = $field;
return $this;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('country_manager'));
}
public function preprocess() {
if (!isset($this->field)) {
throw new \RuntimeException('A field (\\Drupal\\Core\\Field\\FieldItemListInterface) must be set with ::setField() before preprocessing.');
}
}
public function getPreparedReverseGeocodeValues(array $values = []) {
return array_map(function ($value) {
return array_combine([
'lat',
'lon',
], array_map('trim', explode(',', trim($value['value']), 2)));
}, $values);
}
}