class DumperPluginManager in Geocoder 8.3
Same name and namespace in other branches
- 8.2 src/DumperPluginManager.php \Drupal\geocoder\DumperPluginManager
Provides a plugin manager for geocoder dumpers.
Hierarchy
- class \Drupal\Component\Plugin\PluginManagerBase implements PluginManagerInterface uses DiscoveryTrait
- class \Drupal\Core\Plugin\DefaultPluginManager implements CachedDiscoveryInterface, PluginManagerInterface, CacheableDependencyInterface uses DiscoveryCachedTrait, UseCacheBackendTrait
- class \Drupal\geocoder\GeocoderPluginManagerBase
- class \Drupal\geocoder\DumperPluginManager
- class \Drupal\geocoder\GeocoderPluginManagerBase
- class \Drupal\Core\Plugin\DefaultPluginManager implements CachedDiscoveryInterface, PluginManagerInterface, CacheableDependencyInterface uses DiscoveryCachedTrait, UseCacheBackendTrait
Expanded class hierarchy of DumperPluginManager
6 files declare their use of DumperPluginManager
- AddressGeocodeFormatter.php in modules/
geocoder_address/ src/ Plugin/ Field/ FieldFormatter/ AddressGeocodeFormatter.php - DefaultField.php in modules/
geocoder_field/ src/ Plugin/ Geocoder/ Field/ DefaultField.php - FileGeocodeFormatter.php in modules/
geocoder_field/ src/ Plugin/ Field/ FieldFormatter/ FileGeocodeFormatter.php - GeocodeFormatterBase.php in modules/
geocoder_field/ src/ Plugin/ Field/ GeocodeFormatterBase.php - GeocoderApiEnpoints.php in src/
Controller/ GeocoderApiEnpoints.php
1 string reference to 'DumperPluginManager'
1 service uses DumperPluginManager
File
- src/
DumperPluginManager.php, line 17
Namespace
Drupal\geocoderView source
class DumperPluginManager extends GeocoderPluginManagerBase {
/**
* List of fields having a max length.
*
* @var string[]
*/
private $maxLengthFieldTypes = [
'text',
'string',
];
/**
* The country manager service.
*
* @var \Drupal\Core\Locale\CountryManagerInterface
*/
protected $countryManager;
/**
* The logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $logger;
/**
* The Drupal messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* {@inheritdoc}
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, CountryManagerInterface $country_manager, 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->countryManager = $country_manager;
$this->logger = $logger_factory;
$this->messenger = $messenger;
}
/**
* Define an Address field value from a Geojson string.
*
* @param string $geojson
* The GeoJson place string.
*
* @return array
* An array of the Address field value.
*/
public function setAddressFieldFromGeojson($geojson) : array {
$geojson_array = Json::decode($geojson);
$geojson_array['properties'] += [
'adminLevels' => '',
'streetName' => '',
'streetNumber' => '',
'postalCode' => '',
'locality' => '',
];
// Define an administrative_area line1 from adminLevels name, if existing.
$administrative_area = '';
if (!empty($geojson_array['properties']['adminLevels'])) {
$administrative_area = array_shift($geojson_array['properties']['adminLevels'])['name'];
}
// Define the address line1, adding a street number to it, if existing.
$address_line1 = $geojson_array['properties']['streetName'];
if (!empty($geojson_array['properties']['streetNumber'])) {
$address_line1 .= ' ' . $geojson_array['properties']['streetNumber'];
}
return [
'country_code' => $this
->setCountryFromGeojson($geojson),
'address_line1' => $address_line1,
'postal_code' => $geojson_array['properties']['postalCode'],
'locality' => $geojson_array['properties']['locality'],
'administrative_area' => $administrative_area,
];
}
/**
* Define a Country value from a Geojson string.
*
* @param string $geojson
* The GeoJson place string.
*
* @return string
* A country code.
*/
public function setCountryFromGeojson($geojson) : string {
$geojson_array = Json::decode($geojson);
$country_code = isset($geojson_array['properties']['countryCode']) ? strtoupper(substr($geojson_array['properties']['countryCode'], 0, 2)) : '';
// Some provider (like MapQuest) might not return the 2 digits countryCode
// but just the country name or a 3 digits code,
// so try to convert it into countryCode,
// as it seems to be mandatory in Address Field Entity API.
if (!array_key_exists($country_code, $this->countryManager
->getList()) && isset($geojson_array['properties']['country'])) {
$country_code = strtoupper(substr($geojson_array['properties']['country'], 0, 2));
}
// Allow others modules to adjust the country_code at the end.
$this->moduleHandler
->alter('geocode_country_code', $country_code, $geojson_array);
return $country_code;
}
/**
* Check|Fix some incompatibility between Dumper output and Field Config.
*
* @param string $dumper_result
* The Dumper result string.
* @param \Drupal\geocoder\DumperInterface|\Drupal\Component\Plugin\PluginInspectionInterface $dumper
* The Dumper.
* @param \Drupal\Core\Field\FieldConfigInterface $field_config
* The Field Configuration.
*/
public function fixDumperFieldIncompatibility(&$dumper_result, $dumper, FieldConfigInterface $field_config) : void {
// Fix not UTF-8 encoded result strings.
// https://stackoverflow.com/questions/6723562/how-to-detect-malformed-utf-8-string-in-php
if (\is_string($dumper_result)) {
if (!preg_match('//u', $dumper_result)) {
$dumper_result = utf8_encode($dumper_result);
}
}
// If the field is a string|text type check if the result length is
// compatible with its max_length definition, otherwise truncate it and
// set | log a warning message.
if (\in_array($field_config
->getType(), $this->maxLengthFieldTypes, TRUE) && \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'));
// Display a max-length incompatibility warning message.
$this->messenger
->addWarning($incompatibility_warning_message);
// Log the max-length incompatibility.
$this->logger
->get('geocoder')
->warning($incompatibility_warning_message);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultPluginManager:: |
protected | property | Additional namespaces the annotation discovery mechanism should scan for annotation definitions. | |
DefaultPluginManager:: |
protected | property | Name of the alter hook if one should be invoked. | |
DefaultPluginManager:: |
protected | property | The cache key. | |
DefaultPluginManager:: |
protected | property | An array of cache tags to use for the cached definitions. | |
DefaultPluginManager:: |
protected | property | A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. | 9 |
DefaultPluginManager:: |
protected | property | The module handler to invoke the alter hook. | 1 |
DefaultPluginManager:: |
protected | property | An object that implements \Traversable which contains the root paths keyed by the corresponding namespace to look for plugin implementations. | |
DefaultPluginManager:: |
protected | property | The name of the annotation that contains the plugin definition. | |
DefaultPluginManager:: |
protected | property | The interface each plugin should implement. | 1 |
DefaultPluginManager:: |
protected | property | The subdirectory within a namespace to look for plugins, or FALSE if the plugins are in the top level of the namespace. | |
DefaultPluginManager:: |
protected | function | Invokes the hook to alter the definitions if the alter hook is set. | 1 |
DefaultPluginManager:: |
protected | function | Sets the alter hook name. | |
DefaultPluginManager:: |
public | function |
Clears static and persistent plugin definition caches. Overrides CachedDiscoveryInterface:: |
5 |
DefaultPluginManager:: |
protected | function | Extracts the provider from a plugin definition. | |
DefaultPluginManager:: |
protected | function | Finds plugin definitions. | 7 |
DefaultPluginManager:: |
private | function | Fix the definitions of context-aware plugins. | |
DefaultPluginManager:: |
public | function |
The cache contexts associated with this object. Overrides CacheableDependencyInterface:: |
|
DefaultPluginManager:: |
protected | function | Returns the cached plugin definitions of the decorated discovery class. | |
DefaultPluginManager:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyInterface:: |
|
DefaultPluginManager:: |
public | function |
The cache tags associated with this object. Overrides CacheableDependencyInterface:: |
|
DefaultPluginManager:: |
public | function |
Gets the definition of all plugins for this type. Overrides DiscoveryTrait:: |
2 |
DefaultPluginManager:: |
protected | function |
Gets the plugin discovery. Overrides PluginManagerBase:: |
12 |
DefaultPluginManager:: |
protected | function |
Gets the plugin factory. Overrides PluginManagerBase:: |
|
DefaultPluginManager:: |
public | function | Performs extra processing on plugin definitions. | 13 |
DefaultPluginManager:: |
protected | function | Determines if the provider of a definition exists. | 3 |
DefaultPluginManager:: |
public | function | Initialize the cache backend. | |
DefaultPluginManager:: |
protected | function | Sets a cache of plugin definitions for the decorated discovery class. | |
DefaultPluginManager:: |
public | function |
Disable the use of caches. Overrides CachedDiscoveryInterface:: |
1 |
DiscoveryCachedTrait:: |
protected | property | Cached definitions array. | 1 |
DiscoveryCachedTrait:: |
public | function |
Overrides DiscoveryTrait:: |
3 |
DiscoveryTrait:: |
protected | function | Gets a specific plugin definition. | |
DiscoveryTrait:: |
public | function | ||
DumperPluginManager:: |
protected | property | The country manager service. | |
DumperPluginManager:: |
protected | property | The logger factory. | |
DumperPluginManager:: |
private | property | List of fields having a max length. | |
DumperPluginManager:: |
protected | property | The Drupal messenger service. | |
DumperPluginManager:: |
public | function | Check|Fix some incompatibility between Dumper output and Field Config. | |
DumperPluginManager:: |
public | function | Define an Address field value from a Geojson string. | |
DumperPluginManager:: |
public | function | Define a Country value from a Geojson string. | |
DumperPluginManager:: |
public | function |
Creates the discovery object. Overrides DefaultPluginManager:: |
|
GeocoderPluginManagerBase:: |
protected | property | List of fields types available as source for Geocode operations. | |
GeocoderPluginManagerBase:: |
protected | property | List of fields types available as source for Reverse Geocode operations. | |
GeocoderPluginManagerBase:: |
public | function | Gets a list of fields types available for Geocode operations. | |
GeocoderPluginManagerBase:: |
public | function | Return the array of plugins and their settings if any. | 1 |
GeocoderPluginManagerBase:: |
public | function | Gets a list of available plugins to be used in forms. | |
GeocoderPluginManagerBase:: |
public | function | Gets a list of fields types available for Reverse Geocode operations. | |
PluginManagerBase:: |
protected | property | The object that discovers plugins managed by this manager. | |
PluginManagerBase:: |
protected | property | The object that instantiates plugins managed by this manager. | |
PluginManagerBase:: |
protected | property | The object that returns the preconfigured plugin instance appropriate for a particular runtime condition. | |
PluginManagerBase:: |
public | function |
Creates a pre-configured instance of a plugin. Overrides FactoryInterface:: |
12 |
PluginManagerBase:: |
public | function |
Gets a preconfigured instance of a plugin. Overrides MapperInterface:: |
7 |
PluginManagerBase:: |
protected | function | Allows plugin managers to specify custom behavior if a plugin is not found. | 1 |
UseCacheBackendTrait:: |
protected | property | Cache backend instance. | |
UseCacheBackendTrait:: |
protected | property | Flag whether caches should be used or skipped. | |
UseCacheBackendTrait:: |
protected | function | Fetches from the cache backend, respecting the use caches flag. | 1 |
UseCacheBackendTrait:: |
protected | function | Stores data in the persistent cache, respecting the use caches flag. |