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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ Provides additional configuration, styling and components for the Drupal Webform

* LocalGov Forms Date - A date input field based on the [GDS Date Input pattern](https://design-system.service.gov.uk/components/date-input/)
* LocalGov address lookup - Webform element with a configurable address lookup backend. Geocoder plugins act as backends.

## Dependencies
The geocoder-php/nominatim-provider package is necessary to run automated tests:
```
$ composer require --dev geocoder-php/nominatim-provider
```

The localgovdrupal/localgov_geo and localgovdrupal/localgov_os_places_geocoder_provider packages are needed to use the Ordnance Survey Places API-based address lookup plugin. Once these packages are installed, the *Localgov OS Places* plugin will become available for selection from the Localgov address lookup element's configuration form.
20 changes: 0 additions & 20 deletions config/schema/localgov_forms.geocoder.schema.yml

This file was deleted.

15 changes: 15 additions & 0 deletions js/address_change.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
// Reset the form.
resetButton.click(function () {
resetAddressLookUpForm(addressLookupElement, $(this), 'hard');

// Hide manual entry button if asked for.
if (typeof drupalSettings.centralHub.isManualAddressEntryBtnAlwaysVisible !== 'undefined' && drupalSettings.centralHub.isManualAddressEntryBtnAlwaysVisible) {
var manualButton = addressLookupElement.parent().find('.js-manual-address');
manualButton.show();
}
});

// Append the reset button.
Expand All @@ -30,6 +36,7 @@

/**
* Reset Address Lookup Form.
*
* @param {jQuery} addressLookupElement
* Address lookup element.
* @param {jQuery} resetButton
Expand Down Expand Up @@ -149,6 +156,8 @@
var selectList = selectListContainer.find('.js-address-select');
var error = selectListContainer.find('.js-address-error');
var resetButton = addressLookupElement.find('.js-reset-address');
var manualButton = addressLookupElement.find('.js-manual-address');
var manualAddressContainer = addressLookupElement.find('+ .js-address-entry-container');
var ajaxProgressElement = addressLookupElement.find('.ajax-progress');

// This ajac was either not initiated by the address lookup,
Expand All @@ -157,6 +166,12 @@
return;
}

// Unhide the manual entry button only when manual address entry fields
// are hidden.
if (typeof settings.centralHub.isManualAddressEntryBtnAlwaysVisible !== 'undefined' && !settings.centralHub.isManualAddressEntryBtnAlwaysVisible && manualAddressContainer.is(':hidden')) {
manualButton.show();
}

// If there has been a search.
if ((selectList.find('option').length > 0 && searchElement.val().length > 0) || error.length > 0) {
searchButton.removeClass('js-searching');
Expand Down
28 changes: 19 additions & 9 deletions js/address_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
(function ($, Drupal) {

/**
* Add manual entry button
* Adds manual entry button.
*
* @param {jQuery} centralHubElement
* Centralhub address element.
*/
Expand All @@ -29,28 +30,35 @@
}

/**
* Hide manual address form
* Hide manual address form.
*
* @param {jQuery} centralHubElement
* Central hub address lookup element.
* @param {String} type
* 'soft' = Do not clear the address values.
* (used when an address is selected)
* 'hard' = Clear the address values.
* @param {Object} settings
* drupalSettings.
*/
function hideManualAddress(centralHubElement, type) {
function hideManualAddress(centralHubElement, type, settings) {
var manualAddressContainer = centralHubElement.find('.js-address-entry-container');
var manualButton = centralHubElement.find('.js-manual-address');
var addressSelectContainer = centralHubElement.find('.js-address-select-container');
manualAddressContainer.addClass('hidden');
if (type == 'hard') {

if (type == 'hard') {
// Clear all values.
manualAddressContainer.find('input').val('');

// Trigger change events so UPRN and extra fields are cleared.
manualAddressContainer.find('input').first().trigger('change');
}
manualButton.removeClass('hidden');

// Hide manual entry button if asked for.
if (typeof settings.centralHub.isManualAddressEntryBtnAlwaysVisible !== 'undefined' && !settings.centralHub.isManualAddressEntryBtnAlwaysVisible) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probbably remove reference to CentralHub at some point. :-)

manualButton.hide();
}
}

/**
Expand All @@ -67,7 +75,7 @@
var addressError = addressSelectContainer.find('.js-address-error');
manualAddressContainer.removeClass('hidden');
// manualAddressContainer.find('input').val('');
manualButton.addClass('hidden');
manualButton.hide();
// addressSelectContainer.addClass('hidden');
// addressSelect.val('0');
// Clear the search element when entering a manual address.
Expand Down Expand Up @@ -171,7 +179,7 @@
showManualAddress(centralHubElement);
} else if ($(this).val() == 0) {
// If choosing the empty option, clear out the address fields.
hideManualAddress(centralHubElement, 'hard');
hideManualAddress(centralHubElement, 'hard', drupalSettings);
}
};

Expand Down Expand Up @@ -202,15 +210,17 @@
$('.js-webform-type-localgov-webform-uk-address', context).once('localgov-address-webform').each(function () {
var centralHubElement = $(this);
addManualEntryButton(centralHubElement);

// Hide the manual address element, if it has no values.
if (!isManualAddressEntered(centralHubElement)) {
hideManualAddress(centralHubElement, 'soft');
hideManualAddress(centralHubElement, 'soft', settings);
}

// centralHubElement.find('.js-address-searchstring').change(function() {
// hideManualAddress(centralHubElement, 'hard');
// });
centralHubElement.find('.js-reset-address').click(function () {
hideManualAddress(centralHubElement, 'hard');
hideManualAddress(centralHubElement, 'hard', settings);
});
hideAddressSearchErrors(centralHubElement);
});
Expand Down
1 change: 1 addition & 0 deletions src/Element/AddressLookupElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function getInfo() {
'#address_search_description' => '',
'#address_select_title' => '',
'#geocoder_plugins' => [],
'#always_display_manual_address_entry_btn' => 'yes',
];
}

Expand Down
2 changes: 2 additions & 0 deletions src/Element/LocalgovWebformUKAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static function getCompositeElements(array $element) {
'#address_search_description' => $element['#address_search_description'] ?? NULL,
'#address_select_title' => $element['#address_select_title'] ?? NULL,
'#geocoder_plugins' => $element['#geocoder_plugins'] ?? [],
'#always_display_manual_address_entry_btn' => $element['#always_display_manual_address_entry_btn'] ?? 'yes',
];

$elements['address_entry'] = [
Expand Down Expand Up @@ -86,6 +87,7 @@ public static function getCompositeElements(array $element) {
}

$elements['#attached']['library'][] = 'localgov_forms/localgov_forms.address_select';
$elements['#attached']['drupalSettings']['centralHub']['isManualAddressEntryBtnAlwaysVisible'] = isset($element['#always_display_manual_address_entry_btn']) ? ($element['#always_display_manual_address_entry_btn'] === 'yes') : TRUE;

return $elements;
}
Expand Down
21 changes: 19 additions & 2 deletions src/Plugin/WebformElement/LocalgovWebformUKAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@
class LocalgovWebformUKAddress extends WebformUKAddress {

/**
* Declares our geocoder_plugins property.
* Declares our properties.
*
* Configurable properties:
* - geocoder_plugins
* - always_display_manual_address_entry_btn.
*
* {@inheritdoc}
*/
protected function defineDefaultProperties() {

$parent_properties = parent::defineDefaultProperties();
$parent_properties['geocoder_plugins'] = [];
$parent_properties['always_display_manual_address_entry_btn'] = 'yes';

return $parent_properties;
}
Expand All @@ -61,7 +66,9 @@ public function preSave(array &$element, WebformSubmissionInterface $webform_sub
/**
* Webform element config form.
*
* Adds a settings for selecting Geocoder plugins for address lookup.
* Adds settings for:
* - Selecting Geocoder plugins for address lookup.
* - Deciding whether to display the manual address entry button at all times.
*
* {@inheritdoc}
*/
Expand All @@ -77,6 +84,16 @@ public function form(array $form, FormStateInterface $form_state) {
'#description' => $this->t('These plugins are used for address lookup. They are added from Configuration > System > Geocoder > Providers.'),
];

$parent_form['element']['always_display_manual_address_entry_btn'] = [
'#type' => 'radios',
'#title' => 'When to display the manual address entry button',
'#description' => $this->t('Either display at all times or only after an address search.'),
'#options' => [
'yes' => $this->t('Always'),
'no' => $this->t('After an address search'),
],
];

return $parent_form;
}

Expand Down
Loading