GeocoderManager.php in Geolocation Field 8
File
src/GeocoderManager.php
View source
<?php
namespace Drupal\geolocation;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
class GeocoderManager extends DefaultPluginManager {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/geolocation/Geocoder', $namespaces, $module_handler, 'Drupal\\geolocation\\GeocoderInterface', 'Drupal\\geolocation\\Annotation\\Geocoder');
$this
->alterInfo('geolocation_geocoder_info');
$this
->setCacheBackend($cache_backend, 'geolocation_geocoder');
}
public function getGeocoder($id, array $configuration = []) {
$definitions = $this
->getDefinitions();
if (empty($definitions[$id])) {
return FALSE;
}
try {
$instance = $this
->createInstance($id, $configuration);
if ($instance) {
return $instance;
}
} catch (\Exception $e) {
return FALSE;
}
return FALSE;
}
public function getLocationCapableGeocoders() {
$location_capable_geocoders = [];
foreach ($this
->getDefinitions() as $id => $definition) {
if (!empty($definition['locationCapable'])) {
$location_capable_geocoders[$id] = $definition;
}
}
return $location_capable_geocoders;
}
public function getBoundaryCapableGeocoders() {
$boundary_capable_geocoders = [];
foreach ($this
->getDefinitions() as $id => $definition) {
if (!empty($definition['boundaryCapable'])) {
$boundary_capable_geocoders[$id] = $definition;
}
}
return $boundary_capable_geocoders;
}
public static function addGeocoderSettingsFormAjax(array $form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement()['#parents'];
array_pop($triggering_element);
$target = $triggering_element;
$target[] = 'plugin_id';
$plugin_id = $form_state
->getValue($target, '');
$target = $triggering_element;
$target[] = 'settings';
$geocoder_settings = $form_state
->getValue($target, []);
$geocoder_plugin = \Drupal::service('plugin.manager.geolocation.geocoder')
->getGeocoder($plugin_id, $geocoder_settings);
if (empty($geocoder_plugin)) {
$return = [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => t('Non-existing geocoder plugin requested.'),
];
}
else {
$geocoder_settings_form = $geocoder_plugin
->getOptionsForm();
if (!empty($geocoder_settings_form)) {
$return = $geocoder_settings_form;
}
else {
$return = [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => t("No settings available."),
];
}
}
$return = array_merge_recursive($return, [
'#prefix' => '<div id="geocoder-plugin-settings">',
'#suffix' => '</div>',
]);
return $return;
}
}