View source
<?php
namespace Drupal\geocoder;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\geocoder\Annotation\GeocoderDumper;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
class DumperPluginManager extends GeocoderPluginManagerBase {
private $maxLengthFieldTypes = [
"text",
"string",
];
protected $logger;
protected $messenger;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, LoggerChannelFactoryInterface $logger_factory, MessengerInterface $messenger) {
parent::__construct('Plugin/Geocoder/Dumper', $namespaces, $module_handler, DumperInterface::class, GeocoderDumper::class);
$this
->alterInfo('geocoder_dumper_info');
$this
->setCacheBackend($cache_backend, 'geocoder_dumper_plugins');
$this->logger = $logger_factory;
$this->messenger = $messenger;
}
public function setAddressFieldFromGeojson($geojson) {
$geojson_array = Json::decode($geojson);
$country_code = $this
->setCountryFromGeojson($geojson);
$geojson_array['properties'] += [
'streetName' => '',
'postalCode' => '',
'locality' => '',
];
return [
'country_code' => $country_code,
'address_line1' => $geojson_array['properties']['streetName'],
'postal_code' => $geojson_array['properties']['postalCode'],
'locality' => $geojson_array['properties']['locality'],
];
}
public function setCountryFromGeojson($geojson) {
$geojson_array = Json::decode($geojson);
$country_code = isset($geojson_array['properties']['countryCode']) ? strtoupper(substr($geojson_array['properties']['countryCode'], 0, 2)) : NULL;
if (!isset($country_code)) {
$country_code = isset($geojson_array['properties']['country']) ? strtoupper(substr($geojson_array['properties']['country'], 0, 2)) : '';
}
return $country_code;
}
public function fixDumperFieldIncompatibility(&$dumper_result, $dumper, FieldConfigInterface $field_config) {
if (is_string($dumper_result)) {
if (!preg_match('//u', $dumper_result)) {
$dumper_result = utf8_encode($dumper_result);
}
}
if (in_array($field_config
->getType(), $this->maxLengthFieldTypes) && strlen($dumper_result) > $field_config
->getFieldStorageDefinition()
->getSetting('max_length')) {
$incompatibility_warning_message = t("The '@field_name' field 'max length' property is not compatible with the chosen '@dumper' dumper.<br>Thus <b>be aware</b> <u>the dumper output result has been truncated to @max_length chars (max length)</u>.<br> You are advised to change the '@field_name' field definition or chose another compatible dumper.", [
'@field_name' => $field_config
->getLabel(),
'@dumper' => $dumper
->getPluginId(),
'@max_length' => $field_config
->getFieldStorageDefinition()
->getSetting('max_length'),
]);
$dumper_result = substr($dumper_result, 0, $field_config
->getFieldStorageDefinition()
->getSetting('max_length'));
$this->messenger
->addMessage($incompatibility_warning_message, 'warning');
$this->logger
->get('geocoder')
->warning($incompatibility_warning_message);
}
}
}