Skip to content

Commit c84a48c

Browse files
committed
OS-244 #246 Adding Datafordeler address lookup
1 parent d0301c6 commit c84a48c

12 files changed

Lines changed: 39 additions & 610 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa
3232
- git actions check
3333
- [PR-289](https://github.com/OS2Forms/os2forms/pull/289)
3434
Added required "Zoom control position" to map element
35+
- [#246](https://github.com/OS2Forms/os2forms/issues/246)
36+
Adding Datafordeler address lookup
3537

3638
## [5.0.0] 2025-11-18
3739

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: 10 additions & 7 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\DatafordelerMartikulaLookupInterface $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) {
114117
$addressAccessId = $address->getAccessAddressId();
115118

116119
// Find matrikula list from the houseid (husnummer):
117-
$matrikulaId = $datafordelerLookup->getMatrikulaId($addressAccessId);
120+
$matrikulaId = $matrikulaLookup->getMatrikulaId($addressAccessId);
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: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\Core\Form\FormStateInterface;
66
use Drupal\Core\Render\Element\Textfield;
7+
use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DatafordelerAddressLookupInterface;
78
use Symfony\Component\HttpFoundation\ParameterBag;
89

910
/**
@@ -34,26 +35,17 @@ public static function validateDawaElementBase(&$element, FormStateInterface $fo
3435
}
3536

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

40-
$element_type = $element['#type'];
41-
42-
$parameters = new ParameterBag($element['#autocomplete_route_parameters']);
43-
$parameters->set('q', $value);
40+
/** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DatafordelerAddressLookupInterface $datafordelerAddressLookup */
41+
$datafordelerAddressLookup = \Drupal::service('plugin.manager.os2web_datalookup')->createInstance('datafordeler_address_lookup');
4442

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;
43+
$element_type = $element['#type'];
5344

54-
case 'os2forms_dawa_matrikula':
55-
$matches = $dawaService->getMatrikulaMatches($parameters);
56-
break;
45+
if ($element_type == 'os2forms_dawa_address') {
46+
$parameters = new ParameterBag($element['#autocomplete_route_parameters']);
47+
$parameters->set('q', $value);
48+
$matches = $datafordelerAddressLookup->getAddressMatches($parameters);
5749
}
5850

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

modules/os2forms_dawa/src/Entity/DawaAddress.php

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

modules/os2forms_dawa/src/Plugin/WebformElement/DawaElementAddress.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public function form(array $form, FormStateInterface $form_state) {
5151
];
5252
$form['autocomplete']['limit_by_municipality'] = [
5353
'#type' => 'textfield',
54-
'#title' => $this->t('Limit by municipality (-es)'),
55-
'#pattern' => '^(\d{3},?)*$',
56-
'#description' => $this->t('CSV list of municipalities codes, what will limit the address lookup.'),
54+
'#title' => $this->t('Limit by municipality'),
55+
'#pattern' => '^(\d{4})$',
56+
'#description' => $this->t('Municipatity code, what will limit the address lookup. Single number, 4 digits, e.g. 0661'),
5757
];
5858

5959
return $form;

modules/os2forms_dawa/src/Plugin/WebformElement/DawaElementBlock.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*
1111
* @WebformElement(
1212
* id = "os2forms_dawa_block",
13-
* label = @Translation("DAWA Block (autocomplete)"),
14-
* description = @Translation("Provides a DAWA Block Autocomplete element."),
13+
* label = @Translation("DAWA Block (autocomplete) - DEPRECATED"),
14+
* description = @Translation("Provides a DAWA Block Autocomplete element. This element is deprecated due to the API being phased out. There is no provided alternative for this element."),
1515
* category = @Translation("DAWA"),
1616
* )
1717
*/

modules/os2forms_dawa/src/Plugin/WebformElement/DawaElementMatrikula.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*
1111
* @WebformElement(
1212
* id = "os2forms_dawa_matrikula",
13-
* label = @Translation("DAWA Matrikula (autocomplete)"),
14-
* description = @Translation("Provides a DAWA Matrikula Autocomplete element."),
13+
* label = @Translation("DAWA Matrikula (autocomplete) - DEPRECATED"),
14+
* description = @Translation("Provides a DAWA Matrikula Autocomplete element. This element is deprecated due to the API being phased out. There is no provided alternative for this element."),
1515
* category = @Translation("DAWA"),
1616
* )
1717
*/

0 commit comments

Comments
 (0)