PreprocessorPluginManager.php in Geocoder 8.2
File
modules/geocoder_field/src/PreprocessorPluginManager.php
View source
<?php
namespace Drupal\geocoder_field;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\geocoder\GeocoderPluginManagerBase;
use Drupal\geocoder_field\Annotation\GeocoderPreprocessor;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\Core\Entity\ContentEntityInterface;
class PreprocessorPluginManager extends GeocoderPluginManagerBase {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/Geocoder/Preprocessor', $namespaces, $module_handler, PreprocessorInterface::class, GeocoderPreprocessor::class);
$this
->alterInfo('geocoder_preprocessor_info');
$this
->setCacheBackend($cache_backend, 'geocoder_preprocessor_plugins');
}
public function preprocess(FieldItemListInterface &$field) {
$type = $field
->getFieldDefinition()
->getType();
$definitions = array_filter($this
->getDefinitions(), function ($definition) use ($type) {
return in_array($type, $definition['field_types']);
});
uasort($definitions, [
SortArray::class,
'sortByWeightElement',
]);
foreach ($definitions as $plugin_id => $definition) {
$preprocessor = $this
->createInstance($plugin_id);
$preprocessor
->setField($field)
->preprocess();
}
}
public function getOrderedGeocodeFields(ContentEntityInterface $entity) {
$geocoder_fields = [];
$results = [];
foreach ($entity
->getFields() as $field_name => $field) {
if (!($field_config = $field
->getFieldDefinition()) instanceof FieldConfigInterface) {
continue;
}
$geocoder = $field_config
->getThirdPartySettings('geocoder_field');
if (empty($geocoder['method']) || $geocoder['method'] === 'none') {
continue;
}
$geocoder_fields[$field_name] = [
'field_name' => $field_name,
'field_value' => $field,
'weight' => isset($geocoder['weight']) ? $geocoder['weight'] : 0,
];
}
usort($geocoder_fields, function ($a, $b) {
if ($a['weight'] === $b['weight']) {
return 0;
}
return $a['weight'] < $b['weight'] ? -1 : 1;
});
foreach ($geocoder_fields as $field) {
$results[$field['field_name']] = $field['field_value'];
}
return $results;
}
public function sourceFieldIsSameOfOriginal(FieldItemListInterface $source_field, FieldItemListInterface $original_field) {
$source_value = $source_field
->getValue();
$original_value = $original_field
->getValue();
if (isset($source_value[0]) && !isset($source_value[0]['value']) && isset($source_value[0]['target_id'])) {
foreach ($source_value as $i => $value) {
$source_value[$i] = isset($value['target_id']) ? $value['target_id'] : '';
}
}
if (isset($original_value[0]) && !isset($original_value[0]['value']) && isset($original_value[0]['target_id'])) {
foreach ($original_value as $i => $value) {
$original_value[$i] = isset($value['target_id']) ? $value['target_id'] : '';
}
}
return $source_value == $original_value;
}
}