View source
<?php
namespace Drupal\geolocation\Plugin\geolocation\LocationInput;
use Drupal\geolocation\LocationManager;
use Drupal\geolocation\LocationInputInterface;
use Drupal\geolocation\LocationInputBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Location extends LocationInputBase implements LocationInputInterface {
protected $locationManager;
protected $locationPluginId = '';
public function __construct(array $configuration, $plugin_id, $plugin_definition, LocationManager $location_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->locationManager = $location_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.geolocation.location'));
}
public static function getDefaultSettings() {
$settings = parent::getDefaultSettings();
$settings['location_settings'] = [
'settings' => [],
];
$settings['location_plugin_id'] = '';
return $settings;
}
public function getSettingsForm($option_id = NULL, array $settings = [], $context = NULL) {
$values = explode(':', $option_id);
if (count($values) !== 2) {
return [];
}
$location_plugin_id = $values[0];
$location_option_id = $values[1];
if (!$this->locationManager
->hasDefinition($location_plugin_id)) {
return [];
}
$location_plugin = $this->locationManager
->createInstance($location_plugin_id);
$form = parent::getSettingsForm($location_plugin
->getPluginId(), $settings, $context);
$form['location_settings']['settings'] = $location_plugin
->getSettingsForm($location_option_id, $settings['location_settings']['settings'], $context);
$form['location_plugin_id'] = [
'#type' => 'value',
'#value' => $location_plugin_id,
];
return $form;
}
public function getAvailableLocationInputOptions($context) {
$options = [];
foreach ($this->locationManager
->getDefinitions() as $location_plugin_id => $location_plugin_definition) {
$location_plugin = $this->locationManager
->createInstance($location_plugin_id);
foreach ($location_plugin
->getAvailableLocationOptions($context) as $location_id => $location_label) {
$options[$location_plugin_id . ':' . $location_id] = $location_label;
}
}
return $options;
}
public function getCoordinates($form_value, $center_option_id, array $center_option_settings, $context = NULL) {
$values = explode(':', $center_option_id);
if (count($values) !== 2) {
return [];
}
$location_plugin_id = $values[0];
$location_option_id = $values[1];
if (!$this->locationManager
->hasDefinition($location_plugin_id)) {
return [];
}
$location = $this->locationManager
->createInstance($location_plugin_id);
$center = $location
->getCoordinates($location_option_id, $center_option_settings['location_settings']['settings'], $context);
if (empty($center)) {
return FALSE;
}
return $center;
}
public function getForm($center_option_id, array $center_option_settings, $context = NULL, array $default_value = NULL) {
return [];
}
}