Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"minimum-stability": "dev",
"require": {
"drupal/webform": "^6.0",
"drupal/geocoder": "^3.20"
"drupal/geocoder": "^3.20",
"localgovdrupal/localgov_os_places_geocoder_provider": "1.x-dev"
}
}
20 changes: 20 additions & 0 deletions config/schema/localgov_forms.geocoder.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
geocoder_provider.configuration.localgov_os_places:
type: geocoder_provider_configuration
label: 'Localgov OS Places arguments'
mapping:
genericAddressQueryUrl:
type: string
label: 'Street address-based query URL'
description: 'URL of the OS Places API server for street address-based query.'
postcodeQueryUrl:
type: string
label: 'Postcode-based query URL'
description: 'URL of the OS Places API server for postcode-based query.'
apiKey:
type: string
label: 'API key'
description: 'Value of the API key'
userAgent:
type: string
label: 'User agent'
description: 'Value of the User-Agent header'
6 changes: 2 additions & 4 deletions js/address_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@

if (drupalSettings.centralHub.selectedAddress) {
var addressSelected = drupalSettings.centralHub.selectedAddress;
// Change to use namespaced class for setting address.
// @See DRUP-1184.
central_hub_webfrom_address_entry.find('input.js-localgov-forms-webform-uk-address--address-1').val($.trim(addressSelected.flat + ' ' + addressSelected.house));
central_hub_webfrom_address_entry.find('input.js-localgov-forms-webform-uk-address--address-2').val(addressSelected.street);
central_hub_webfrom_address_entry.find('input.js-localgov-forms-webform-uk-address--address-1').val(addressSelected.line1);
central_hub_webfrom_address_entry.find('input.js-localgov-forms-webform-uk-address--address-2').val(addressSelected.line2);
central_hub_webfrom_address_entry.find('input.js-localgov-forms-webform-uk-address--town-city').val(addressSelected.town);
central_hub_webfrom_address_entry.find('input.js-localgov-forms-webform-uk-address--postcode').val(addressSelected.postcode);

Expand Down
72 changes: 51 additions & 21 deletions src/AddressLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Drupal\localgov_forms;

use Geocoder\Location;
use Drupal\localgov_forms\Geocoder\Model\LocalgovAddressInterface;
use LocalgovDrupal\OsPlacesGeocoder\Model\LocationUprnInterface;

/**
* Address lookup service.
Expand Down Expand Up @@ -52,28 +52,42 @@ public static function toSearchStr(array $param) :string {
*
* UPRN is used as the unique id for each address record. For addresses
* without the UPRN property, we use "(lat,lon)" as the unique id.
*
* When no flat number or organisation name or building name is provided, the
* street address is used as the first line of the address. Otherwise the
* street address is used as the second line.
*/
public static function reformat(Location $addr) :array {

$unique_id = '';
$display_name = '';
$uprn = '';
$flat = '';
$unique_id = '';
$display_name = '';
$uprn = '';
$flat = '';
$house_name = '';
$org = '';
$latitude = 0;
$longitude = 0;

$street_number = $addr->getStreetNumber();
$street_name = $addr->getStreetName();
$locality = $addr->getLocality();
$postcode = $addr->getPostalCode();
$country_name = $addr->getCountry()->getName();
$country_code = $addr->getCountry()->getCode();
$latitude = $addr->getCoordinates()->getLatitude();
$longitude = $addr->getCoordinates()->getLongitude();

$is_lgd_address = $addr instanceof LocalgovAddressInterface;
if ($coordinate = $addr->getCoordinates()) {
$latitude = $coordinate->getLatitude();
$longitude = $coordinate->getLongitude();
}

$is_lgd_address = $addr instanceof LocationUprnInterface;
if ($is_lgd_address) {
$uprn = $addr->getUprn();
$unique_id = $uprn;
$display_name = $addr->getDisplayName();
$flat = $addr->getFlat();
$house_name = $addr->getHouseName();
$org = $addr->getOrganisationName();
}
else {
$unique_id = sprintf("(%s,%s)", $latitude, $longitude);
Expand All @@ -84,21 +98,37 @@ public static function reformat(Location $addr) :array {
$display_name = implode(', ', array_filter($display_name_parts));
}

return [
'name' => $unique_id,
'uprn' => $uprn,
'display' => $display_name,
'street' => $street_name,
'flat' => $flat,
'house' => $street_number,
'town' => $locality,
'postcode' => $postcode,
$address = [
'name' => $unique_id ?? '',
'uprn' => $uprn ?? '',
'display' => $display_name ?? '',
'street' => implode(' ', array_filter([
$street_number, $street_name,
])),
'flat' => $flat ?? '',
'house' => implode(', ', array_filter([$org, $house_name])),
'town' => $locality ?? '',
'postcode' => $postcode ?? '',
'lat' => $latitude ?? '',
'lng' => $longitude ?? '',
'country' => $country_name ?? '',
'country_code' => $country_code ?? '',
'line1' => '',
'line2' => '',
'src' => $addr->getProvidedBy(),
'lat' => $latitude,
'lng' => $longitude,
'country' => $country_name,
'country_code' => $country_code,
];

if (empty($address['flat']) && empty($address['house'])) {
$address['line1'] = $address['street'];
}
else {
$address['line1'] = implode(', ', array_filter([
$address['flat'], $address['house'],
]));
$address['line2'] = $address['street'];
}

return $address;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Element/LocalgovWebformUKAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static function validateWebformComposite(&$element, FormStateInterface $f

// Get the search string and selected value.
$search_string = $value['address_lookup']['address_search']['address_searchstring'];
$selected = $value['address_lookup']['address_select']['address_select_list'];
$selected = $value['address_lookup']['address_select']['address_select_list'] ?? [];

// Check to see if there are values in the address element form.
$has_address_values = FALSE;
Expand Down
97 changes: 0 additions & 97 deletions src/Geocoder/Model/LocalgovAddress.php

This file was deleted.

31 changes: 0 additions & 31 deletions src/Geocoder/Model/LocalgovAddressInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Geocoder\Provider\Provider as ProviderInterface;
use Drupal\localgov_forms\Geocoder\Model\LocalgovAddress;
use LocalgovDrupal\OsPlacesGeocoder\Model\UprnAddress;

/**
* A Mock PHP Geocoder provider.
*
* Generates a collection of LocalgovAddress instances for automated testing
* Generates a collection of UprnAddress instances for automated testing
* purposes.
*/
class LocalgovMockGeocoder implements ProviderInterface {
Expand All @@ -40,18 +40,18 @@ public function geocodeQuery(GeocodeQuery $query) :LocationCollectionInterface {
$is_bhcc_hq = !strcasecmp($search_string, 'BN1 1JE');

if ($is_bhcc_hq) {
$results[] = LocalgovAddress::createFromArray([
$results[] = UprnAddress::createFromArray([
'providedBy' => $this->getName(),
'streetNumber' => 'Bartholomew House',
'org' => 'Brighton & Hove City Council',
'houseName' => 'Bartholomew House',
'streetNumber' => NULL,
'streetName' => 'Bartholomew Square',
'flat' => '',
'locality' => 'Brighton',
'postalCode' => 'BN1 1JE',
'county' => 'East Sussex',
'country' => 'United Kingdom',
'countryCode' => 'GB',
'display' => 'Bartholomew House, Bartholomew Square, Brighton, BN1 1JE',
'formattedAddress' => 'Bartholomew House, Bartholomew Square, Brighton, BN1 1JE',
'display' => 'Brighton & Hove City Council, Bartholomew House, Bartholomew Square, Brighton, BN1 1JE',
'latitude' => '-0.1409790',
'longitude' => '50.8208609',
'easting' => '531044',
Expand Down
4 changes: 2 additions & 2 deletions tests/src/FunctionalJavascript/GeocoderAddressLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class GeocoderAddressLookup extends WebDriverTestBase {
*
* @var array
*/
protected $defaultTheme = 'localgov_base';
protected $defaultTheme = 'stark';

/**
* Test for postcode-based address lookup.
Expand Down Expand Up @@ -66,7 +66,7 @@ public function testAddressLookup() {
$postcode_textfield = $page->find('css', '#edit-address-postcode');
$this->assertNotEmpty($postcode_textfield);

$this->assertEquals('Bartholomew House', $address1_textfield->getValue());
$this->assertEquals('Brighton & Hove City Council, Bartholomew House', $address1_textfield->getValue());
$this->assertEquals('Bartholomew Square', $address2_textfield->getValue());
$this->assertEquals('Brighton', $town_textfield->getValue());
$this->assertEquals('BN1 1JE', $postcode_textfield->getValue());
Expand Down