22
33namespace Drupal \os2forms_nemid \Plugin \WebformElement ;
44
5+ use Drupal \Component \Utility \NestedArray ;
56use 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