Skip to content
This repository has been archived by the owner on Jan 22, 2020. It is now read-only.

Commit

Permalink
Refactored 'dmaps_geocoding_options' form, added test
Browse files Browse the repository at this point in the history
  • Loading branch information
afi13 committed Feb 5, 2016
1 parent 25c0cc9 commit 601e3fd
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 93 deletions.
6 changes: 3 additions & 3 deletions d8/modules/custom/dmaps/config/schema/dmaps.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ dmaps.geocoding_settings.*:
type: config_object
label: 'Geocoding settings'
mapping:
location_general_geocoders_in_use:
type: integer
label: 'General geocoders in use'
geocoder:
type: string
label: 'Geocoding settings'

dmaps.geocoder.*:
type: config_object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

/**
* @file
* Contains \Drupal\dmaps\Form\DmapsGeocoderOptionsForm
*/

Expand Down
118 changes: 86 additions & 32 deletions d8/modules/custom/dmaps/src/Form/DmapsGeocodingOptionsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,56 @@
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\dmaps\DmapsGeocoder;
use Drupal\dmaps\LocationCountriesManager;

class DmapsGeocodingOptionsForm extends ConfigFormBase {

/**
* Geocoder provider
*
* @var string
*/
protected $geocoderManager;

/**
* The dmaps LocationCountriesManager service
*
* @var \Drupal\dmaps\LocationCountriesManager
*/
protected $countryManager;

/**
* Constructs a new DmapsGeocodingOptionsForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param \Drupal\dmaps\DmapsGeocoder $geocoder_manager
* The dmaps Geocoder service.
* @param \Drupal\dmaps\LocationCountriesManager $country_manager
* The dmaps country manager service.
*/
public function __construct(ConfigFactoryInterface $config_factory, DmapsGeocoder $geocoder_manager, LocationCountriesManager $country_manager) {
parent::__construct($config_factory);
$this->configFactory = $config_factory;
$this->countryManager = $country_manager;
$this->geocoderManager = $geocoder_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('dmaps.geocoder'),
$container->get('dmaps.location_countries_manager')
);
}

/**
* @inheritdoc
*/
Expand All @@ -31,10 +78,8 @@ public function getEditableConfigNames() {
* @inheritdoc
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// @todo 8.x-2.x - need to rewrite after implementing geocoding providers plugins.
// @todo 8.x-2.x - need to refactor after implementing geocoding providers plugins.
$config = $this->config('dmaps.geocoding_settings');
$geocoder = \Drupal::service('dmaps.geocoder');
$country_manager = \Drupal::service('dmaps.location_countries_manager');

$form['countries'] = [
'#type' => 'table',
Expand All @@ -49,27 +94,27 @@ public function buildForm(array $form, FormStateInterface $form_state) {
// First, we build two arrays to help us figure out on the fly whether a specific country is covered by a multi-country geocoder,
// and what the details of the multi-country geocoder are
// (1) Get list of geocoders.
$general_geocoders_list = $geocoder->getGeocoders();
$geocoders_list = $this->geocoderManager->getGeocoders();
// (2) get data about each geocoder and the list of countries covered by each geocoder.
$general_geocoders_data = [];
$general_geocoders_countries = [];
foreach ($general_geocoders_list as $geocoder_name) {
$geocoder->initGeocoder($geocoder_name);
$geocoders_data = [];
$geocoders_countries = [];
foreach ($geocoders_list as $geocoder_name) {
$this->geocoderManager->initGeocoder($geocoder_name);
$info_function = $geocoder_name . '_geocode_info';
if (function_exists($info_function)) {
$general_geocoders_data[$geocoder_name] = $info_function();
$geocoders_data[$geocoder_name] = $info_function();
}

$countries_function = $geocoder_name . '_geocode_country_list';
if (function_exists($countries_function)) {
$general_geocoders_countries[$geocoder_name] = $countries_function();
$geocoders_countries[$geocoder_name] = $countries_function();
}
}

$supported_countries = $country_manager->getSupportedList();
$supported_countries = $this->countryManager->getSupportedList();
foreach ($supported_countries as $country_iso => $country_name) {
$geocoding_options = [];
$country_manager->locationLoadCountry($country_iso);
$this->countryManager->locationLoadCountry($country_iso);

$form['countries'][$country_iso] = [
'#type' => 'container',
Expand All @@ -90,27 +135,40 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$country_specific_provider_function = 'location_geocode_' . $country_iso . '_providers';
if (function_exists($country_specific_provider_function)) {
foreach ($country_specific_provider_function() as $name => $details) {
$geocoder_link = '<a href="' . $details['url'] . '">' . $details['name'] . '</a>';
$terms_link = '(<a href="' . $details['tos'] . '">' . $this->t('Terms of Use') . '</a>)';
$url = Url::fromUri($details['url']);
$geocoder_link = Link::fromTextAndUrl($details['name'], $url)
->toRenderable();
$url = Url::fromUri($details['tos']);
$terms_link = Link::fromTextAndUrl($this->t('Terms of Use'), $url)
->toRenderable();
$geocoding_options[$name . '|' . $country_iso] = $geocoder_link . ' ' . $terms_link;
}
}

foreach ($general_geocoders_list as $geocoder_name) {
if (in_array($country_iso, $general_geocoders_countries[$geocoder_name])) {
$geocoder_link = '<a href="' . $general_geocoders_data[$geocoder_name]['url'] . '">' . $general_geocoders_data[$geocoder_name]['name'] . '</a>';
$terms_link = '(<a href="' . $general_geocoders_data[$geocoder_name]['tos'] . '">' . $this->t('Terms of Use') . '</a>)';
$geocoding_options[$geocoder_name] = $geocoder_link . ' ' . $terms_link;
foreach ($geocoders_list as $geocoder_name) {
if (in_array($country_iso, $geocoders_countries[$geocoder_name])) {
$url = Url::fromUri($geocoders_data[$geocoder_name]['url']);
$geocoder_link = Link::fromTextAndUrl($geocoders_data[$geocoder_name]['name'], $url)
->toRenderable();
$url = Url::fromUri($geocoders_data[$geocoder_name]['tos']);
$terms_link = Link::fromTextAndUrl($this->t('Terms of Use'), $url)
->toRenderable();
$renderer = \Drupal::service('renderer');
$terms_link = $renderer->render($terms_link);
$terms_link = '(' . $terms_link . ')';
$geocoding_options[$geocoder_name] = $renderer->render($geocoder_link) . ' ' . $terms_link;
}
}

$country_geocode_key = 'location_geocode_' . $country_iso;
$country_geocode_key = 'geocode_' . $country_iso;
$current_value = $config->get($country_iso);

if (count($geocoding_options)) {
$geocoding_options = array_merge(['none' => $this->t('None')], $geocoding_options);

$form['countries'][$country_iso][$country_geocode_key] = [
'#type' => 'radios',
'#default_value' => $config->get($country_geocode_key) ? $config->get($country_geocode_key) : 'none',
'#default_value' => $current_value ? $current_value : 'none',
'#options' => $geocoding_options,
];
}
Expand All @@ -121,8 +179,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
];
}

$config_link_key = 'location_geocode_config_link_' . $country_iso;
$current_value = $config->get($country_geocode_key);
$config_link_key = 'config_link_' . $country_iso;
if ($current_value == 'none') {
$form['countries'][$country_iso][$config_link_key] = [
'#type' => 'markup',
Expand All @@ -141,7 +198,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#url' => Url::fromRoute('dmaps.locations.geocoder_options', [
'iso' => $country_iso,
'service' => $current_val_chopped,
])->toString(),
]),
];
}
elseif (function_exists($geocode_settings_form_function_general)) {
Expand All @@ -151,7 +208,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#url' => Url::fromRoute('dmaps.locations.geocoder_options', [
'iso' => $country_iso,
'service' => $current_value,
])->toString(),
]),
];
}
else {
Expand All @@ -170,18 +227,15 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* @inheritdoc
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('dmaps.geocoding_settings');
$geocoders = \Drupal::service('dmaps.geocoder')->getGeocoders();
$geocoders_in_use = [];
$config = $this->config('dmaps.geocoding_settings');

$values = $form_state->getValues();
foreach ($values['countries'] as $country) {
foreach ($values['countries'] as $iso => $country) {
$key = key($country);
$value = $country[$key];
if (in_array($value, $geocoders)) {
$geocoders_in_use[$value] = $value;
$config->set($key, $country[$key]);
}
$geocoders_in_use[$value] = $value;
$config->set($iso, $value);
}
$config->save();

Expand Down
3 changes: 1 addition & 2 deletions d8/modules/custom/dmaps/src/LocationCountriesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ public function locationCountryName($country = 'us') {
* TRUE if the file was found and loaded, FALSE otherwise.
*/
public function getCountry($country) {
// Implements location_load_country().
$this->setStdCountryCode($country);
static::locationStandardizeCountryCode($country);

// @todo 8.x-2.x - convert to country plugins. Or even configs.
$file = DRUPAL_ROOT . '/' . drupal_get_path('module', 'dmaps') . '/files/supported_countries/location.' . $country . '.inc';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* @file
* Contains \Drupal\dmaps\Tests\Form\GeocodingOptionsFormTest.
*/

namespace Drupal\dmaps\Tests\Form;

use Drupal\simpletest\WebTestBase;

/**
* Tests the geocoding options form.
*
* @group dmaps
*/
class GeocodingOptionsFormTest extends WebTestBase {

/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['dmaps'];

/**
* A user that has permission to administer site configuration.
*
* @var \Drupal\user\UserInterface
*/
protected $web_user;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

$this->web_user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->web_user);
}

/**
* Testing Geocoding Options form for Dmaps module.
*
* @throws \Exception
*/
function testFormPage() {
$this->drupalGet('/admin/config/content/location/geocoding');
$edit = ['countries[af][geocode_af]' => 'google'];
$this->drupalPostForm('/admin/config/content/location/geocoding', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'));

// $this->assertLink(t('Configure parameters'));
// $this->drupalGet('/admin/config/content/location/geocoding/af/google');
// $this->assertFieldByName('location_geocode_google_apikey', NULL, t('Google Geocoding API Server Key'));
// $this->assertFieldByName('location_geocode_google_delay', NULL, t('Delay between geocoding requests (is milliseconds)'));
// $this->assertFieldByName('location_geocode_af_google_accuracy_code', 3, t('Google Maps Geocoding Accuracy for af'));
// $edit = [
// 'location_geocode_google_apikey' => $this->randomMachineName(),
// 'location_geocode_google_delay' => 500,
// 'location_geocode_af_google_accuracy_code' => 3,
// ];
// $this->drupalPostForm('/admin/config/content/location/geocoding/af/google', $edit, t('Save configuration'));
// $this->assertText(t('The configuration options have been saved.'));
}


}
56 changes: 0 additions & 56 deletions d8/modules/custom/dmaps/src/Tests/Form/UtilFormTest.php

This file was deleted.

0 comments on commit 601e3fd

Please sign in to comment.