Skip to content

Commit 5f6752b

Browse files
committed
Adding CPR fetch field
1 parent 01da744 commit 5f6752b

File tree

4 files changed

+216
-27
lines changed

4 files changed

+216
-27
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_nemid\Element;
4+
5+
use Drupal\webform\Element\WebformCompositeBase;
6+
7+
/**
8+
* Provides a 'os2forms_nemid_cpr_fetch_data'.
9+
*
10+
* @FormElement("os2forms_nemid_cpr_fetch_data")
11+
*/
12+
class NemidCprFetchData extends WebformCompositeBase {
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public static function getCompositeElements(array $element) {
18+
$elements = [];
19+
if ($element) {
20+
$elements['cpr_fetch_data_value'] = [
21+
'#type' => 'textfield',
22+
'#title' => $element['#title'],
23+
];
24+
25+
$elements['cpr_fetch_data_submit'] = [
26+
'#type' => 'button',
27+
'#value' => isset($element['#fetch_button_title']) ? $element['#fetch_button_title'] : t('Hent'),
28+
'#limit_validation_errors' => [
29+
[
30+
$element['#webform_key']
31+
]
32+
],
33+
];
34+
}
35+
36+
return $elements;
37+
}
38+
39+
}

modules/os2forms_nemid/src/Plugin/WebformElement/NemidCpr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @see \Drupal\os2forms_nemid\Plugin\NemidElementBase
1616
* @see \Drupal\os2forms_nemid\Element\NemidCpr
1717
*/
18-
class NemidCpr extends NemloginElementBase implements NemidElementPersonalInterface {
18+
class NemidCpr extends ServiceplatformenCprElementBase implements NemidElementPersonalInterface {
1919

2020
/**
2121
* {@inheritdoc}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_nemid\Plugin\WebformElement;
4+
5+
use Drupal\Core\Form\FormStateInterface;
6+
use Drupal\webform\Plugin\WebformElement\WebformCompositeBase;
7+
8+
/**
9+
* Provides a 'os2forms_nemid_cpr_fetch_data' element.
10+
*
11+
* @WebformElement(
12+
* id = "os2forms_nemid_cpr_fetch_data",
13+
* label = @Translation("NemID CPR Fetch data"),
14+
* description = @Translation("Provides a NemID CPR fetch data element."),
15+
* category = @Translation("NemID"),
16+
* composite = TRUE,
17+
* )
18+
*
19+
*/
20+
class NemidCprFetchData extends WebformCompositeBase {
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function getDefaultProperties() {
26+
$properties = [
27+
'fetch_button_title' => t('Hent'),
28+
] + parent::getDefaultProperties();
29+
return $properties;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function form(array $form, FormStateInterface $form_state) {
36+
$form = parent::form($form, $form_state);
37+
$form['cpr_fetch_data_fs'] = [
38+
'#type' => 'fieldset',
39+
'#title' => $this->t('CPR fetch data settings'),
40+
];
41+
$form['cpr_fetch_data_fs']['fetch_button_title'] = [
42+
'#type' => 'textfield',
43+
'#title' => $this->t('Fetch button title'),
44+
'#description' => $this->t('The text that will be used on the fetch button to submit inserted CPR and initialize person data lookup'),
45+
];
46+
47+
return $form;
48+
}
49+
}

modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php

Lines changed: 127 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\os2forms_nemid\Plugin\WebformElement;
44

5+
use Drupal\Component\Utility\NestedArray;
56
use Drupal\Core\Form\FormStateInterface;
67

78
/**
@@ -20,38 +21,38 @@ abstract class ServiceplatformenCprElementBase extends NemidElementBase {
2021
*/
2122
public function handleElementPrepopulate(array &$element, FormStateInterface &$form_state) {
2223
$prepopulateKey = $this->getPrepopulateFieldFieldKey();
23-
24-
// Fetch value from serviceplatforment CPR.
2524
$spCrpData = NULL;
2625

27-
if ($form_state->has('servicePlatformenCprData')) {
28-
$spCrpData = $form_state->get('servicePlatformenCprData');
29-
}
30-
else {
31-
// Making the request to the plugin, and storing the information on the
32-
// form, so that it's available on the next element within the same
33-
// webform render.
34-
35-
/** @var \Drupal\os2web_nemlogin\Service\AuthProviderService $authProviderService */
36-
$authProviderService = \Drupal::service('os2web_nemlogin.auth_provider');
37-
/** @var \Drupal\os2web_nemlogin\Plugin\AuthProviderInterface $plugin */
38-
$plugin = $authProviderService->getActivePlugin();
26+
// Handling CPR being changed/reset.
27+
if ($form_state->isRebuilding() && $this->isCprNumberTrigger($form_state)) {
28+
// Resetting the current field value - it fetch is successfull,
29+
// it will be filled later.
30+
$element['#value'] = NULL;
3931

40-
if ($plugin->isAuthenticated()) {
41-
$cpr = $plugin->fetchValue('cpr');
32+
$cpr = $this->getCprNumberValue($form_state);
4233

43-
$pluginManager = \Drupal::service('plugin.manager.os2web_datalookup');
44-
/** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\ServiceplatformenCPR $servicePlatformentCprPlugin */
45-
$servicePlatformentCprPlugin = $pluginManager->createInstance('serviceplatformen_cpr');
34+
// If another cprFetchData, resetting cached servicePlatformenCprData.
35+
if (strcmp($cpr, $form_state->get('nemidCprFetchData')) !== 0) {
36+
$storage = $form_state->getStorage();
37+
unset($storage['servicePlatformenCprData']);
38+
$form_state->setStorage($storage);
4639

47-
if ($servicePlatformentCprPlugin->isReady()) {
48-
$spCrpData = $servicePlatformentCprPlugin->getAddress($cpr);
49-
// Making composite field, address.
50-
$spCrpData['address'] = $spCrpData['road'] . ' ' . $spCrpData['road_no'] . ' ' . $spCrpData['floor'] . ' ' . $spCrpData['door'];
51-
52-
// Making composite field, city.
53-
$spCrpData['city'] = $spCrpData['zipcode'] . ' ' . $spCrpData['city'];
40+
// Saving the new CPR-number.
41+
$form_state->set('nemidCprFetchData', $cpr);
42+
}
43+
}
5444

45+
// Trying to fetch person data from cache.
46+
if ($form_state->has('servicePlatformenCprData')) {
47+
$spCrpData = $form_state->get('servicePlatformenCprData');
48+
}
49+
else {
50+
// Cached version does not exist.
51+
//
52+
// Making the request to the plugin, and storing the data, so that it's
53+
// available on the next element within the same webform render.
54+
if ($spCrpData = $this->fetchPersonData($form_state)) {
55+
if (isset($spCrpData['status']) && $spCrpData['status']) {
5556
$form_state->set('servicePlatformenCprData', $spCrpData);
5657
}
5758
}
@@ -65,4 +66,104 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f
6566
}
6667
}
6768

69+
/**
70+
* Makes request to serviceplatformen.
71+
*
72+
* @param \Drupal\Core\Form\FormStateInterface $form_state
73+
* Form state object.
74+
*
75+
* @return array|null
76+
* Company information or NULL if request could not be performed.
77+
*/
78+
private function fetchPersonData(FormStateInterface $form_state) {
79+
$spCrpData = NULL;
80+
81+
// 1. Getting CPR from Nemlogin.
82+
/** @var \Drupal\os2web_nemlogin\Service\AuthProviderService $authProviderService */
83+
$authProviderService = \Drupal::service('os2web_nemlogin.auth_provider');
84+
/** @var \Drupal\os2web_nemlogin\Plugin\AuthProviderInterface $plugin */
85+
$nemloginAuth = $authProviderService->getActivePlugin();
86+
if ($nemloginAuth->isAuthenticated()) {
87+
$cpr = $nemloginAuth->fetchValue('cpr');
88+
}
89+
// 2. Getting CPR from CPR fetch data field
90+
else {
91+
if ($form_state->isRebuilding() && $this->isCprNumberTrigger($form_state)) {
92+
$cpr = $this->getCprNumberValue($form_state);
93+
}
94+
}
95+
96+
if ($cpr) {
97+
$pluginManager = \Drupal::service('plugin.manager.os2web_datalookup');
98+
/** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\ServiceplatformenCPR $servicePlatformentCprPlugin */
99+
$servicePlatformentCprPlugin = $pluginManager->createInstance('serviceplatformen_cpr');
100+
101+
if ($servicePlatformentCprPlugin->isReady()) {
102+
$spCrpData = $servicePlatformentCprPlugin->getAddress($cpr);
103+
}
104+
105+
// Post fetch procedure - manipulating the address fields.
106+
if (isset($spCrpData['status']) && $spCrpData['status']) {
107+
// Making composite field, address.
108+
$spCrpData['address'] = $spCrpData['road'] . ' ' . $spCrpData['road_no'] . ' ' . $spCrpData['floor'] . ' ' . $spCrpData['door'];
109+
110+
// Making composite field, city.
111+
$spCrpData['city'] = $spCrpData['zipcode'] . ' ' . $spCrpData['city'];
112+
}
113+
}
114+
115+
return $spCrpData;
116+
}
117+
118+
/**
119+
* Checks if form rebuild trigger is CPR-number fetch button.
120+
*
121+
* @param \Drupal\Core\Form\FormStateInterface $form_state
122+
* Form state object.
123+
*
124+
* @return bool
125+
* TRUE or FALSE.
126+
*/
127+
public function isCprNumberTrigger(FormStateInterface $form_state) {
128+
if ($triggerElement = $form_state->getTriggeringElement()) {
129+
//Checking trigger element parent.
130+
$form_array = $form_state->getCompleteForm();
131+
$triggerElParents = $triggerElement['#array_parents'];
132+
133+
// Removing last element = current trigger elements.
134+
array_pop($triggerElParents);
135+
$parentElement = NestedArray::getValue($form_array, $triggerElParents);
136+
137+
// Checking if parent element is 'os2forms_nemid_cpr_fetch_data'
138+
if ($parentElement && isset($parentElement['#type']) && $parentElement['#type'] == 'os2forms_nemid_cpr_fetch_data') {
139+
return TRUE;
140+
}
141+
}
142+
143+
return FALSE;
144+
}
145+
146+
/**
147+
* Gets the value from CPR-number field.
148+
*
149+
* @param \Drupal\Core\Form\FormStateInterface $form_state
150+
* Form state object.
151+
*
152+
* @return string
153+
* P-Number value from the field.
154+
*/
155+
public function getCprNumberValue(FormStateInterface $form_state) {
156+
$triggerElement = $form_state->getTriggeringElement();
157+
158+
$pNumberParents = $triggerElement['#parents'];
159+
160+
// Removing last element = current trigger elements.
161+
array_pop($pNumberParents);
162+
163+
array_push($pNumberParents, 'cpr_fetch_data_value');
164+
$pNumber = $form_state->getValue($pNumberParents);
165+
166+
return $pNumber;
167+
}
168+
68169
}

0 commit comments

Comments
 (0)