Skip to content

Commit 3310697

Browse files
authored
OS-244 #246 Adding Datafordeler address lookup (#327)
1 parent 149ad4a commit 3310697

17 files changed

Lines changed: 76 additions & 694 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa
3030
- git actions check
3131
- [PR-289](https://github.com/OS2Forms/os2forms/pull/289)
3232
Added required "Zoom control position" to map element
33+
- [#246](https://github.com/OS2Forms/os2forms/issues/246)
34+
Adding Datafordeler address lookup
3335
- [#251](https://github.com/OS2Forms/os2forms/issues/251)
3436
Updated Webform encrypt uninstall problem fix
3537

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(function ($, Drupal) {
2+
// Overrides splitValues() function found in core/misc/autocomplete.js for
3+
// DAWA elements. Danish addresses contain commas. Original splitValues()
4+
// method splits upon commas. Combined with default extractLastTerm this
5+
// results in unexpected behavior when using autocomplete with respect to
6+
// danish addresses. Inspired by
7+
// https://www.drupal.org/project/drupal/issues/2821181#comment-12012538.
8+
Drupal.autocomplete.splitValues = function (value) {
9+
// Check if the current autocomplete is for a focused DAWA address field
10+
if ($('.ui-autocomplete-input:focus').closest('.os2forms-dawa-address').length) {
11+
// For DAWA address fields, return the entire value as a single value
12+
return [ value.trim() ];
13+
}
14+
// For other fields, use the original behavior
15+
return Drupal.autocomplete.splitValues(value);
16+
};
17+
})(jQuery, Drupal);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dawa_address_autocomplete:
2+
version: 1.x
3+
js:
4+
js/dawa_address_autocomplete.js: {}
5+
dependencies:
6+
- core/jquery
7+
- core/drupal

modules/os2forms_dawa/os2forms_dawa.module

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* OS2Forms Address autocomplete functionality module.
66
*/
77

8+
use Drupal\Core\Form\FormStateInterface;
9+
810
/**
911
* Implements hook_webform_migrate_d7_webform_element_ELEMENT_TYPE_alter().
1012
*
@@ -26,3 +28,12 @@ function os2forms_dawa_webform_migrate_d7_webform_element_address_autocomp_alter
2628
function os2forms_dawa_webform_migrate_d7_webform_element_addrs_autocomp_l_alter(&$markup, $indent, array $element) {
2729
$markup .= "$indent '#type': os2forms_dawa_address_matrikula\n";
2830
}
31+
32+
/**
33+
* Implements hook_webform_element_alter().
34+
*/
35+
function os2forms_dawa_webform_element_alter(array &$element, FormStateInterface $form_state, array $context) {
36+
if ('os2forms_dawa_address' === $element['#type']) {
37+
$element['#attached']['library'][] = 'os2forms_dawa/dawa_address_autocomplete';
38+
}
39+
}

modules/os2forms_dawa/os2forms_dawa.services.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

modules/os2forms_dawa/src/Controller/DawaElementController.php

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Drupal\os2forms_dawa\Controller;
44

55
use Drupal\Core\Controller\ControllerBase;
6-
use Drupal\os2forms_dawa\Service\DawaService;
6+
use Drupal\os2web_datalookup\Plugin\DataLookupManager;
77
use Symfony\Component\DependencyInjection\ContainerInterface;
88
use Symfony\Component\HttpFoundation\JsonResponse;
99
use Symfony\Component\HttpFoundation\Request;
@@ -14,28 +14,28 @@
1414
class DawaElementController extends ControllerBase {
1515

1616
/**
17-
* The DAWA service object.
17+
* Datafordeler address lookup.
1818
*
19-
* @var \Drupal\os2forms_dawa\Service\DawaService
19+
* @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DatafordelerAddressLookupInterface
2020
*/
21-
protected $dawaService;
21+
protected $datafordelerAddressLookup;
2222

2323
/**
2424
* Constructs a DawaElementController object.
2525
*
26-
* @param \Drupal\os2forms_dawa\Service\DawaService $os2forms_dawa_service
27-
* The DAWA service object.
26+
* @param \Drupal\os2web_datalookup\Plugin\DataLookupManager $dataLookupManager
27+
* Datalookup manager.
2828
*/
29-
public function __construct(DawaService $os2forms_dawa_service) {
30-
$this->dawaService = $os2forms_dawa_service;
29+
public function __construct(DataLookupManager $dataLookupManager) {
30+
$this->datafordelerAddressLookup = $dataLookupManager->createInstance('datafordeler_address_lookup');
3131
}
3232

3333
/**
3434
* {@inheritdoc}
3535
*/
3636
public static function create(ContainerInterface $container) {
3737
return new static(
38-
$container->get('os2forms_dawa.service')
38+
$container->get('plugin.manager.os2web_datalookup')
3939
);
4040
}
4141

@@ -61,18 +61,8 @@ public function autocomplete(Request $request, $element_type) {
6161
$matches = [];
6262

6363
// Get the matches based on the element type.
64-
switch ($element_type) {
65-
case 'os2forms_dawa_address':
66-
$matches = $this->dawaService->getAddressMatches($query);
67-
break;
68-
69-
case 'os2forms_dawa_block':
70-
$matches = $this->dawaService->getBlockMatches($query);
71-
break;
72-
73-
case 'os2forms_dawa_matrikula':
74-
$matches = $this->dawaService->getMatrikulaMatches($query);
75-
break;
64+
if ($element_type == 'os2forms_dawa_address') {
65+
$matches = $this->datafordelerAddressLookup->getAddressMatches($query);
7666
}
7767

7868
return new JsonResponse($matches);

modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,32 @@ public static function getCompositeElements(array $element) {
9696
private static function getMatrikulaOptions($addressValue, array $element) {
9797
$options = [];
9898

99-
/** @var \Drupal\os2forms_dawa\Service\DawaService $dawaService */
100-
$dawaService = \Drupal::service('os2forms_dawa.service');
99+
/** @var \Drupal\os2web_datalookup\Plugin\DataLookupManager $datalookupManager */
100+
$datalookupManager = \Drupal::service('plugin.manager.os2web_datalookup');
101101

102-
/** @var \Drupal\os2forms_dawa\Plugin\os2web\DataLookup\DatafordelerDataLookupInterface $datafordelerLookup */
103-
$datafordelerLookup = \Drupal::service('plugin.manager.os2web_datalookup')->createInstance('datafordeler_data_lookup');
102+
/** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DatafordelerAddressLookupInterface $addressLookup */
103+
$addressLookup = $datalookupManager->createInstance('datafordeler_address_lookup');
104+
105+
/** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DatafordelerMatrikulaLookupInterface $matrikulaLookup */
106+
$matrikulaLookup = $datalookupManager->createInstance('datafordeler_matrikula_lookup');
104107

105108
// Getting address.
106109
$addressParams = new ParameterBag();
107110
$addressParams->set('q', $addressValue);
108111
if (isset($element['#limit_by_municipality'])) {
109112
$addressParams->set('limit_by_municipality', $element['#limit_by_municipality']);
110113
}
111-
$address = $dawaService->getSingleAddress($addressParams);
114+
$address = $addressLookup->getSingleAddress($addressParams);
112115

113116
if ($address) {
114-
$addressAccessId = $address->getAccessAddressId();
117+
$addressHouseId = $address->getHouseId();
115118

116119
// Find matrikula list from the houseid (husnummer):
117-
$matrikulaId = $datafordelerLookup->getMatrikulaId($addressAccessId);
120+
$matrikulaId = $matrikulaLookup->getMatrikulaId($addressHouseId);
118121

119122
// Find Matrikula entries from matrikulas ID.
120123
if ($matrikulaId) {
121-
$matrikulaEnties = $datafordelerLookup->getMatrikulaEntries($matrikulaId);
124+
$matrikulaEnties = $matrikulaLookup->getMatrikulaEntries($matrikulaId);
122125
foreach ($matrikulaEnties as $matrikula) {
123126
$matrikulaOption = $matrikula->getMatrikulaNumber() . ' ' . $matrikula->getOwnershipName();
124127

modules/os2forms_dawa/src/Element/DawaElementBase.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,15 @@ public static function validateDawaElementBase(&$element, FormStateInterface $fo
3434
}
3535

3636
if (!empty($value)) {
37-
/** @var \Drupal\os2forms_dawa\Service\DawaService $dawaService*/
38-
$dawaService = \Drupal::service('os2forms_dawa.service');
37+
$matches = [];
3938

40-
$element_type = $element['#type'];
39+
if ($element['#type'] == 'os2forms_dawa_address') {
40+
/** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DatafordelerAddressLookupInterface $datafordelerAddressLookup */
41+
$datafordelerAddressLookup = \Drupal::service('plugin.manager.os2web_datalookup')->createInstance('datafordeler_address_lookup');
4142

42-
$parameters = new ParameterBag($element['#autocomplete_route_parameters']);
43-
$parameters->set('q', $value);
44-
45-
switch ($element_type) {
46-
case 'os2forms_dawa_address':
47-
$matches = $dawaService->getAddressMatches($parameters);
48-
break;
49-
50-
case 'os2forms_dawa_block':
51-
$matches = $dawaService->getBlockMatches($parameters);
52-
break;
53-
54-
case 'os2forms_dawa_matrikula':
55-
$matches = $dawaService->getMatrikulaMatches($parameters);
56-
break;
43+
$parameters = new ParameterBag($element['#autocomplete_route_parameters']);
44+
$parameters->set('q', $value);
45+
$matches = $datafordelerAddressLookup->getAddressMatches($parameters);
5746
}
5847

5948
// Checking if the current value is within the list of the values from an

modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)