From 306b988b612ed98e4f359b3510ac48fa2b305d38 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Fri, 22 Dec 2023 10:32:49 +0200 Subject: [PATCH 01/32] S2FRMS-40 adding child select fields --- CHANGELOG.md | 2 + .../src/Element/MitidChildAddress.php | 64 +++++++++++++ .../src/Element/MitidChildApartmentNr.php | 64 +++++++++++++ .../src/Element/MitidChildCity.php | 64 +++++++++++++ .../src/Element/MitidChildCoaddress.php | 64 +++++++++++++ .../src/Element/MitidChildCpr.php | 64 +++++++++++++ .../src/Element/MitidChildFloor.php | 64 +++++++++++++ .../src/Element/MitidChildHouseNr.php | 64 +++++++++++++ .../src/Element/MitidChildKommunekode.php | 64 +++++++++++++ .../src/Element/MitidChildName.php | 64 +++++++++++++ .../src/Element/MitidChildPostalCode.php | 64 +++++++++++++ .../src/Element/MitidChildStreet.php | 64 +++++++++++++ .../MitidChildrenSelectAjaxBehaviour.php | 92 +++++++++++++++++++ .../src/Element/NemidChildrenRadios.php | 15 +++ .../src/Element/NemidChildrenSelect.php | 16 ++++ .../WebformElement/MitidChildAddress.php | 19 ++++ .../WebformElement/MitidChildApartmentNr.php | 19 ++++ .../Plugin/WebformElement/MitidChildCity.php | 19 ++++ .../WebformElement/MitidChildCoaddress.php | 19 ++++ .../Plugin/WebformElement/MitidChildCpr.php | 19 ++++ .../WebformElement/MitidChildElementBase.php | 60 ++++++++++++ .../Plugin/WebformElement/MitidChildFloor.php | 19 ++++ .../WebformElement/MitidChildHouseNr.php | 19 ++++ .../WebformElement/MitidChildKommunekode.php | 19 ++++ .../Plugin/WebformElement/MitidChildName.php | 19 ++++ .../WebformElement/MitidChildPostalCode.php | 19 ++++ .../WebformElement/MitidChildStreet.php | 19 ++++ .../WebformElement/NemidChildrenRadios.php | 20 +--- .../WebformElement/NemidChildrenSelect.php | 20 +--- .../src/Service/FormsHelper.php | 2 +- 30 files changed, 1105 insertions(+), 35 deletions(-) create mode 100644 modules/os2forms_nemid/src/Element/MitidChildAddress.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildApartmentNr.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildCity.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildCoaddress.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildCpr.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildFloor.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildHouseNr.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildKommunekode.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildName.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildPostalCode.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildStreet.php create mode 100644 modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildAddress.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildApartmentNr.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCity.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCoaddress.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCpr.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildElementBase.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildFloor.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildHouseNr.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildKommunekode.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildName.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildPostalCode.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildStreet.php diff --git a/CHANGELOG.md b/CHANGELOG.md index f4af33ef..8643264b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +- [S2FRMS-40] Adding child select autopopulate fields + ## [3.13.3] 2023-12-05 - [#76](https://github.com/OS2Forms/os2forms/pull/76) diff --git a/modules/os2forms_nemid/src/Element/MitidChildAddress.php b/modules/os2forms_nemid/src/Element/MitidChildAddress.php new file mode 100644 index 00000000..f8daa7a6 --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildAddress.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildAddress'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildAddress'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildAddress'], + ], + '#theme' => 'input__os2forms_mitid_child_address', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_address' element. + */ + public static function processMitidChildAddress(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_address'. + */ + public static function validateMitidChildAddress(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildAddress(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-address']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildApartmentNr.php b/modules/os2forms_nemid/src/Element/MitidChildApartmentNr.php new file mode 100644 index 00000000..65cecc7b --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildApartmentNr.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildApartmentNr'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildApartmentNr'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildApartmentNr'], + ], + '#theme' => 'input__os2forms_mitid_child_apartmentNr', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_apartmentNr' element. + */ + public static function processMitidChildApartmentNr(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_apartmentNr'. + */ + public static function validateMitidChildApartmentNr(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildApartmentNr(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-apartment-nr']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildCity.php b/modules/os2forms_nemid/src/Element/MitidChildCity.php new file mode 100644 index 00000000..8ce47265 --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildCity.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildCity'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildCity'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildCity'], + ], + '#theme' => 'input__os2forms_mitid_child_city', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_city' element. + */ + public static function processMitidChildCity(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_city'. + */ + public static function validateMitidChildCity(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildCity(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-city']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildCoaddress.php b/modules/os2forms_nemid/src/Element/MitidChildCoaddress.php new file mode 100644 index 00000000..87fd6be0 --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildCoaddress.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildCoaddress'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildCoaddress'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildCoaddress'], + ], + '#theme' => 'input__os2forms_mitid_child_coaddress', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_coaddress' element. + */ + public static function processMitidChildCoaddress(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_coaddress'. + */ + public static function validateMitidChildCoaddress(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildCoaddress(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-coaddress']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildCpr.php b/modules/os2forms_nemid/src/Element/MitidChildCpr.php new file mode 100644 index 00000000..1ea9c566 --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildCpr.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildCpr'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildCpr'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildCpr'], + ], + '#theme' => 'input__os2forms_mitid_child_cpr', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_cpr' element. + */ + public static function processMitidChildCpr(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_cpr'. + */ + public static function validateMitidChildCpr(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildCpr(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-cpr']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildFloor.php b/modules/os2forms_nemid/src/Element/MitidChildFloor.php new file mode 100644 index 00000000..49e14cf4 --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildFloor.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildFloor'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildFloor'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildFloor'], + ], + '#theme' => 'input__os2forms_mitid_child_floor', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_floor' element. + */ + public static function processMitidChildFloor(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_floor'. + */ + public static function validateMitidChildFloor(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildFloor(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-floor']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildHouseNr.php b/modules/os2forms_nemid/src/Element/MitidChildHouseNr.php new file mode 100644 index 00000000..f07e3d6a --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildHouseNr.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildHouseNr'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildHouseNr'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildHouseNr'], + ], + '#theme' => 'input__os2forms_mitid_child_house_nr', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_house_nr' element. + */ + public static function processMitidChildHouseNr(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_house_nr'. + */ + public static function validateMitidChildHouseNr(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildHouseNr(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-house-nr']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildKommunekode.php b/modules/os2forms_nemid/src/Element/MitidChildKommunekode.php new file mode 100644 index 00000000..9b773624 --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildKommunekode.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildKommunekode'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildKommunekode'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildKommunekode'], + ], + '#theme' => 'input__os2forms_mitid_child_kommunekode', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_kommunekode' element. + */ + public static function processMitidChildKommunekode(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_kommunekode'. + */ + public static function validateMitidChildKommunekode(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildKommunekode(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-kommunekode']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildName.php b/modules/os2forms_nemid/src/Element/MitidChildName.php new file mode 100644 index 00000000..4d37838f --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildName.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildName'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildName'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildName'], + ], + '#theme' => 'input__os2forms_mitid_child_name', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_name' element. + */ + public static function processMitidChildName(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_name'. + */ + public static function validateMitidChildName(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildName(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-name']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildPostalCode.php b/modules/os2forms_nemid/src/Element/MitidChildPostalCode.php new file mode 100644 index 00000000..d78da623 --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildPostalCode.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildPostalCode'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildPostalCode'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildPostalCode'], + ], + '#theme' => 'input__os2forms_mitid_child_postal_code', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_postal_code' element. + */ + public static function processMitidChildPostalCode(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_postal_code'. + */ + public static function validateMitidChildPostalCode(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildPostalCode(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-postal-code']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildStreet.php b/modules/os2forms_nemid/src/Element/MitidChildStreet.php new file mode 100644 index 00000000..b5f68beb --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildStreet.php @@ -0,0 +1,64 @@ + [ + [$class, 'processMitidChildStreet'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildStreet'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildStreet'], + ], + '#theme' => 'input__os2forms_mitid_child_street', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_street' element. + */ + public static function processMitidChildStreet(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_street'. + */ + public static function validateMitidChildStreet(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildStreet(array $element) { + $element = parent::prerenderNemidElementBase($element); + static::setAttributes($element, ['form-text', 'os2forms-mitid-child-street']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php new file mode 100644 index 00000000..9be13c93 --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php @@ -0,0 +1,92 @@ +getTriggeringElement(); + $triggerName = $trigger['#name']; + + $childCpr = $form_state->getValue($triggerName); + + /** @var \Drupal\os2web_datalookup\Plugin\DataLookupManager $os2web_datalookup_plugins */ + $os2web_datalookup_plugins = \Drupal::service('plugin.manager.os2web_datalookup'); + + /** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterfaceCpr $cprPlugin */ + $cprPlugin = $os2web_datalookup_plugins->createDefaultInstanceByGroup('cpr_lookup'); + + if ($cprPlugin->isReady()) { + $cprLookupResult = $cprPlugin->lookup($childCpr); + } + + /** @var \Drupal\webform\WebformSubmissionForm $webformSubmissionForm */ + $webformSubmissionForm = $form_state->getFormObject(); + /** @var \Drupal\webform\WebformSubmissionInterface $webformSubmission */ + $webformSubmission = $webformSubmissionForm->getEntity(); + $webform = $webformSubmission->getWebform(); + $elements = $webform->getElementsDecodedAndFlattened(); + + $response = new AjaxResponse(); + + foreach ($elements as $element) { + if(isset($element['#type'])) { + switch ($element['#type']) { + case 'os2forms_mitid_child_name': + $element['#value'] = $cprLookupResult->getName(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-name', $element)); + break; + case 'os2forms_mitid_child_cpr': + $element['#value'] = $cprLookupResult->getCpr(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-cpr', $element)); + break; + case 'os2forms_mitid_child_address': + $element['#value'] = $cprLookupResult->getAddress(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-address', $element)); + break; + case 'os2forms_mitid_child_apartment_nr': + $element['#value'] = $cprLookupResult->getApartmentNr(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-apartment_nr', $element)); + break; + case 'os2forms_mitid_child_city': + $element['#value'] = $cprLookupResult->getCity(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-city', $element)); + break; + case 'os2forms_mitid_child_coaddress': + $element['#value'] = $cprLookupResult->getCoName(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-coaddress', $element)); + break; + case 'os2forms_mitid_child_floor': + $element['#value'] = $cprLookupResult->getFloor(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-floor', $element)); + break; + case 'os2forms_mitid_child_house_nr': + $element['#value'] = $cprLookupResult->getHouseNr(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-house-nr', $element)); + break; + case 'os2forms_mitid_child_kommunekode': + $element['#value'] = $cprLookupResult->getMunicipalityCode(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-kommunekode', $element)); + break; + case 'os2forms_mitid_child_postal_code': + $element['#value'] = $cprLookupResult->getPostalCode(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-postal-code', $element)); + break; + case 'os2forms_mitid_child_street': + $element['#value'] = $cprLookupResult->getStreet(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-street', $element)); + break; + } + } + } + + return $response; + } +} diff --git a/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php b/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php index 67379784..c6d303b9 100644 --- a/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php +++ b/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php @@ -17,4 +17,19 @@ */ class NemidChildrenRadios extends Radios { + /** + * {@inheritdoc} + */ + public function getInfo() { + return parent::getInfo() + [ + '#ajax' => [ + 'callback' => [MitidChildrenSelectAjaxBehaviour::class, 'mitidChildrenSelectAjax'], + 'event' => 'change', + 'progress' => [ + 'type' => 'throbber', + 'message' => $this->t('Please wait...'), + ], + ], + ]; + } } diff --git a/modules/os2forms_nemid/src/Element/NemidChildrenSelect.php b/modules/os2forms_nemid/src/Element/NemidChildrenSelect.php index 052dd6e1..567021f4 100644 --- a/modules/os2forms_nemid/src/Element/NemidChildrenSelect.php +++ b/modules/os2forms_nemid/src/Element/NemidChildrenSelect.php @@ -17,4 +17,20 @@ */ class NemidChildrenSelect extends Select { + /** + * {@inheritdoc} + */ + public function getInfo() { + return parent::getInfo() + [ + '#ajax' => [ + 'callback' => [MitidChildrenSelectAjaxBehaviour::class, 'mitidChildrenSelectAjax'], + 'event' => 'change', + 'progress' => [ + 'type' => 'throbber', + 'message' => $this->t('Please wait...'), + ], + ], + ]; + } + } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildAddress.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildAddress.php new file mode 100644 index 00000000..ace06e71 --- /dev/null +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildAddress.php @@ -0,0 +1,19 @@ + '', + 'size' => '', + 'minlength' => '', + 'maxlength' => '', + 'placeholder' => '', + 'readonly' => '', + ]; + + return $properties; + } + + /** + * {@inheritdoc} + */ + public function form(array $form, FormStateInterface $form_state) { + $form = parent::form($form, $form_state); + + // @see \Drupal\webform\Plugin\WebformElement\WebformEntityReferenceTrait::form + $element_properties = $form_state->get('element_properties'); + // If element is new, set readonly by default. + if (empty($element_properties['title'])) { + $form['form']['readonly']['#value'] = TRUE; + } + + // Here you can define and alter a webform element's properties UI. + // Form element property visibility and default values are defined via + // ::getDefaultProperties. + // + // @see \Drupal\webform\Plugin\WebformElementBase::form + // @see \Drupal\webform\Plugin\WebformElement\TextBase::form + return $form; + } + +} diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildFloor.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildFloor.php new file mode 100644 index 00000000..ec8371b6 --- /dev/null +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildFloor.php @@ -0,0 +1,19 @@ + '', 'options' => [], ] + parent::defineDefaultProperties(); return $properties; @@ -41,14 +40,6 @@ public function form(array $form, FormStateInterface $form_state) { $form['options']['options']['#required'] = FALSE; $form['options']['#access'] = FALSE; - $form['element']['cpr_output_type'] = [ - '#type' => 'radios', - '#options' => ['cpr' => $this->t('CPR'), 'name' => $this->t('Name')], - '#title' => $this - ->t('CPR output type'), - '#required' => TRUE, - ]; - return $form; } @@ -141,12 +132,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f if ($children = $cprLookupResult->getFieldValue($prepopulateKey)) { if (is_array($children) && !empty($children)) { foreach ($children as $child) { - if ($element['#cpr_output_type'] == 'cpr') { - $options[$child['cpr']] = $child['name']; - } - else { - $options[$child['name']] = $child['name']; - } + $options[$child['cpr']] = $child['name']; } } } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php index ec8ddefc..d2eec470 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php @@ -12,8 +12,8 @@ * * @WebformElement( * id = "os2forms_nemid_children_select", - * label = @Translation("NemID Children Select"), - * description = @Translation("Provides a NemID Children select element."), + * label = @Translation("MitID Children Select"), + * description = @Translation("Provides a MitID Children select element."), * category = @Translation("NemID"), * ) * @@ -27,7 +27,6 @@ class NemidChildrenSelect extends Select implements NemidElementPersonalInterfac */ protected function defineDefaultProperties() { $properties = [ - 'cpr_output_type' => '', 'options' => [], ] + parent::defineDefaultProperties(); return $properties; @@ -41,14 +40,6 @@ public function form(array $form, FormStateInterface $form_state) { $form['options']['options']['#required'] = FALSE; $form['options']['#access'] = FALSE; - $form['element']['cpr_output_type'] = [ - '#type' => 'radios', - '#options' => ['cpr' => $this->t('CPR'), 'name' => $this->t('Name')], - '#title' => $this - ->t('CPR output type'), - '#required' => TRUE, - ]; - return $form; } @@ -141,12 +132,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f if ($children = $cprLookupResult->getFieldValue($prepopulateKey)) { if (is_array($children) && !empty($children)) { foreach ($children as $child) { - if ($element['#cpr_output_type'] == 'cpr') { - $options[$child['cpr']] = $child['name']; - } - else { - $options[$child['name']] = $child['name']; - } + $options[$child['cpr']] = $child['name']; } } } diff --git a/modules/os2forms_nemid/src/Service/FormsHelper.php b/modules/os2forms_nemid/src/Service/FormsHelper.php index e271d552..f1fecd60 100644 --- a/modules/os2forms_nemid/src/Service/FormsHelper.php +++ b/modules/os2forms_nemid/src/Service/FormsHelper.php @@ -102,7 +102,7 @@ public function retrieveCprLookupResult(FormStateInterface $form_state) { * * @throws \Drupal\Component\Plugin\Exception\PluginException */ - private function lookupPersonData(FormStateInterface $form_state) { + public function lookupPersonData(FormStateInterface $form_state) { $cprResult = new CprLookupResult(); $cpr = NULL; From 5518d4c4852fb125be32956b7a963a66d3070743 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Fri, 12 Jan 2024 13:16:52 +0200 Subject: [PATCH 02/32] S2FRMS-40 - Adding name&address protection text, fixing populate --- .../MitidChildrenSelectAjaxBehaviour.php | 40 +++++++++++++++++-- .../src/Plugin/WebformElement/NemidCpr.php | 7 ++++ .../ServiceplatformenCprElementBase.php | 19 +++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php index 9be13c93..bd826051 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php +++ b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php @@ -2,6 +2,7 @@ namespace Drupal\os2forms_nemid\Element; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\Form\FormStateInterface; @@ -32,15 +33,21 @@ public static function mitidChildrenSelectAjax(array &$form, FormStateInterface /** @var \Drupal\webform\WebformSubmissionInterface $webformSubmission */ $webformSubmission = $webformSubmissionForm->getEntity(); $webform = $webformSubmission->getWebform(); - $elements = $webform->getElementsDecodedAndFlattened(); + $elementsFlattened = $webform->getElementsInitializedAndFlattened(); $response = new AjaxResponse(); - foreach ($elements as $element) { - if(isset($element['#type'])) { + foreach ($elementsFlattened as $flattenedElement) { + if (isset($flattenedElement['#type'])) { + $parents = $flattenedElement['#webform_parents']; + $element = NestedArray::getValue($form['elements'], $parents); + switch ($element['#type']) { case 'os2forms_mitid_child_name': $element['#value'] = $cprLookupResult->getName(); + if (!empty($element['#value'])) { + $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-name', $element)); break; case 'os2forms_mitid_child_cpr': @@ -49,38 +56,65 @@ public static function mitidChildrenSelectAjax(array &$form, FormStateInterface break; case 'os2forms_mitid_child_address': $element['#value'] = $cprLookupResult->getAddress(); + if (!empty($element['#value'])) { + $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-address', $element)); break; case 'os2forms_mitid_child_apartment_nr': $element['#value'] = $cprLookupResult->getApartmentNr(); + if (!empty($element['#value'])) { + $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-apartment_nr', $element)); break; case 'os2forms_mitid_child_city': $element['#value'] = $cprLookupResult->getCity(); + if (!empty($element['#value'])) { + $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-city', $element)); break; case 'os2forms_mitid_child_coaddress': $element['#value'] = $cprLookupResult->getCoName(); + if (!empty($element['#value'])) { + $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-coaddress', $element)); break; case 'os2forms_mitid_child_floor': $element['#value'] = $cprLookupResult->getFloor(); + if (!empty($element['#value'])) { + $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-floor', $element)); break; case 'os2forms_mitid_child_house_nr': $element['#value'] = $cprLookupResult->getHouseNr(); + if (!empty($element['#value'])) { + $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-house-nr', $element)); break; case 'os2forms_mitid_child_kommunekode': $element['#value'] = $cprLookupResult->getMunicipalityCode(); + if (!empty($element['#value'])) { + $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-kommunekode', $element)); break; case 'os2forms_mitid_child_postal_code': $element['#value'] = $cprLookupResult->getPostalCode(); + if (!empty($element['#value'])) { + $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-postal-code', $element)); break; case 'os2forms_mitid_child_street': $element['#value'] = $cprLookupResult->getStreet(); + if (!empty($element['#value'])) { + $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-street', $element)); break; } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidCpr.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidCpr.php index 03a27154..9abd1f9e 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidCpr.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidCpr.php @@ -26,4 +26,11 @@ public function getPrepopulateFieldFieldKey(array &$element) { return CprLookupResult::CPR; } + /** + * {@inheritdoc} + */ + public function appendNameAddressProtectedText(CprLookupResult $cprLookupResult) { + return ''; + } + } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php b/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php index 9338107d..34f48522 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php @@ -3,6 +3,7 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; use Drupal\Core\Form\FormStateInterface; +use Drupal\os2web_datalookup\LookupResult\CprLookupResult; /** * Provides a abstract ServicePlatformenCpr Element. @@ -27,8 +28,26 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f $prepopulateKey = $this->getPrepopulateFieldFieldKey($element); if ($value = $cprLookupResult->getFieldValue($prepopulateKey)) { $element['#value'] = $value; + + // Appending name and address protection. + if (!empty($value)) { + $element['#value'] .= $this->appendNameAddressProtectedText($cprLookupResult); + } } } } + /** + * Appends name and address protected text, if person has name/address protection. + * + * @param CprLookupResult $cprLookupResult + * Initialized CprLooupResult. + * + * @return string + * String " (Navne- og adressebeskyttelse)" or nothing. + */ + public function appendNameAddressProtectedText(CprLookupResult $cprLookupResult) { + return $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + } + } From d8ade8b854693850336c5c4af499f837e26adda7 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Tue, 16 Jan 2024 13:52:12 +0200 Subject: [PATCH 03/32] S2FRMS-40 - child select - name address protection --- .../src/Plugin/WebformElement/NemidChildrenRadios.php | 2 +- .../src/Plugin/WebformElement/NemidChildrenSelect.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php index 05bbedea..d871a11d 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php @@ -132,7 +132,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f if ($children = $cprLookupResult->getFieldValue($prepopulateKey)) { if (is_array($children) && !empty($children)) { foreach ($children as $child) { - $options[$child['cpr']] = $child['name']; + $options[$child['cpr']] = $child['name'] . ($child['nameAddressProtected'] ? ' (Navne- og adressebeskyttelse)' : '') ; } } } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php index d2eec470..eab8106e 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php @@ -132,7 +132,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f if ($children = $cprLookupResult->getFieldValue($prepopulateKey)) { if (is_array($children) && !empty($children)) { foreach ($children as $child) { - $options[$child['cpr']] = $child['name']; + $options[$child['cpr']] = $child['name'] . ($child['nameAddressProtected'] ? ' (Navne- og adressebeskyttelse)' : '') ; } } } From bff89d673e53ca8c56a8f78a7c622954f4d73108 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Sat, 20 Jan 2024 16:47:43 +0200 Subject: [PATCH 04/32] S2FRMS-40 - Adding other guardian element --- .../WebformHandler/WebformHandlerSF1601.php | 2 + .../src/Element/MitidChildOtherGuardian.php | 65 +++++++ .../MitidChildrenSelectAjaxBehaviour.php | 168 +++++++++--------- .../MitidChildOtherGuardian.php | 19 ++ .../src/Service/FormsHelper.php | 21 +++ 5 files changed, 187 insertions(+), 88 deletions(-) create mode 100644 modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php create mode 100644 modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php diff --git a/modules/os2forms_digital_post/src/Plugin/WebformHandler/WebformHandlerSF1601.php b/modules/os2forms_digital_post/src/Plugin/WebformHandler/WebformHandlerSF1601.php index 667368db..9da57179 100644 --- a/modules/os2forms_digital_post/src/Plugin/WebformHandler/WebformHandlerSF1601.php +++ b/modules/os2forms_digital_post/src/Plugin/WebformHandler/WebformHandlerSF1601.php @@ -217,12 +217,14 @@ private function getRecipientElements(): array { 'os2forms_nemid_company_cvr_fetch_data', 'os2forms_nemid_cpr', 'os2forms_person_lookup', + 'os2forms_mitid_child_other_guardian', // @todo Remove these when we remove the elements. 'cpr_element', 'cpr_value_element', 'cvr_element', 'cvr_value_element', ]; + $elements = array_filter( $elements, static function (array $element) use ($elementTypes) { diff --git a/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php b/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php new file mode 100644 index 00000000..faa8f386 --- /dev/null +++ b/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php @@ -0,0 +1,65 @@ + [ + [$class, 'processMitidChildOtherGuardian'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateMitidChildOtherGuardian'], + ], + '#pre_render' => [ + [$class, 'preRenderMitidChildOtherGuardian'], + ], + '#theme' => 'input__os2forms_mitid_child_other_guardian', + ]; + } + + /** + * Processes a 'os2forms_mitid_child_other_guardian' element. + */ + public static function processMitidChildOtherGuardian(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add and manipulate your element's properties and callbacks. + return $element; + } + + /** + * Webform element validation handler for #type 'os2forms_mitid_child_other_guardian'. + */ + public static function validateMitidChildOtherGuardian(&$element, FormStateInterface $form_state, &$complete_form) { + // Here you can add custom validation logic. + } + + /** + * {@inheritdoc} + */ + public static function preRenderMitidChildOtherGuardian(array $element) { + $element = parent::preRenderHidden($element); + static::setAttributes($element, ['os2forms-mitid-child-other-guardian']); + return $element; + } + +} diff --git a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php index bd826051..985593a6 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php +++ b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php @@ -6,6 +6,7 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\Form\FormStateInterface; +use Drupal\os2forms_nemid\Service\FormsHelper; /** * Provides a children select ajax behaviour for MitidChildrenSelect and MitidChildrenRadios. @@ -13,9 +14,11 @@ class MitidChildrenSelectAjaxBehaviour { public static function mitidChildrenSelectAjax(array &$form, FormStateInterface $form_state) { + $response = new AjaxResponse(); + + // Getting child CPR. $trigger = $form_state->getTriggeringElement(); $triggerName = $trigger['#name']; - $childCpr = $form_state->getValue($triggerName); /** @var \Drupal\os2web_datalookup\Plugin\DataLookupManager $os2web_datalookup_plugins */ @@ -26,97 +29,86 @@ public static function mitidChildrenSelectAjax(array &$form, FormStateInterface if ($cprPlugin->isReady()) { $cprLookupResult = $cprPlugin->lookup($childCpr); - } - /** @var \Drupal\webform\WebformSubmissionForm $webformSubmissionForm */ - $webformSubmissionForm = $form_state->getFormObject(); - /** @var \Drupal\webform\WebformSubmissionInterface $webformSubmission */ - $webformSubmission = $webformSubmissionForm->getEntity(); - $webform = $webformSubmission->getWebform(); - $elementsFlattened = $webform->getElementsInitializedAndFlattened(); + /** @var \Drupal\webform\WebformSubmissionForm $webformSubmissionForm */ + $webformSubmissionForm = $form_state->getFormObject(); + /** @var \Drupal\webform\WebformSubmissionInterface $webformSubmission */ + $webformSubmission = $webformSubmissionForm->getEntity(); + $webform = $webformSubmission->getWebform(); + $elementsFlattened = $webform->getElementsInitializedAndFlattened(); - $response = new AjaxResponse(); + foreach ($elementsFlattened as $flattenedElement) { + if (isset($flattenedElement['#type'])) { + $parents = $flattenedElement['#webform_parents']; + $element = NestedArray::getValue($form['elements'], $parents); + + switch ($element['#type']) { + case 'os2forms_mitid_child_name': + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getName() : 'Navne- og adressebeskyttet'; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-name', $element)); + break; + case 'os2forms_mitid_child_cpr': + $element['#value'] = $cprLookupResult->getCpr(); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-cpr', $element)); + break; + case 'os2forms_mitid_child_address': + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getAddress() : 'Navne- og adressebeskyttet'; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-address', $element)); + break; + case 'os2forms_mitid_child_apartment_nr': + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getApartmentNr() : 'Navne- og adressebeskyttet'; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-apartment_nr', $element)); + break; + case 'os2forms_mitid_child_city': + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getCity() : 'Navne- og adressebeskyttet'; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-city', $element)); + break; + case 'os2forms_mitid_child_coaddress': + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getCoName() : 'Navne- og adressebeskyttet'; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-coaddress', $element)); + break; + case 'os2forms_mitid_child_floor': + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getFloor() : 'Navne- og adressebeskyttet'; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-floor', $element)); + break; + case 'os2forms_mitid_child_house_nr': + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getHouseNr() : 'Navne- og adressebeskyttet'; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-house-nr', $element)); + break; + case 'os2forms_mitid_child_kommunekode': + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getMunicipalityCode() : 'Navne- og adressebeskyttet'; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-kommunekode', $element)); + break; + case 'os2forms_mitid_child_postal_code': + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getPostalCode() : 'Navne- og adressebeskyttet'; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-postal-code', $element)); + break; + case 'os2forms_mitid_child_street': + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getStreet() : 'Navne- og adressebeskyttet'; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-street', $element)); + break; + case 'os2forms_mitid_child_other_guardian': + $otherGuardianCpr = NULL; + $allGuardians = $cprLookupResult->getGuardians(); + + if (!empty($allGuardians)) { + // Making a guess primary CPR is saved in the form state. + $primaryCprLookupResult = FormsHelper::retrieveCachedCprLookupResult($form_state); - foreach ($elementsFlattened as $flattenedElement) { - if (isset($flattenedElement['#type'])) { - $parents = $flattenedElement['#webform_parents']; - $element = NestedArray::getValue($form['elements'], $parents); + if ($primaryCprLookupResult) { + foreach ($allGuardians as $guardian) { + // Find other CPR number. + if ($guardian['cpr'] != $primaryCprLookupResult->getCpr()) { + $otherGuardianCpr = $guardian['cpr']; + } + } + } + } - switch ($element['#type']) { - case 'os2forms_mitid_child_name': - $element['#value'] = $cprLookupResult->getName(); - if (!empty($element['#value'])) { - $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; - } - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-name', $element)); - break; - case 'os2forms_mitid_child_cpr': - $element['#value'] = $cprLookupResult->getCpr(); - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-cpr', $element)); - break; - case 'os2forms_mitid_child_address': - $element['#value'] = $cprLookupResult->getAddress(); - if (!empty($element['#value'])) { - $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; - } - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-address', $element)); - break; - case 'os2forms_mitid_child_apartment_nr': - $element['#value'] = $cprLookupResult->getApartmentNr(); - if (!empty($element['#value'])) { - $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; - } - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-apartment_nr', $element)); - break; - case 'os2forms_mitid_child_city': - $element['#value'] = $cprLookupResult->getCity(); - if (!empty($element['#value'])) { - $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; - } - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-city', $element)); - break; - case 'os2forms_mitid_child_coaddress': - $element['#value'] = $cprLookupResult->getCoName(); - if (!empty($element['#value'])) { - $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; - } - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-coaddress', $element)); - break; - case 'os2forms_mitid_child_floor': - $element['#value'] = $cprLookupResult->getFloor(); - if (!empty($element['#value'])) { - $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; - } - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-floor', $element)); - break; - case 'os2forms_mitid_child_house_nr': - $element['#value'] = $cprLookupResult->getHouseNr(); - if (!empty($element['#value'])) { - $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; - } - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-house-nr', $element)); - break; - case 'os2forms_mitid_child_kommunekode': - $element['#value'] = $cprLookupResult->getMunicipalityCode(); - if (!empty($element['#value'])) { - $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; - } - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-kommunekode', $element)); - break; - case 'os2forms_mitid_child_postal_code': - $element['#value'] = $cprLookupResult->getPostalCode(); - if (!empty($element['#value'])) { - $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; - } - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-postal-code', $element)); - break; - case 'os2forms_mitid_child_street': - $element['#value'] = $cprLookupResult->getStreet(); - if (!empty($element['#value'])) { - $element['#value'] .= $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; - } - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-street', $element)); - break; + $element['#value'] = $otherGuardianCpr; + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-other-guardian', $element)); + break; + } } } } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php new file mode 100644 index 00000000..f641e784 --- /dev/null +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php @@ -0,0 +1,19 @@ +has('cprLookupResult')) { + $cprLookupResult = $form_state->get('cprLookupResult'); + } + + return $cprLookupResult; + } + /** * Performs lookup of person data. * From ed022f9a27fbb86f3f64c6b8374450cb7d7e722f Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Fri, 26 Jan 2024 11:14:23 +0200 Subject: [PATCH 05/32] S2FRMS-40 other guardian CPR fix --- .../src/Element/MitidChildOtherGuardian.php | 40 ++++++++----------- .../src/Element/NemidChildrenRadios.php | 1 + .../WebformElement/NemidChildrenRadios.php | 21 +++++++++- .../WebformElement/NemidChildrenSelect.php | 21 +++++++++- .../ServiceplatformenCprElementBase.php | 4 +- 5 files changed, 59 insertions(+), 28 deletions(-) diff --git a/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php b/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php index faa8f386..31e99f9c 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php +++ b/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php @@ -8,13 +8,12 @@ /** * Provides a 'os2forms_mitid_child_other_guardian'. * - * @FormElement("os2forms_mitid_child_ther_guardian") + * @FormElement("os2forms_mitid_child_other_guardian") * * @see \Drupal\Core\Render\Element\FormElement * @see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21FormElement.php/class/FormElement * @see \Drupal\Core\Render\Element\RenderElement * @see https://api.drupal.org/api/drupal/namespace/Drupal%21Core%21Render%21Element - * @see \Drupal\os2forms_nemid\Element\NemidCpr */ class MitidChildOtherGuardian extends Hidden { @@ -22,28 +21,20 @@ class MitidChildOtherGuardian extends Hidden { * {@inheritdoc} */ public function getInfo() { - $class = get_class($this); - return parent::getInfo() + [ - '#process' => [ - [$class, 'processMitidChildOtherGuardian'], - [$class, 'processAjaxForm'], - ], - '#element_validate' => [ - [$class, 'validateMitidChildOtherGuardian'], - ], - '#pre_render' => [ - [$class, 'preRenderMitidChildOtherGuardian'], - ], - '#theme' => 'input__os2forms_mitid_child_other_guardian', + $class = static::class; + + $parentInfo = parent::getInfo() + [ + '#element_validate' => [ + [$class, 'validateMitidChildOtherGuardian'], + ], + ]; + + // Adding custom #pre_render. + $parentInfo['#pre_render'] = [ + [$class, 'preRenderMitidChildOtherGuardian'] ]; - } - /** - * Processes a 'os2forms_mitid_child_other_guardian' element. - */ - public static function processMitidChildOtherGuardian(&$element, FormStateInterface $form_state, &$complete_form) { - // Here you can add and manipulate your element's properties and callbacks. - return $element; + return $parentInfo; } /** @@ -56,9 +47,10 @@ public static function validateMitidChildOtherGuardian(&$element, FormStateInter /** * {@inheritdoc} */ - public static function preRenderMitidChildOtherGuardian(array $element) { + public static function preRenderMitidChildOtherGuardian($element) { $element = parent::preRenderHidden($element); - static::setAttributes($element, ['os2forms-mitid-child-other-guardian']); + static::setAttributes($element, ['os2forms-mitid-child-other-guardian', 'js-form-type-os2forms-mitid-child-other-guardian']); + return $element; } diff --git a/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php b/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php index c6d303b9..bf188756 100644 --- a/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php +++ b/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php @@ -2,6 +2,7 @@ namespace Drupal\os2forms_nemid\Element; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element\Radios; /** diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php index d871a11d..688c6435 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php @@ -28,6 +28,7 @@ class NemidChildrenRadios extends Radios implements NemidElementPersonalInterfac protected function defineDefaultProperties() { $properties = [ 'options' => [], + 'address_protection_help_text' => '', ] + parent::defineDefaultProperties(); return $properties; } @@ -40,6 +41,12 @@ public function form(array $form, FormStateInterface $form_state) { $form['options']['options']['#required'] = FALSE; $form['options']['#access'] = FALSE; + $form['element_description']['address_protection_help_text'] = [ + '#type' => 'webform_html_editor', + '#title' => $this->t('Address protection help text'), + '#description' => $this->t('Address protection help text is shown if any of the children has address and name protection active'), + ]; + return $form; } @@ -127,18 +134,30 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f $options = []; + $showAddressNameProtectionMessage = FALSE; + if ($cprLookupResult) { $prepopulateKey = $this->getPrepopulateFieldFieldKey($element); if ($children = $cprLookupResult->getFieldValue($prepopulateKey)) { if (is_array($children) && !empty($children)) { foreach ($children as $child) { - $options[$child['cpr']] = $child['name'] . ($child['nameAddressProtected'] ? ' (Navne- og adressebeskyttelse)' : '') ; + if ($child['nameAddressProtected']) { + $options[$child['cpr']] = $child['cpr'] . ' (Navne- og adressebeskyttet)'; + $showAddressNameProtectionMessage = TRUE; + } + else { + $options[$child['cpr']] = $child['name']; + } } } } } $element['#options'] = $options; + + if ($showAddressNameProtectionMessage) { + $element['#suffix'] = $element['#address_protection_help_text']; + } } } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php index eab8106e..b49cc3f4 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php @@ -28,6 +28,7 @@ class NemidChildrenSelect extends Select implements NemidElementPersonalInterfac protected function defineDefaultProperties() { $properties = [ 'options' => [], + 'address_protection_help_text' => '', ] + parent::defineDefaultProperties(); return $properties; } @@ -40,6 +41,12 @@ public function form(array $form, FormStateInterface $form_state) { $form['options']['options']['#required'] = FALSE; $form['options']['#access'] = FALSE; + $form['element_description']['address_protection_help_text'] = [ + '#type' => 'webform_html_editor', + '#title' => $this->t('Address protection help text'), + '#description' => $this->t('Address protection help text is shown if any of the children has address and name protection active'), + ]; + return $form; } @@ -127,18 +134,30 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f $options = []; + $showAddressNameProtectionMessage = FALSE; + if ($cprLookupResult) { $prepopulateKey = $this->getPrepopulateFieldFieldKey($element); if ($children = $cprLookupResult->getFieldValue($prepopulateKey)) { if (is_array($children) && !empty($children)) { foreach ($children as $child) { - $options[$child['cpr']] = $child['name'] . ($child['nameAddressProtected'] ? ' (Navne- og adressebeskyttelse)' : '') ; + if ($child['nameAddressProtected']) { + $options[$child['cpr']] = $child['cpr'] . ' (Navne- og adressebeskyttet)'; + $showAddressNameProtectionMessage = TRUE; + } + else { + $options[$child['cpr']] = $child['name']; + } } } } } $element['#options'] = $options; + + if ($showAddressNameProtectionMessage) { + $element['#suffix'] = $element['#address_protection_help_text']; + } } } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php b/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php index 34f48522..6567c6e3 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php @@ -44,10 +44,10 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f * Initialized CprLooupResult. * * @return string - * String " (Navne- og adressebeskyttelse)" or nothing. + * String " (Navne- og adressebeskyttet)" or nothing. */ public function appendNameAddressProtectedText(CprLookupResult $cprLookupResult) { - return $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttelse)' : ''; + return $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttet)' : ''; } } From 936813b012515f1daf23953827b295c732c70eb2 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Wed, 31 Jan 2024 13:33:14 +0200 Subject: [PATCH 06/32] S2FRMS-40 making other guardian CPR private --- .../MitidChildrenSelectAjaxBehaviour.php | 2 +- .../WebformElement/MitidChildOtherGuardian.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php index 985593a6..31570084 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php +++ b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php @@ -57,7 +57,7 @@ public static function mitidChildrenSelectAjax(array &$form, FormStateInterface break; case 'os2forms_mitid_child_apartment_nr': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getApartmentNr() : 'Navne- og adressebeskyttet'; - $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-apartment_nr', $element)); + $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-apartment-nr', $element)); break; case 'os2forms_mitid_child_city': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getCity() : 'Navne- og adressebeskyttet'; diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php index f641e784..f519de48 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php @@ -2,6 +2,7 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; +use Drupal\Core\Form\FormStateInterface; use Drupal\webform\Plugin\WebformElement\Hidden; /** @@ -16,4 +17,20 @@ */ class MitidChildOtherGuardian extends Hidden { + /** + * {@inheritdoc} + */ + public function form(array $form, FormStateInterface $form_state) { + $form = parent::form($form, $form_state); + + // @see \Drupal\webform\Plugin\WebformElement\WebformEntityReferenceTrait::form + $element_properties = $form_state->get('element_properties'); + + // If element is new, set private by default. + if (empty($element_properties['title'])) { + $form['admin']['private']['#value'] = TRUE; + } + + return $form; + } } From e12d53498bafab052b66fff04ee69e0bf243b41c Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Wed, 31 Jan 2024 15:50:13 +0200 Subject: [PATCH 07/32] S2FRMS-40 hiding other guardian value for email --- .../WebformElement/MitidChildOtherGuardian.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php index f519de48..53bed78a 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php @@ -4,6 +4,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\webform\Plugin\WebformElement\Hidden; +use Drupal\webform\WebformSubmissionInterface; /** * Provides a 'os2forms_mitid_child_other_guardian' element. @@ -17,20 +18,11 @@ */ class MitidChildOtherGuardian extends Hidden { - /** - * {@inheritdoc} - */ - public function form(array $form, FormStateInterface $form_state) { - $form = parent::form($form, $form_state); - - // @see \Drupal\webform\Plugin\WebformElement\WebformEntityReferenceTrait::form - $element_properties = $form_state->get('element_properties'); - - // If element is new, set private by default. - if (empty($element_properties['title'])) { - $form['admin']['private']['#value'] = TRUE; + public function getValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) { + if (isset($options['email'])) { + return ''; } - return $form; + return parent::getValue($element, $webform_submission, $options); } } From 61e4481438addfead15bd2b03c195bc47c627e26 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Wed, 31 Jan 2024 16:33:48 +0200 Subject: [PATCH 08/32] S2FRMS-40 hiding other guardian value for pdf --- .../src/Plugin/WebformElement/MitidChildOtherGuardian.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php index 53bed78a..3347c17e 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php @@ -2,7 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\Core\Form\FormStateInterface; use Drupal\webform\Plugin\WebformElement\Hidden; use Drupal\webform\WebformSubmissionInterface; @@ -19,7 +18,7 @@ class MitidChildOtherGuardian extends Hidden { public function getValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) { - if (isset($options['email'])) { + if (isset($options['email']) || isset($options['pdf'])) { return ''; } From 1c4210a548b3c4d210d2712f869248b302ad1775 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Tue, 11 Jun 2024 16:51:55 +0300 Subject: [PATCH 09/32] OS-52 Maestro notification cpr_guardian --- .../src/Plugin/WebformHandler/MaestroNotificationHandler.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/os2forms_forloeb/src/Plugin/WebformHandler/MaestroNotificationHandler.php b/modules/os2forms_forloeb/src/Plugin/WebformHandler/MaestroNotificationHandler.php index 02c7f943..69d7d9a7 100644 --- a/modules/os2forms_forloeb/src/Plugin/WebformHandler/MaestroNotificationHandler.php +++ b/modules/os2forms_forloeb/src/Plugin/WebformHandler/MaestroNotificationHandler.php @@ -239,6 +239,7 @@ private function getRecipientElementOptions(): array { 'cvr_element', 'cvr_value_element', 'os2forms_person_lookup', + 'os2forms_mitid_child_other_guardian', ]; $isAllowedElement = static fn ($e) => in_array($e['#type'], $elementTypes, TRUE); From 196f8e1890c5a4a83e52d492a21034d41208f28c Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Wed, 19 Jun 2024 15:17:30 +0300 Subject: [PATCH 10/32] Cleaning CHANGELOG --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df601d0a..b822e25b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,7 +46,6 @@ before starting to add changes. Use example [placed in the end of the page](#exa Fix array access with `purge_days` configuration. ## [3.14.1] 2024-01-16 ->>>>>>> develop - CprFetchData adding ajax error fix From 83d72a4e61965da6a150736360eb8ff42e0ca3cf Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Wed, 19 Jun 2024 15:19:18 +0300 Subject: [PATCH 11/32] OS-74 - Replacing DAWA matrikula select with Datafordeler select --- CHANGELOG.md | 2 + .../Element/DawaElementAddressMatrikula.php | 47 +++-- .../src/Entity/DatafordelerMatrikula.php | 64 ++++++ .../os2forms_dawa/src/Entity/DawaAddress.php | 18 ++ .../DataLookup/DatafordelerDataLookup.php | 182 ++++++++++++++++++ .../DatafordelerDataLookupInterface.php | 38 ++++ .../os2forms_dawa/src/Service/DawaService.php | 4 +- 7 files changed, 334 insertions(+), 21 deletions(-) create mode 100644 modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php create mode 100644 modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php create mode 100644 modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php diff --git a/CHANGELOG.md b/CHANGELOG.md index db0ded3f..3f71ffaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +- [OS-74] Replacing DAWA matrikula select with Datafordeler select + ## [3.15.2] 2024-05-27 - [#108](https://github.com/OS2Forms/os2forms/pull/108) diff --git a/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php b/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php index fcaf3c51..12cb99f1 100644 --- a/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php +++ b/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php @@ -3,6 +3,7 @@ namespace Drupal\os2forms_dawa\Element; use Drupal\Core\Form\FormStateInterface; +use Drupal\os2forms_dawa\Plugin\os2web\DataLookup\DatafordelerDataLookupInterface; use Drupal\webform\Element\WebformCompositeBase; use Symfony\Component\HttpFoundation\ParameterBag; @@ -94,9 +95,16 @@ public static function getCompositeElements(array $element) { * Array of matrikula options key and the values are identical. */ private static function getMatrikulaOptions($addressValue, array $element) { + $options = []; + /** @var \Drupal\os2forms_dawa\Service\DawaService $dawaService */ $dawaService = \Drupal::service('os2forms_dawa.service'); + /** @var \Drupal\os2forms_dawa\Service\DatafordelerService $datafordelerService */ + + /** @var DatafordelerDataLookupInterface $datafordelerLookup */ + $datafordelerLookup = \Drupal::service('plugin.manager.os2web_datalookup')->createInstance('datafordeler_data_lookup'); + // Getting address. $addressParams = new ParameterBag(); $addressParams->set('q', $addressValue); @@ -106,29 +114,30 @@ private static function getMatrikulaOptions($addressValue, array $element) { $address = $dawaService->getSingleAddress($addressParams); if ($address) { - // Getting matrikula options. - $matrikulaParams = new ParameterBag(); - // Getting municipality code from address. - if ($municipality_code = $address->getMunicipalityCode()) { - $matrikulaParams->set('limit_by_municipality', $municipality_code); - } - // Getting property nr from address. - if ($property_nr = $address->getPropertyNumber()) { - $matrikulaParams->set('limit_by_property', $property_nr); - } - // If the matrikula option must not have the code. - if (isset($element['#remove_code'])) { - $matrikulaParams->set('remove_code', $element['#remove_code']); - } + $addressAccessId = $address->getAccessAddressId(); - // Get the options. - $matrikulaOptions = $dawaService->getMatrikulaMatches($matrikulaParams); + // Find matrikula list from the houseid (husnummer): + $matrikulaIdList = $datafordelerLookup->getMatrikulaIds($addressAccessId); - // Use values as keys. - return array_combine($matrikulaOptions, $matrikulaOptions); + // Find Matrikula entry from matrikulas ID. + if (!empty($matrikulaIdList)) { + foreach ($matrikulaIdList as $matrikulaId) { + $matrikula = $datafordelerLookup->getMatrikulaEntry($matrikulaId); + + if ($matrikula) { + $matrikulaOption = $matrikula->getMatrikulaNumber() . ' ' . $matrikula->getOwnershipName(); + + if (isset($element['#remove_code']) && !$element['#remove_code']) { + $matrikulaOption .= ' (' . $matrikula->getOwnerLicenseCode() . ')'; + } + + $options[$matrikulaOption] = $matrikulaOption; + } + } + } } - return []; + return $options; } /** diff --git a/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php b/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php new file mode 100644 index 00000000..96ab322b --- /dev/null +++ b/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php @@ -0,0 +1,64 @@ +ownerLicenseCode = $jordstykke['properties']['ejerlavskode']; + $this->ownershipName = $jordstykke['properties']['ejerlavsnavn']; + $this->matrikulaNumber = $jordstykke['properties']['matrikelnummer']; + } + } + + public function getOwnerLicenseCode(): string { + return $this->ownerLicenseCode; + } + + public function getOwnershipName(): string { + return $this->ownershipName; + } + + public function getMatrikulaNumber(): string { + return $this->matrikulaNumber; + } + + + +} diff --git a/modules/os2forms_dawa/src/Entity/DawaAddress.php b/modules/os2forms_dawa/src/Entity/DawaAddress.php index 723dde3e..89fe92a7 100644 --- a/modules/os2forms_dawa/src/Entity/DawaAddress.php +++ b/modules/os2forms_dawa/src/Entity/DawaAddress.php @@ -45,6 +45,13 @@ class DawaAddress { */ protected $longitude; + /** + * Address access ID. + * + * @var string + */ + protected $accessAddressId; + /** * DawaAddress constructor. * @@ -61,6 +68,7 @@ public function __construct(array $json) { $this->propertyNumber = $json['adgangsadresse']['esrejendomsnr']; $this->longitude = $json['adgangsadresse']['adgangspunkt']['koordinater'][0]; $this->latitude = $json['adgangsadresse']['adgangspunkt']['koordinater'][1]; + $this->accessAddressId = $json['adgangsadresse']['id']; } } @@ -114,4 +122,14 @@ public function getLongitude() { return $this->longitude; } + /** + * Gets Address access ID. + * + * @return string + * Address access ID. + */ + public function getAccessAddressId() { + return $this->accessAddressId; + } + } diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php new file mode 100644 index 00000000..4a5ef8d7 --- /dev/null +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php @@ -0,0 +1,182 @@ +httpClient = $httpClient; + parent::__construct($configuration, $plugin_id, $plugin_definition); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('http_client') + ); + } + + /** + * {@inheritdoc} + */ + public function getMatrikulaIds(string $addressAccessId) : array { + $url = "https://services.datafordeler.dk/BBR/BBRPublic/1/rest/grund";// + + $configuration = $this->getConfiguration(); + $json = $this->httpClient->request('GET', $url, [ + 'query' => [ + 'husnummer' => $addressAccessId, + 'status' => 7, + 'username' => $configuration['username'], + 'password' => $configuration['password'] + ] + ])->getBody(); + + $jsonDecoded = json_decode($json, TRUE); + if (is_array($jsonDecoded)) { + return $jsonDecoded[0]['jordstykkeList']; + } + + return []; + } + + /** + * {@inheritdoc} + */ + public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula { + $url = "https://services.datafordeler.dk/Matriklen2/Matrikel/2.0.0/rest/SamletFastEjendom"; + + $configuration = $this->getConfiguration(); + $json = $this->httpClient->request('GET', $url, [ + 'query' => [ + 'jordstykkeid' => $matrikulaId, + 'username' => $configuration['username'], + 'password' => $configuration['password'] + ] + ])->getBody(); + + $jsonDecoded = json_decode($json, TRUE); + if (is_array($jsonDecoded)) { + return new DatafordelerMatrikula($jsonDecoded); + } + + return NULL; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'username' => '', + 'password' => '', + ] + parent::defaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['username'] = [ + '#type' => 'textfield', + '#title' => $this->t('Username for service calls'), + '#default_value' => $this->configuration['username'], + '#required' => TRUE, + '#description' => $this->t('Username required for performing API requests'), + ]; + $form['password'] = [ + '#type' => 'textfield', + '#title' => $this->t('Password for service calls'), + '#default_value' => $this->configuration['password'], + '#required' => TRUE, + '#description' => $this->t('Password required for performing API requests'), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + parent::validateConfigurationForm($form, $form_state); + +// // Validating 'address_autocomplete_path', 'block_autocomplete_path', +// // 'matrikula_autocomplete_path'. +// $elementsToValidate = [ +// 'address_autocomplete_path', +// 'block_autocomplete_path', +// 'matrikula_autocomplete_path', +// ]; +// foreach ($elementsToValidate as $elementKey) { +// $autocomplete_path = $form_state->getValue($elementKey); +// $json = file_get_contents($autocomplete_path); +// $jsonDecoded = json_decode($json, TRUE); +// if (empty($jsonDecoded)) { +// $form_state->setErrorByName($elementKey, $this->t('URL is not valid or it does not provide the result in the required format')); +// } +// else { +// $entry = reset($jsonDecoded); +// if (!array_key_exists('tekst', $entry)) { +// $form_state->setErrorByName($elementKey, $this->t('URL is not valid or it does not provide the result in the required format')); +// } +// } +// } +// +// // Validating address_api_path. +// $autocomplete_path = $form_state->getValue('address_api_path'); +// // Limiting the output. +// $json = file_get_contents($autocomplete_path . '?per_side=1'); +// $jsonDecoded = json_decode($json, TRUE); +// if (empty($jsonDecoded)) { +// $form_state->setErrorByName('address_api_path', $this->t('URL is not valid or it does not provide the result in the required format')); +// } +// else { +// $entry = reset($jsonDecoded); +// if (!array_key_exists('id', $entry)) { +// $form_state->setErrorByName('address_api_path', $this->t('URL is not valid or it does not provide the result in the required format')); +// } +// } + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $configuration = $this->getConfiguration(); + $configuration['username'] = $form_state->getValue('username'); + $configuration['password'] = $form_state->getValue('password'); + $this->setConfiguration($configuration); + } +} diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php new file mode 100644 index 00000000..fb24a715 --- /dev/null +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php @@ -0,0 +1,38 @@ + Date: Wed, 19 Jun 2024 16:56:54 +0300 Subject: [PATCH 12/32] S2FRMS-40 - PHPCS fixes --- .../src/Element/MitidChildAddress.php | 2 +- .../src/Element/MitidChildApartmentNr.php | 2 +- .../src/Element/MitidChildCoaddress.php | 2 +- .../src/Element/MitidChildHouseNr.php | 2 +- .../src/Element/MitidChildKommunekode.php | 2 +- .../src/Element/MitidChildOtherGuardian.php | 17 ++++++----- .../src/Element/MitidChildPostalCode.php | 2 +- .../MitidChildrenSelectAjaxBehaviour.php | 29 ++++++++++++++++++- .../src/Element/NemidChildrenRadios.php | 18 ++++++------ .../src/Element/NemidChildrenSelect.php | 16 +++++----- .../WebformElement/MitidChildAddress.php | 2 -- .../WebformElement/MitidChildApartmentNr.php | 2 -- .../Plugin/WebformElement/MitidChildCity.php | 2 -- .../WebformElement/MitidChildCoaddress.php | 2 -- .../Plugin/WebformElement/MitidChildCpr.php | 2 -- .../WebformElement/MitidChildElementBase.php | 14 ++++----- .../Plugin/WebformElement/MitidChildFloor.php | 2 -- .../WebformElement/MitidChildHouseNr.php | 2 -- .../WebformElement/MitidChildKommunekode.php | 2 -- .../Plugin/WebformElement/MitidChildName.php | 2 -- .../MitidChildOtherGuardian.php | 4 +++ .../WebformElement/MitidChildPostalCode.php | 2 -- .../WebformElement/MitidChildStreet.php | 2 -- .../ServiceplatformenCprElementBase.php | 4 +-- .../src/Service/FormsHelper.php | 2 +- 25 files changed, 75 insertions(+), 63 deletions(-) diff --git a/modules/os2forms_nemid/src/Element/MitidChildAddress.php b/modules/os2forms_nemid/src/Element/MitidChildAddress.php index f8daa7a6..04850006 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildAddress.php +++ b/modules/os2forms_nemid/src/Element/MitidChildAddress.php @@ -46,7 +46,7 @@ public static function processMitidChildAddress(&$element, FormStateInterface $f } /** - * Webform element validation handler for #type 'os2forms_mitid_child_address'. + * Webform element validation handler for 'os2forms_mitid_child_address'. */ public static function validateMitidChildAddress(&$element, FormStateInterface $form_state, &$complete_form) { // Here you can add custom validation logic. diff --git a/modules/os2forms_nemid/src/Element/MitidChildApartmentNr.php b/modules/os2forms_nemid/src/Element/MitidChildApartmentNr.php index 65cecc7b..82690966 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildApartmentNr.php +++ b/modules/os2forms_nemid/src/Element/MitidChildApartmentNr.php @@ -46,7 +46,7 @@ public static function processMitidChildApartmentNr(&$element, FormStateInterfac } /** - * Webform element validation handler for #type 'os2forms_mitid_child_apartmentNr'. + * Webform element validation handler 'os2forms_mitid_child_apartmentNr'. */ public static function validateMitidChildApartmentNr(&$element, FormStateInterface $form_state, &$complete_form) { // Here you can add custom validation logic. diff --git a/modules/os2forms_nemid/src/Element/MitidChildCoaddress.php b/modules/os2forms_nemid/src/Element/MitidChildCoaddress.php index 87fd6be0..eb208774 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildCoaddress.php +++ b/modules/os2forms_nemid/src/Element/MitidChildCoaddress.php @@ -46,7 +46,7 @@ public static function processMitidChildCoaddress(&$element, FormStateInterface } /** - * Webform element validation handler for #type 'os2forms_mitid_child_coaddress'. + * Webform element validation handler for 'os2forms_mitid_child_coaddress'. */ public static function validateMitidChildCoaddress(&$element, FormStateInterface $form_state, &$complete_form) { // Here you can add custom validation logic. diff --git a/modules/os2forms_nemid/src/Element/MitidChildHouseNr.php b/modules/os2forms_nemid/src/Element/MitidChildHouseNr.php index f07e3d6a..68ff9e57 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildHouseNr.php +++ b/modules/os2forms_nemid/src/Element/MitidChildHouseNr.php @@ -46,7 +46,7 @@ public static function processMitidChildHouseNr(&$element, FormStateInterface $f } /** - * Webform element validation handler for #type 'os2forms_mitid_child_house_nr'. + * Webform element validation handler for 'os2forms_mitid_child_house_nr'. */ public static function validateMitidChildHouseNr(&$element, FormStateInterface $form_state, &$complete_form) { // Here you can add custom validation logic. diff --git a/modules/os2forms_nemid/src/Element/MitidChildKommunekode.php b/modules/os2forms_nemid/src/Element/MitidChildKommunekode.php index 9b773624..1fc3312f 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildKommunekode.php +++ b/modules/os2forms_nemid/src/Element/MitidChildKommunekode.php @@ -46,7 +46,7 @@ public static function processMitidChildKommunekode(&$element, FormStateInterfac } /** - * Webform element validation handler for #type 'os2forms_mitid_child_kommunekode'. + * Webform element validation handler for 'os2forms_mitid_child_kommunekode'. */ public static function validateMitidChildKommunekode(&$element, FormStateInterface $form_state, &$complete_form) { // Here you can add custom validation logic. diff --git a/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php b/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php index 31e99f9c..b8b54d99 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php +++ b/modules/os2forms_nemid/src/Element/MitidChildOtherGuardian.php @@ -24,21 +24,21 @@ public function getInfo() { $class = static::class; $parentInfo = parent::getInfo() + [ - '#element_validate' => [ - [$class, 'validateMitidChildOtherGuardian'], - ], - ]; + '#element_validate' => [ + [$class, 'validateMitidChildOtherGuardian'], + ], + ]; // Adding custom #pre_render. $parentInfo['#pre_render'] = [ - [$class, 'preRenderMitidChildOtherGuardian'] + [$class, 'preRenderMitidChildOtherGuardian'], ]; return $parentInfo; } /** - * Webform element validation handler for #type 'os2forms_mitid_child_other_guardian'. + * Webform element validation handler 'os2forms_mitid_child_other_guardian'. */ public static function validateMitidChildOtherGuardian(&$element, FormStateInterface $form_state, &$complete_form) { // Here you can add custom validation logic. @@ -49,7 +49,10 @@ public static function validateMitidChildOtherGuardian(&$element, FormStateInter */ public static function preRenderMitidChildOtherGuardian($element) { $element = parent::preRenderHidden($element); - static::setAttributes($element, ['os2forms-mitid-child-other-guardian', 'js-form-type-os2forms-mitid-child-other-guardian']); + static::setAttributes($element, [ + 'os2forms-mitid-child-other-guardian', + 'js-form-type-os2forms-mitid-child-other-guardian', + ]); return $element; } diff --git a/modules/os2forms_nemid/src/Element/MitidChildPostalCode.php b/modules/os2forms_nemid/src/Element/MitidChildPostalCode.php index d78da623..9c6db66c 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildPostalCode.php +++ b/modules/os2forms_nemid/src/Element/MitidChildPostalCode.php @@ -46,7 +46,7 @@ public static function processMitidChildPostalCode(&$element, FormStateInterface } /** - * Webform element validation handler for #type 'os2forms_mitid_child_postal_code'. + * Webform element validation handler for 'os2forms_mitid_child_postal_code'. */ public static function validateMitidChildPostalCode(&$element, FormStateInterface $form_state, &$complete_form) { // Here you can add custom validation logic. diff --git a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php index 31570084..2badc955 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php +++ b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php @@ -9,10 +9,25 @@ use Drupal\os2forms_nemid\Service\FormsHelper; /** - * Provides a children select ajax behaviour for MitidChildrenSelect and MitidChildrenRadios. + * Provides a children select ajax behaviour. + * + * User in MitidChildrenSelect and MitidChildrenRadios. */ class MitidChildrenSelectAjaxBehaviour { + /** + * Ajax function to update fields after child select. + * + * @param array $form + * Form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + * + * @return \Drupal\Core\Ajax\AjaxResponse + * Ajax response + * + * @throws \Drupal\Component\Plugin\Exception\PluginException + */ public static function mitidChildrenSelectAjax(array &$form, FormStateInterface $form_state) { $response = new AjaxResponse(); @@ -47,46 +62,57 @@ public static function mitidChildrenSelectAjax(array &$form, FormStateInterface $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getName() : 'Navne- og adressebeskyttet'; $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-name', $element)); break; + case 'os2forms_mitid_child_cpr': $element['#value'] = $cprLookupResult->getCpr(); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-cpr', $element)); break; + case 'os2forms_mitid_child_address': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getAddress() : 'Navne- og adressebeskyttet'; $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-address', $element)); break; + case 'os2forms_mitid_child_apartment_nr': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getApartmentNr() : 'Navne- og adressebeskyttet'; $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-apartment-nr', $element)); break; + case 'os2forms_mitid_child_city': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getCity() : 'Navne- og adressebeskyttet'; $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-city', $element)); break; + case 'os2forms_mitid_child_coaddress': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getCoName() : 'Navne- og adressebeskyttet'; $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-coaddress', $element)); break; + case 'os2forms_mitid_child_floor': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getFloor() : 'Navne- og adressebeskyttet'; $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-floor', $element)); break; + case 'os2forms_mitid_child_house_nr': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getHouseNr() : 'Navne- og adressebeskyttet'; $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-house-nr', $element)); break; + case 'os2forms_mitid_child_kommunekode': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getMunicipalityCode() : 'Navne- og adressebeskyttet'; $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-kommunekode', $element)); break; + case 'os2forms_mitid_child_postal_code': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getPostalCode() : 'Navne- og adressebeskyttet'; $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-postal-code', $element)); break; + case 'os2forms_mitid_child_street': $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getStreet() : 'Navne- og adressebeskyttet'; $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-street', $element)); break; + case 'os2forms_mitid_child_other_guardian': $otherGuardianCpr = NULL; $allGuardians = $cprLookupResult->getGuardians(); @@ -115,4 +141,5 @@ public static function mitidChildrenSelectAjax(array &$form, FormStateInterface return $response; } + } diff --git a/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php b/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php index bf188756..7545f5bb 100644 --- a/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php +++ b/modules/os2forms_nemid/src/Element/NemidChildrenRadios.php @@ -2,7 +2,6 @@ namespace Drupal\os2forms_nemid\Element; -use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element\Radios; /** @@ -23,14 +22,15 @@ class NemidChildrenRadios extends Radios { */ public function getInfo() { return parent::getInfo() + [ - '#ajax' => [ - 'callback' => [MitidChildrenSelectAjaxBehaviour::class, 'mitidChildrenSelectAjax'], - 'event' => 'change', - 'progress' => [ - 'type' => 'throbber', - 'message' => $this->t('Please wait...'), - ], + '#ajax' => [ + 'callback' => [MitidChildrenSelectAjaxBehaviour::class, 'mitidChildrenSelectAjax'], + 'event' => 'change', + 'progress' => [ + 'type' => 'throbber', + 'message' => $this->t('Please wait...'), ], - ]; + ], + ]; } + } diff --git a/modules/os2forms_nemid/src/Element/NemidChildrenSelect.php b/modules/os2forms_nemid/src/Element/NemidChildrenSelect.php index 567021f4..bc0382d2 100644 --- a/modules/os2forms_nemid/src/Element/NemidChildrenSelect.php +++ b/modules/os2forms_nemid/src/Element/NemidChildrenSelect.php @@ -22,15 +22,15 @@ class NemidChildrenSelect extends Select { */ public function getInfo() { return parent::getInfo() + [ - '#ajax' => [ - 'callback' => [MitidChildrenSelectAjaxBehaviour::class, 'mitidChildrenSelectAjax'], - 'event' => 'change', - 'progress' => [ - 'type' => 'throbber', - 'message' => $this->t('Please wait...'), - ], + '#ajax' => [ + 'callback' => [MitidChildrenSelectAjaxBehaviour::class, 'mitidChildrenSelectAjax'], + 'event' => 'change', + 'progress' => [ + 'type' => 'throbber', + 'message' => $this->t('Please wait...'), ], - ]; + ], + ]; } } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildAddress.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildAddress.php index ace06e71..d1e512ba 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildAddress.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildAddress.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_address' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildApartmentNr.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildApartmentNr.php index 2abcc654..e8b1ac97 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildApartmentNr.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildApartmentNr.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_apartment_nr' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCity.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCity.php index e99fd58c..002a0962 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCity.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCity.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_city' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCoaddress.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCoaddress.php index 35ea0b75..ccb1f535 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCoaddress.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCoaddress.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_coadress' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCpr.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCpr.php index 9d26dc05..b16e511f 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCpr.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildCpr.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_cpr' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildElementBase.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildElementBase.php index bfe9a630..fbdd7dca 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildElementBase.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildElementBase.php @@ -24,13 +24,13 @@ public function getDefaultProperties() { // @see \Drupal\webform\Plugin\WebformElementBase::getDefaultProperties // @see \Drupal\webform\Plugin\WebformElementBase::getDefaultBaseProperties $properties = parent::getDefaultProperties() + [ - 'multiple' => '', - 'size' => '', - 'minlength' => '', - 'maxlength' => '', - 'placeholder' => '', - 'readonly' => '', - ]; + 'multiple' => '', + 'size' => '', + 'minlength' => '', + 'maxlength' => '', + 'placeholder' => '', + 'readonly' => '', + ]; return $properties; } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildFloor.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildFloor.php index ec8371b6..c9ac4725 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildFloor.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildFloor.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_floor' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildHouseNr.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildHouseNr.php index 5f94d38c..62eb97c3 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildHouseNr.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildHouseNr.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_house_nr' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildKommunekode.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildKommunekode.php index 1413727a..60940db4 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildKommunekode.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildKommunekode.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_kommunekode' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildName.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildName.php index e5d436f6..d9971f9b 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildName.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildName.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_name' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php index 3347c17e..54dc97d5 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildOtherGuardian.php @@ -17,6 +17,9 @@ */ class MitidChildOtherGuardian extends Hidden { + /** + * {@inheritdoc} + */ public function getValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) { if (isset($options['email']) || isset($options['pdf'])) { return ''; @@ -24,4 +27,5 @@ public function getValue(array $element, WebformSubmissionInterface $webform_sub return parent::getValue($element, $webform_submission, $options); } + } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildPostalCode.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildPostalCode.php index 642584cd..1f365c62 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildPostalCode.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildPostalCode.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_postal_code' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildStreet.php b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildStreet.php index 606bc25f..44ad2964 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildStreet.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/MitidChildStreet.php @@ -2,8 +2,6 @@ namespace Drupal\os2forms_nemid\Plugin\WebformElement; -use Drupal\webform\Plugin\WebformElementBase; - /** * Provides a 'os2forms_mitid_child_street' element. * diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php b/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php index 6567c6e3..e77530a3 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php @@ -38,9 +38,9 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f } /** - * Appends name and address protected text, if person has name/address protection. + * Appends name/address protected text, if person has name/address protection. * - * @param CprLookupResult $cprLookupResult + * @param \Drupal\os2web_datalookup\LookupResult\CprLookupResult $cprLookupResult * Initialized CprLooupResult. * * @return string diff --git a/modules/os2forms_nemid/src/Service/FormsHelper.php b/modules/os2forms_nemid/src/Service/FormsHelper.php index d25606cc..55ace01b 100644 --- a/modules/os2forms_nemid/src/Service/FormsHelper.php +++ b/modules/os2forms_nemid/src/Service/FormsHelper.php @@ -139,7 +139,7 @@ public function retrieveCprLookupResult(FormStateInterface $form_state) { * CPRLookupResult or NULL. */ public static function retrieveCachedCprLookupResult(FormStateInterface $form_state) { - /** @var CprLookupResult $cprLookupResult */ + /** @var \Drupal\os2web_datalookup\LookupResult\CprLookupResult $cprLookupResult */ $cprLookupResult = NULL; // Trying to fetch person data from cache. From 5d18c4e16a57d4107debce9714918ef848073bef Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Fri, 21 Jun 2024 15:38:09 +0300 Subject: [PATCH 13/32] phpcs fixes --- .../Element/DawaElementAddressMatrikula.php | 5 +- .../src/Entity/DatafordelerMatrikula.php | 27 +++++++-- .../DataLookup/DatafordelerDataLookup.php | 60 +++---------------- .../DatafordelerDataLookupInterface.php | 5 +- .../os2forms_dawa/src/Service/DawaService.php | 2 +- 5 files changed, 36 insertions(+), 63 deletions(-) diff --git a/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php b/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php index 12cb99f1..2e75805f 100644 --- a/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php +++ b/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php @@ -3,7 +3,6 @@ namespace Drupal\os2forms_dawa\Element; use Drupal\Core\Form\FormStateInterface; -use Drupal\os2forms_dawa\Plugin\os2web\DataLookup\DatafordelerDataLookupInterface; use Drupal\webform\Element\WebformCompositeBase; use Symfony\Component\HttpFoundation\ParameterBag; @@ -100,9 +99,7 @@ private static function getMatrikulaOptions($addressValue, array $element) { /** @var \Drupal\os2forms_dawa\Service\DawaService $dawaService */ $dawaService = \Drupal::service('os2forms_dawa.service'); - /** @var \Drupal\os2forms_dawa\Service\DatafordelerService $datafordelerService */ - - /** @var DatafordelerDataLookupInterface $datafordelerLookup */ + /** @var \Drupal\os2forms_dawa\Plugin\os2web\DataLookup\DatafordelerDataLookupInterface $datafordelerLookup */ $datafordelerLookup = \Drupal::service('plugin.manager.os2web_datalookup')->createInstance('datafordeler_data_lookup'); // Getting address. diff --git a/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php b/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php index 96ab322b..0e05a272 100644 --- a/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php +++ b/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php @@ -5,19 +5,21 @@ /** * Class DatafordelerMatrikula. * - * Wrapper class for Datafordeler matrikula object that easies the matrikula property - * access. + * Wrapper class for Datafordeler matrikula object that easies + * the matrikula property access. */ class DatafordelerMatrikula { /** * Owner licence code / ejerlavskode. + * * @var string */ protected string $ownerLicenseCode; /** * Ownership name / ejerlavsnavn. + * * @var string */ protected string $ownershipName; @@ -25,6 +27,7 @@ class DatafordelerMatrikula { /** * Matrikula number / matrikelnummer. + * * @var string */ protected string $matrikulaNumber; @@ -47,18 +50,34 @@ public function __construct(array $json) { } } + /** + * Returns owner licence code. + * + * @return string + * Owners licence code. + */ public function getOwnerLicenseCode(): string { return $this->ownerLicenseCode; } + /** + * Returns ownership name. + * + * @return string + * ownership name. + */ public function getOwnershipName(): string { return $this->ownershipName; } + /** + * Returns makrikula number. + * + * @return string + * Matrikula number + */ public function getMatrikulaNumber(): string { return $this->matrikulaNumber; } - - } diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php index 4a5ef8d7..a702e2d9 100644 --- a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php @@ -8,7 +8,6 @@ use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupBase; use GuzzleHttp\ClientInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpClient\HttpClient; /** * Defines a plugin for Datafordeler Data. @@ -23,7 +22,7 @@ class DatafordelerDataLookup extends DataLookupBase implements DatafordelerDataL /** * The HTTP client to fetch the feed data with. * - * @var ClientInterface + * @var \GuzzleHttp\ClientInterface */ protected $httpClient; @@ -43,7 +42,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('http_client') + $container->get('http_client'), ); } @@ -51,7 +50,7 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function getMatrikulaIds(string $addressAccessId) : array { - $url = "https://services.datafordeler.dk/BBR/BBRPublic/1/rest/grund";// + $url = "https://services.datafordeler.dk/BBR/BBRPublic/1/rest/grund"; $configuration = $this->getConfiguration(); $json = $this->httpClient->request('GET', $url, [ @@ -59,8 +58,8 @@ public function getMatrikulaIds(string $addressAccessId) : array { 'husnummer' => $addressAccessId, 'status' => 7, 'username' => $configuration['username'], - 'password' => $configuration['password'] - ] + 'password' => $configuration['password'], + ], ])->getBody(); $jsonDecoded = json_decode($json, TRUE); @@ -82,8 +81,8 @@ public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula 'query' => [ 'jordstykkeid' => $matrikulaId, 'username' => $configuration['username'], - 'password' => $configuration['password'] - ] + 'password' => $configuration['password'], + ], ])->getBody(); $jsonDecoded = json_decode($json, TRUE); @@ -126,50 +125,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta return $form; } - /** - * {@inheritdoc} - */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - parent::validateConfigurationForm($form, $form_state); - -// // Validating 'address_autocomplete_path', 'block_autocomplete_path', -// // 'matrikula_autocomplete_path'. -// $elementsToValidate = [ -// 'address_autocomplete_path', -// 'block_autocomplete_path', -// 'matrikula_autocomplete_path', -// ]; -// foreach ($elementsToValidate as $elementKey) { -// $autocomplete_path = $form_state->getValue($elementKey); -// $json = file_get_contents($autocomplete_path); -// $jsonDecoded = json_decode($json, TRUE); -// if (empty($jsonDecoded)) { -// $form_state->setErrorByName($elementKey, $this->t('URL is not valid or it does not provide the result in the required format')); -// } -// else { -// $entry = reset($jsonDecoded); -// if (!array_key_exists('tekst', $entry)) { -// $form_state->setErrorByName($elementKey, $this->t('URL is not valid or it does not provide the result in the required format')); -// } -// } -// } -// -// // Validating address_api_path. -// $autocomplete_path = $form_state->getValue('address_api_path'); -// // Limiting the output. -// $json = file_get_contents($autocomplete_path . '?per_side=1'); -// $jsonDecoded = json_decode($json, TRUE); -// if (empty($jsonDecoded)) { -// $form_state->setErrorByName('address_api_path', $this->t('URL is not valid or it does not provide the result in the required format')); -// } -// else { -// $entry = reset($jsonDecoded); -// if (!array_key_exists('id', $entry)) { -// $form_state->setErrorByName('address_api_path', $this->t('URL is not valid or it does not provide the result in the required format')); -// } -// } - } - /** * {@inheritdoc} */ @@ -179,4 +134,5 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s $configuration['password'] = $form_state->getValue('password'); $this->setConfiguration($configuration); } + } diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php index fb24a715..4dd71a3c 100644 --- a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php @@ -15,7 +15,7 @@ interface DatafordelerDataLookupInterface extends DataLookupInterface { /** - * Returns list of ID for Matrikula / jordstykke that is related with this address. + * Returns list of ID for Matrikula / jordstykke related with this address. * * @param string $addressAccessId * Address to make search against. @@ -31,8 +31,9 @@ public function getMatrikulaIds(string $addressAccessId) : array; * @param string $matrikulaId * Id to make search against. * - * @return DatafordelerMatrikula|NULL + * @return \Drupal\os2forms_dawa\Entity\DatafordelerMatrikula|null * Matrikula entry or NULL. */ public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula; + } diff --git a/modules/os2forms_dawa/src/Service/DawaService.php b/modules/os2forms_dawa/src/Service/DawaService.php index 1c68fa7d..671edcb1 100644 --- a/modules/os2forms_dawa/src/Service/DawaService.php +++ b/modules/os2forms_dawa/src/Service/DawaService.php @@ -7,7 +7,7 @@ use Symfony\Component\HttpFoundation\ParameterBag; /** - * Class DawaService. + * DAWA API service class. * * @package Drupal\os2forms_dawa\Service */ From 9eed4d9eec9eb412e213314fd7f459cc709bd41e Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Tue, 25 Jun 2024 11:23:38 +0300 Subject: [PATCH 14/32] Preparing 3.15.3 deploy --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f71ffaa..e020e664 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +## [3.15.3] 2024-06-25 + - [OS-74] Replacing DAWA matrikula select with Datafordeler select ## [3.15.2] 2024-05-27 From f5090b0a0d78635ca5602e7638a357306c59bd61 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Tue, 25 Jun 2024 12:34:28 +0200 Subject: [PATCH 15/32] 1062: Encryption tweaks --- ...ebformOs2FormsEncryptSubmissionStorage.php | 313 +++++++++++++++++- 1 file changed, 308 insertions(+), 5 deletions(-) diff --git a/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php b/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php index ac02e348..0b0608fa 100644 --- a/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php +++ b/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php @@ -3,25 +3,32 @@ namespace Drupal\os2forms_encrypt; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Session\AccountInterface; +use Drupal\encrypt\EncryptionProfileInterface; +use Drupal\encrypt\Entity\EncryptionProfile; use Drupal\webform\WebformInterface; +use Drupal\webform\WebformSubmissionInterface; use Drupal\webform_encrypt\WebformEncryptSubmissionStorage; use Drupal\webform_revisions\Controller\WebformRevisionsController; /** * This class extension WebformEncryptSubmissionStorage. * - * This is to enabled encryption and decryption for data and checks if webform - * revisions are enabled and runs the same code (copied here as multiple - * inherits is not a thing in PHP). + * This is to encrypt just the data sent to database and check if webform + * revisions are enabled. * - * So the getColumns is an moduleExists check and the rest is copied from - * webform revision storage class. + * This mostly runs the same code (copied here as multiple + * inherits is not a thing in PHP), with minor tweaks. */ class WebformOs2FormsEncryptSubmissionStorage extends WebformEncryptSubmissionStorage { /** * {@inheritdoc} + * + * Overwritten to add if webform_revisions module exists. + * + * @see Drupal\webform\WebformSubmissionStorage::getColumns */ public function getColumns(WebformInterface $webform = NULL, EntityInterface $source_entity = NULL, AccountInterface $account = NULL, $include_elements = TRUE) { if (!\Drupal::moduleHandler()->moduleExists('webform_revisions')) { @@ -172,4 +179,300 @@ public function getColumns(WebformInterface $webform = NULL, EntityInterface $so } } + /** + * {@inheritdoc} + * + * Overwritten to only encrypt data send to database. + * + * @see Drupal\webform\WebformSubmissionStorage::saveData + */ + public function saveData(WebformSubmissionInterface $webform_submission, $delete_first = TRUE) { + // Get submission data rows. + $data_original = $webform_submission->getData(); + + $webform = $webform_submission->getWebform(); + + $encrypted_data = $this->encryptElements($data_original, $webform); + + $webform_submission->setData($encrypted_data); + + $webform_id = $webform_submission->getWebform()->id(); + $sid = $webform_submission->id(); + + $elements = $webform_submission->getWebform()->getElementsInitializedFlattenedAndHasValue(); + $computed_elements = $webform_submission->getWebform()->getElementsComputed(); + + $rows = []; + foreach ($encrypted_data as $name => $item) { + $element = $elements[$name] ?? ['#webform_multiple' => FALSE, '#webform_composite' => FALSE]; + + // Check if this is a computed element which is not + // stored in the database. + $is_computed_element = (isset($computed_elements[$name])) ? TRUE : FALSE; + if ($is_computed_element && empty($element['#store'])) { + continue; + } + + if ($element['#webform_composite']) { + if (is_array($item)) { + $composite_items = (empty($element['#webform_multiple'])) ? [$item] : $item; + foreach ($composite_items as $delta => $composite_item) { + foreach ($composite_item as $property => $value) { + $rows[] = [ + 'webform_id' => $webform_id, + 'sid' => $sid, + 'name' => $name, + 'property' => $property, + 'delta' => $delta, + 'value' => (string) $value, + ]; + } + } + } + } + elseif ($element['#webform_multiple']) { + if (is_array($item)) { + foreach ($item as $delta => $value) { + $rows[] = [ + 'webform_id' => $webform_id, + 'sid' => $sid, + 'name' => $name, + 'property' => '', + 'delta' => $delta, + 'value' => (string) $value, + ]; + } + } + } + else { + $rows[] = [ + 'webform_id' => $webform_id, + 'sid' => $sid, + 'name' => $name, + 'property' => '', + 'delta' => 0, + 'value' => (string) $item, + ]; + } + } + + if ($delete_first) { + // Delete existing submission data rows. + $this->database->delete('webform_submission_data') + ->condition('sid', $sid) + ->execute(); + } + + // Insert new submission data rows. + $query = $this->database + ->insert('webform_submission_data') + ->fields(['webform_id', 'sid', 'name', 'property', 'delta', 'value']); + foreach ($rows as $row) { + $query->values($row); + } + $query->execute(); + + $webform_submission->setData($data_original); + } + + /** + * {@inheritdoc} + * + * Overwritten to avoid webform_submission->getData() before decryption, + * as this may cause issues with computed elements, e.g. computed twig. + * + * @see Drupal\webform_encrypt\WebformEncryptSubmissionStorage::loadData + * @see Drupal\webform\WebformSubmissionStorage::loadData + */ + protected function loadData(array &$webform_submissions) { + + // Load webform submission data. + if ($sids = array_keys($webform_submissions)) { + $submissions_data = []; + + // Initialize all multiple value elements to make sure a value is defined. + $webform_default_data = []; + foreach ($webform_submissions as $sid => $webform_submission) { + /** @var \Drupal\webform\WebformInterface $webform */ + $webform = $webform_submissions[$sid]->getWebform(); + $webform_id = $webform->id(); + if (!isset($webform_default_data[$webform_id])) { + $webform_default_data[$webform_id] = []; + $elements = ($webform) ? $webform->getElementsInitializedFlattenedAndHasValue() : []; + foreach ($elements as $element_key => $element) { + if (!empty($element['#webform_multiple'])) { + $webform_default_data[$webform_id][$element_key] = []; + } + } + } + $submissions_data[$sid] = $webform_default_data[$webform_id]; + } + + /** @var \Drupal\Core\Database\StatementInterface $result */ + $result = $this->database->select('webform_submission_data', 'sd') + ->fields('sd', ['webform_id', 'sid', 'name', 'property', 'delta', 'value']) + ->condition('sd.sid', $sids, 'IN') + ->orderBy('sd.sid', 'ASC') + ->orderBy('sd.name', 'ASC') + ->orderBy('sd.property', 'ASC') + ->orderBy('sd.delta', 'ASC') + ->execute(); + while ($record = $result->fetchAssoc()) { + $sid = $record['sid']; + $name = $record['name']; + + /** @var \Drupal\webform\WebformInterface $webform */ + $webform = $webform_submissions[$sid]->getWebform(); + $elements = ($webform) ? $webform->getElementsInitializedFlattenedAndHasValue() : []; + $element = $elements[$name] ?? ['#webform_multiple' => FALSE, '#webform_composite' => FALSE]; + + if ($element['#webform_composite']) { + if ($element['#webform_multiple']) { + $submissions_data[$sid][$name][$record['delta']][$record['property']] = $record['value']; + } + else { + $submissions_data[$sid][$name][$record['property']] = $record['value']; + } + } + elseif ($element['#webform_multiple']) { + $submissions_data[$sid][$name][$record['delta']] = $record['value']; + } + else { + $submissions_data[$sid][$name] = $record['value']; + } + } + + + foreach ($submissions_data as $sid => $submission_data) { + $this->decryptChildren($submission_data); + $webform_submissions[$sid]->setData($submission_data); + $webform_submissions[$sid]->setOriginalData($submission_data); + } + } + } + + /** + * {@inheritdoc} + * + * Overwritten to avoid encrypting null. + * + * @see Drupal\webform_encrypt\WebformEncryptSubmissionStorage::encryptElements + */ + public function encryptElements(array $data, WebformInterface $webform) { + // Load the configuration. + $config = $webform->getThirdPartySetting('webform_encrypt', 'element'); + + foreach ($data as $element_name => $value) { + $encryption_profile = isset($config[$element_name]) ? EncryptionProfile::load($config[$element_name]['encrypt_profile']) : FALSE; + // If the value is an array and we have a encryption profile. + if ($encryption_profile) { + if (is_array($value)) { + $this->encryptChildren($data[$element_name], $encryption_profile); + } + else { + if (is_null($value)) { + $data[$element_name] = $value; + } else { + $encrypted_value = $this->encrypt($value, $encryption_profile); + // Save the encrypted data value. + $data[$element_name] = $encrypted_value; + } + } + } + } + return $data; + } + + /** + * {@inheritdoc} + * + * Overwritten to avoid encryption, see saveData. + * + * @see Drupal\webform_encrypt\WebformEncryptSubmissionStorage::doPreSave + * @see Drupal\webform\WebformSubmissionStorage::doPreSave + * @see Drupal\Core\Entity\ContentEntityStorageBase::doPreSave + * @see Drupal\Core\Entity\EntityStorageBase::doPreSave + * + */ + protected function doPreSave(EntityInterface $entity) + { + /** @var \Drupal\webform\WebformSubmissionInterface $entity */ + + // From ContentEntityStorageBase.php. + + // Sync the changes made in the fields array to the internal values array. + $entity->updateOriginalValues(); + + if ($entity->getEntityType()->isRevisionable() && !$entity->isNew() && empty($entity->getLoadedRevisionId())) { + // Update the loaded revision id for rare special cases when no loaded + // revision is given when updating an existing entity. This for example + // happens when calling save() in hook_entity_insert(). + $entity->updateLoadedRevisionId(); + } + + $id = $entity->id(); + + // Track the original ID. + if ($entity->getOriginalId() !== NULL) { + $id = $entity->getOriginalId(); + } + + // Track if this entity exists already. + $id_exists = $this->has($id, $entity); + + // A new entity should not already exist. + if ($id_exists && $entity->isNew()) { + throw new EntityStorageException("'{$this->entityTypeId}' entity with ID '$id' already exists."); + } + + // Load the original entity, if any. + if ($id_exists && !isset($entity->original)) { + $entity->original = $this->loadUnchanged($id); + } + + // Allow code to run before saving. + $entity->preSave($this); + $this->invokeHook('presave', $entity); + + if (!$entity->isNew()) { + // If the ID changed then original can't be loaded, throw an exception + // in that case. + if (empty($entity->original) || $entity->id() != $entity->original->id()) { + throw new EntityStorageException("Update existing '{$this->entityTypeId}' entity while changing the ID is not supported."); + } + // Do not allow changing the revision ID when resaving the current + // revision. + if (!$entity->isNewRevision() && $entity->getRevisionId() != $entity->getLoadedRevisionId()) { + throw new EntityStorageException("Update existing '{$this->entityTypeId}' entity revision while changing the revision ID is not supported."); + } + } + + $this->invokeWebformElements('preSave', $entity); + $this->invokeWebformHandlers('preSave', $entity); + + return $id; + } + + /** + * {@inheritdoc} + * + * Overwritten to avoid encrypting null. + * + * @see Drupal\webform_encrypt\WebformEncryptSubmissionStorage::encryptChildren + */ + public function encryptChildren(array &$data, EncryptionProfileInterface $encryption_profile) { + foreach ($data as $key => $value) { + if (is_array($value)) { + $this->encryptChildren($data[$key], $encryption_profile); + } + elseif (is_null($value)) { + $data[$key] = $value; + } + else { + $encrypted_value = $this->encrypt($value, $encryption_profile); + $data[$key] = $encrypted_value; + } + } + } + } From 34432c565e06a9095d82d5ee9f34aa7d997dd7e0 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Tue, 25 Jun 2024 15:48:01 +0200 Subject: [PATCH 16/32] 1062: Encrypt computed elements --- composer.json | 3 +- .../os2forms_encrypt/os2forms_encrypt.module | 12 ++++ .../os2forms_encrypt.services.yml | 4 ++ .../src/Helper/Os2FormsEncryptor.php | 66 +++++++++++++++++++ ...ebformOs2FormsEncryptSubmissionStorage.php | 11 +--- 5 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 modules/os2forms_encrypt/os2forms_encrypt.services.yml create mode 100644 modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php diff --git a/composer.json b/composer.json index 15077228..38ab90b0 100644 --- a/composer.json +++ b/composer.json @@ -104,7 +104,8 @@ "2733781 - Add Export to Word Support": "https://www.drupal.org/files/issues/2019-11-22/2733781-47.patch" }, "drupal/webform": { - "Unlock possibility of using Entity print module export to Word": "https://www.drupal.org/files/issues/2020-02-29/3096552-6.patch" + "Unlock possibility of using Entity print module export to Word": "https://www.drupal.org/files/issues/2020-02-29/3096552-6.patch", + "Webform computed element post save alter": "https://www.drupal.org/files/issues/2024-06-25/webform_computed_post_save_field_alter.patch" }, "drupal/user_default_page": { "Warning: in_array() expects parameter 2 to be array, null given in user_default_page_user_logout() (https://www.drupal.org/node/3246986)": "https://www.drupal.org/files/issues/2021-11-01/user_default_page-3246986-2.patch" diff --git a/modules/os2forms_encrypt/os2forms_encrypt.module b/modules/os2forms_encrypt/os2forms_encrypt.module index 516a7fe4..9e43af31 100644 --- a/modules/os2forms_encrypt/os2forms_encrypt.module +++ b/modules/os2forms_encrypt/os2forms_encrypt.module @@ -33,3 +33,15 @@ function os2forms_encrypt_entity_type_alter(array &$entity_types): void { $entity_types['webform_submission']->setStorageClass('Drupal\os2forms_encrypt\WebformOs2FormsEncryptSubmissionStorage'); } } + +/** + * Implements hook_webform_computed_post_save_field_alter(). + * + * Ensure encryption of computed element values. + */ +function os2forms_encrypt_webform_computed_post_save_field_alter(array &$fields): void { + /** @var \Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor $os2formsEncryptor */ + $os2formsEncryptor = \Drupal::service('os2forms_encrypt.encryptor'); + + $fields['value'] = $os2formsEncryptor->encryptValue($fields['value'], $fields['name'], $fields['webform_id']); +} diff --git a/modules/os2forms_encrypt/os2forms_encrypt.services.yml b/modules/os2forms_encrypt/os2forms_encrypt.services.yml new file mode 100644 index 00000000..255a0ef5 --- /dev/null +++ b/modules/os2forms_encrypt/os2forms_encrypt.services.yml @@ -0,0 +1,4 @@ +services: + os2forms_encrypt.encryptor: + class: Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor + arguments: ['@encryption', '@entity_type.manager'] diff --git a/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php new file mode 100644 index 00000000..b254f33c --- /dev/null +++ b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php @@ -0,0 +1,66 @@ +encryptionService = $encryptService; + $this->entityTypeManager = $entityTypeManager; + } + + /** + * @param string $value + * The value that should be encrypted. + * @param string $element + * The element. + * @param string $webformId + * The webform id. + * + * @return string + * The encrypted string if element is configured to be encrypted. + */ + public function encryptValue(string $value, string $element, string $webformId): string { + /** @var WebformInterface $webform */ + $webform = $this->entityTypeManager->getStorage('webform')->load($webformId); + + $config = $webform->getThirdPartySetting('webform_encrypt', 'element'); + $encryption_profile = isset($config[$element]) ? EncryptionProfile::load($config[$element]['encrypt_profile']) : FALSE; + + if (!$encryption_profile) { + return $value; + } + + $encrypted_data = [ + 'data' => base64_encode($this->encryptionService->encrypt($value, $encryption_profile)), + 'encrypt_profile' => $encryption_profile->id(), + ]; + + return serialize($encrypted_data); + + } + +} diff --git a/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php b/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php index 0b0608fa..30242bec 100644 --- a/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php +++ b/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php @@ -342,7 +342,6 @@ protected function loadData(array &$webform_submissions) { } } - foreach ($submissions_data as $sid => $submission_data) { $this->decryptChildren($submission_data); $webform_submissions[$sid]->setData($submission_data); @@ -372,7 +371,8 @@ public function encryptElements(array $data, WebformInterface $webform) { else { if (is_null($value)) { $data[$element_name] = $value; - } else { + } + else { $encrypted_value = $this->encrypt($value, $encryption_profile); // Save the encrypted data value. $data[$element_name] = $encrypted_value; @@ -392,14 +392,9 @@ public function encryptElements(array $data, WebformInterface $webform) { * @see Drupal\webform\WebformSubmissionStorage::doPreSave * @see Drupal\Core\Entity\ContentEntityStorageBase::doPreSave * @see Drupal\Core\Entity\EntityStorageBase::doPreSave - * */ - protected function doPreSave(EntityInterface $entity) - { + protected function doPreSave(EntityInterface $entity) { /** @var \Drupal\webform\WebformSubmissionInterface $entity */ - - // From ContentEntityStorageBase.php. - // Sync the changes made in the fields array to the internal values array. $entity->updateOriginalValues(); From 717899bba15792877d89ff4cea1c826e966fdf48 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Tue, 25 Jun 2024 15:55:02 +0200 Subject: [PATCH 17/32] 1062: Code cleanup --- CHANGELOG.md | 3 +++ .../src/Helper/Os2FormsEncryptor.php | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e020e664..4b552c06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +- [#114](https://github.com/OS2Forms/os2forms/pull/114) + Encrypted computed elements. + ## [3.15.3] 2024-06-25 - [OS-74] Replacing DAWA matrikula select with Datafordeler select diff --git a/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php index b254f33c..160e21a1 100644 --- a/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php +++ b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php @@ -2,14 +2,13 @@ namespace Drupal\os2forms_encrypt\Helper; -use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\encrypt\EncryptServiceInterface; use Drupal\encrypt\Entity\EncryptionProfile; -use Drupal\webform\Entity\WebformSubmission; -use Drupal\webform\WebformInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; +/** + * The Os2FormsEncryptor class. + */ class Os2FormsEncryptor { /** @@ -26,13 +25,14 @@ class Os2FormsEncryptor { */ private EntityTypeManagerInterface $entityTypeManager; - public function __construct(EncryptServiceInterface $encryptService, EntityTypeManagerInterface $entityTypeManager) { $this->encryptionService = $encryptService; $this->entityTypeManager = $entityTypeManager; } /** + * Encrypts value if element is configured to be encrypted. + * * @param string $value * The value that should be encrypted. * @param string $element @@ -41,10 +41,10 @@ public function __construct(EncryptServiceInterface $encryptService, EntityTypeM * The webform id. * * @return string - * The encrypted string if element is configured to be encrypted. + * The resulting value. */ public function encryptValue(string $value, string $element, string $webformId): string { - /** @var WebformInterface $webform */ + /** @var \Drupal\webform\WebformInterface $webform */ $webform = $this->entityTypeManager->getStorage('webform')->load($webformId); $config = $webform->getThirdPartySetting('webform_encrypt', 'element'); From a6613f060994c0034cb307150d47b335962da93a Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Tue, 25 Jun 2024 16:23:19 +0200 Subject: [PATCH 18/32] 1062: Documentation --- modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php | 1 - .../src/WebformOs2FormsEncryptSubmissionStorage.php | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php index 160e21a1..365c0f08 100644 --- a/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php +++ b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php @@ -60,7 +60,6 @@ public function encryptValue(string $value, string $element, string $webformId): ]; return serialize($encrypted_data); - } } diff --git a/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php b/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php index 30242bec..4416f4ff 100644 --- a/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php +++ b/modules/os2forms_encrypt/src/WebformOs2FormsEncryptSubmissionStorage.php @@ -13,11 +13,16 @@ use Drupal\webform_revisions\Controller\WebformRevisionsController; /** - * This class extension WebformEncryptSubmissionStorage. + * This class extends WebformEncryptSubmissionStorage. * * This is to encrypt just the data sent to database and check if webform * revisions are enabled. * + * The reason we need to tweak the encryption made by + * WebformEncryptSubmissionStorage is that the value of computed elements, + * e.g. Computed Twig, will be attempted computed AFTER encryptio. This can + * cause all sorts of exceptions and type errors on runtime. + * * This mostly runs the same code (copied here as multiple * inherits is not a thing in PHP), with minor tweaks. */ From 012d84886e0ff3f5767e1d9e4adcfed0a4b004c5 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Thu, 27 Jun 2024 16:10:57 +0200 Subject: [PATCH 19/32] 1062: Force encryption on all elements --- modules/os2forms_encrypt/README.md | 33 ++++++++++++++ .../os2forms_encrypt/os2forms_encrypt.module | 44 +++++++++++------- .../os2forms_encrypt.services.yml | 6 ++- .../src/Commands/Os2FormsEncryptCommands.php | 5 ++- .../src/Form/SettingsForm.php | 18 ++++++++ .../src/Helper/FormHelper.php | 29 ++++++++++++ .../src/Helper/Os2FormsEncryptor.php | 45 ++++++++++++++++++- 7 files changed, 160 insertions(+), 20 deletions(-) create mode 100644 modules/os2forms_encrypt/README.md create mode 100644 modules/os2forms_encrypt/src/Helper/FormHelper.php diff --git a/modules/os2forms_encrypt/README.md b/modules/os2forms_encrypt/README.md new file mode 100644 index 00000000..bc1773ba --- /dev/null +++ b/modules/os2forms_encrypt/README.md @@ -0,0 +1,33 @@ +# OS2Forms Encrypt module + +This module extends and modifies upon [Webform Encrypt](https://www.drupal.org/project/webform_encrypt) +to provide encryption of webform element values in the database. + +## Modifications from the base Webform Encrypt module + +### Encryption time + +Any computed elements, e.g. Computed Twig, may cause issues as +their values are attempted computed after encryption. If any calculations +are done this could result in runtime TypeErrors. + +This is handled by modifying the time at which decryption is made, in +`WebformOs2FormsEncryptSubmissionStorage`. + +### Permissions + +The Webform Encrypt module introduces a `view encrypted values` permission. +This permission should be granted to roles that need to view encrypted values. + +**Note**, that in Drupal 9 and newer drush commands are ran as an +anonymous user. This means the anonymous user needs this permission, if +at any point they need values from submissions to do their job. + +### Configurable per element + +The Webform Encrypt module allows configuration on element level. That is, +webform builders can actively enable and disable for each element. + +We want all elements to be encrypted whenever encryption is enabled. +This is done by `os2forms_encrypt_webform_presave` and `os2forms_encrypt_form_alter` in +`os2forms_encrypt.module`. diff --git a/modules/os2forms_encrypt/os2forms_encrypt.module b/modules/os2forms_encrypt/os2forms_encrypt.module index 9e43af31..b25fbc70 100644 --- a/modules/os2forms_encrypt/os2forms_encrypt.module +++ b/modules/os2forms_encrypt/os2forms_encrypt.module @@ -5,23 +5,6 @@ * This module enabled webform submission encryption as a default option. */ -/** - * Implements hook_webform_element_info_alter(). - * - * Add extra processing function to "force" enabled encryption on webform - * elements when they are being saved in the UI. - */ -function os2forms_encrypt_element_info_alter(array &$definitions): void { - foreach ($definitions as $element_id => &$definition) { - if ($element_id === 'webform_element_encrypt') { - $definition['#process'][] = [ - 'Drupal\os2forms_encrypt\Element\WebformElementEncrypt', - 'processWebformElementEncrypt', - ]; - } - } -} - /** * Implements hook_entity_type_alter(). * @@ -45,3 +28,30 @@ function os2forms_encrypt_webform_computed_post_save_field_alter(array &$fields) $fields['value'] = $os2formsEncryptor->encryptValue($fields['value'], $fields['name'], $fields['webform_id']); } + +/** + * Implementes hook_webform_presave(). + * + * Enable encryption on all webform elements, whenever saved. + */ +function os2forms_encrypt_webform_presave(\Drupal\webform\WebformInterface $entity): void { + /** @var \Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor $os2formsEncryptor */ + $os2formsEncryptor = Drupal::service('os2forms_encrypt.encryptor'); + + $os2formsEncryptor->enableEncryption($entity); +} + +/** + * Implements hook_form_alter(). + * + * Removes 'element_encrypt' element from element forms. + * + * The hook_webform_presave method ensures all elements are + * configured to be encrypted, making this element redundant. + */ +function os2forms_encrypt_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, string $form_id) { + /** @var \Drupal\os2forms_encrypt\Helper\FormHelper $formHelper */ + $formHelper = Drupal::service('os2forms_encrypt.form_helper'); + + $formHelper->formAlter($form, $form_state, $form_id); +} diff --git a/modules/os2forms_encrypt/os2forms_encrypt.services.yml b/modules/os2forms_encrypt/os2forms_encrypt.services.yml index 255a0ef5..04eb9393 100644 --- a/modules/os2forms_encrypt/os2forms_encrypt.services.yml +++ b/modules/os2forms_encrypt/os2forms_encrypt.services.yml @@ -1,4 +1,8 @@ services: os2forms_encrypt.encryptor: class: Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor - arguments: ['@encryption', '@entity_type.manager'] + arguments: ['@encryption', '@entity_type.manager', '@config.factory'] + + os2forms_encrypt.form_helper: + class: Drupal\os2forms_encrypt\Helper\FormHelper + diff --git a/modules/os2forms_encrypt/src/Commands/Os2FormsEncryptCommands.php b/modules/os2forms_encrypt/src/Commands/Os2FormsEncryptCommands.php index e8920546..5dc4a2d9 100644 --- a/modules/os2forms_encrypt/src/Commands/Os2FormsEncryptCommands.php +++ b/modules/os2forms_encrypt/src/Commands/Os2FormsEncryptCommands.php @@ -56,12 +56,15 @@ public function enabledEncrypt(): void { return; } + $defaultEncryptionProfile = $config->get('default_encryption_profile'); + // Get the storage for Webform entity type. $webformStorage = $this->entityTypeManager->getStorage('webform'); // Load all webform entities. $webforms = $webformStorage->loadMultiple(); + /** @var \Drupal\webform\Entity\Webform $webform */ foreach ($webforms as $webform) { $elements = $webform->getElementsDecoded(); @@ -72,7 +75,7 @@ public function enabledEncrypt(): void { if (!isset($config['element'][$key])) { $config['element'][$key] = [ 'encrypt' => TRUE, - 'encrypt_profile' => 'webform', + 'encrypt_profile' => $defaultEncryptionProfile, ]; $changed = TRUE; } diff --git a/modules/os2forms_encrypt/src/Form/SettingsForm.php b/modules/os2forms_encrypt/src/Form/SettingsForm.php index 35722d3a..0f3f0862 100644 --- a/modules/os2forms_encrypt/src/Form/SettingsForm.php +++ b/modules/os2forms_encrypt/src/Form/SettingsForm.php @@ -58,6 +58,23 @@ public function buildForm(array $form, FormStateInterface $form_state): array { '#default_value' => $config->get('enabled'), ]; + // TODO: RESOLVE WITH DEPENDENCY INJECTION + $encryptionOptions = \Drupal::service('encrypt.encryption_profile.manager') + ->getEncryptionProfileNamesAsOptions(); + + $form['default_encryption_profile'] = [ + '#type' => 'select', + '#title' => $this->t('Default encryption profile'), + '#description' => $this->t('Upon saving webforms, elements that are not configured to be encrypted will be configured to encrypted with the selected encryption profile. The os2forms-encrypt:enable command will also use the default encryption profile.'), + '#options' => $encryptionOptions, + '#default_value' => $config->get('default_encryption_profile'), + '#states' => [ + 'visible' => [ + ':input[name="enabled"]' => ['checked' => TRUE], + ] + ] + ]; + return parent::buildForm($form, $form_state); } @@ -69,6 +86,7 @@ public function submitForm(array &$form, FormStateInterface $form_state): void { $this->config(self::$configName) ->set('enabled', $form_state->getValue('enabled')) + ->set('default_encryption_profile', $form_state->getValue('default_encryption_profile')) ->save(); } diff --git a/modules/os2forms_encrypt/src/Helper/FormHelper.php b/modules/os2forms_encrypt/src/Helper/FormHelper.php new file mode 100644 index 00000000..372f2394 --- /dev/null +++ b/modules/os2forms_encrypt/src/Helper/FormHelper.php @@ -0,0 +1,29 @@ +encryptionService = $encryptService; $this->entityTypeManager = $entityTypeManager; + $this->configFactory = $configFactory; } /** @@ -62,4 +74,35 @@ public function encryptValue(string $value, string $element, string $webformId): return serialize($encrypted_data); } + /** + * Enables encrypt on all elements of webform. + * + * @param WebformInterface $webform + * The webform. + * + * @return void + */ + public function enableEncryption(WebformInterface $webform): void { + + // Check that encryption is enabled. + $config = $this->configFactory->get(SettingsForm::$configName); + if (!$config->get('enabled') || !$webform instanceof Webform) { + return; + } + + // Check that there are any elements to enable encryption on. + $elements = $webform->getElementsDecoded(); + + if (empty($elements)) { + return; + } + + $encryptedElements = array_map(static fn () => [ + 'encrypt' => TRUE, + 'encrypt_profile' => $config->get('default_encryption_profile'), + ] , $elements); + + $webform->setThirdPartySetting('webform_encrypt', 'element', $encryptedElements); + } + } From 9b1e54e84a858438e0981d1b9996458361edc24c Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Thu, 27 Jun 2024 16:26:38 +0200 Subject: [PATCH 20/32] 1062: Use dependency injection --- .../os2forms_encrypt.services.yml | 3 ++ .../src/Form/SettingsForm.php | 28 +++++++++++++++++-- .../src/Helper/Os2FormsEncryptor.php | 2 +- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/modules/os2forms_encrypt/os2forms_encrypt.services.yml b/modules/os2forms_encrypt/os2forms_encrypt.services.yml index 04eb9393..3d474c7f 100644 --- a/modules/os2forms_encrypt/os2forms_encrypt.services.yml +++ b/modules/os2forms_encrypt/os2forms_encrypt.services.yml @@ -6,3 +6,6 @@ services: os2forms_encrypt.form_helper: class: Drupal\os2forms_encrypt\Helper\FormHelper + os2forms_encrypt.settings_form: + class: Drupal\os2forms_encrypt\Form\SettingsForm + arguments: ['@config.factory', '@encrypt.encryption_profile.manager'] diff --git a/modules/os2forms_encrypt/src/Form/SettingsForm.php b/modules/os2forms_encrypt/src/Form/SettingsForm.php index 0f3f0862..4b6edcb3 100644 --- a/modules/os2forms_encrypt/src/Form/SettingsForm.php +++ b/modules/os2forms_encrypt/src/Form/SettingsForm.php @@ -2,9 +2,12 @@ namespace Drupal\os2forms_encrypt\Form; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Link; +use Drupal\encrypt\EncryptionProfileManager; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class SettingsForm. @@ -20,6 +23,27 @@ class SettingsForm extends ConfigFormBase { */ public static string $configName = 'os2forms_encrypt.settings'; + /** + * The config factory. + * + * @var \Drupal\encrypt\EncryptionProfileManager + */ + private EncryptionProfileManager $encryptionProfileManager; + + public function __construct(ConfigFactoryInterface $config_factory, EncryptionProfileManager $encryptionProfileManager) + { + parent::__construct($config_factory); + $this->encryptionProfileManager = $encryptionProfileManager; + } + + public static function create(ContainerInterface $container) + { + return new static( + $container->get('config.factory'), + $container->get('encrypt.encryption_profile.manager') + ); + } + /** * {@inheritdoc} */ @@ -58,9 +82,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array { '#default_value' => $config->get('enabled'), ]; - // TODO: RESOLVE WITH DEPENDENCY INJECTION - $encryptionOptions = \Drupal::service('encrypt.encryption_profile.manager') - ->getEncryptionProfileNamesAsOptions(); + $encryptionOptions = $this->encryptionProfileManager->getEncryptionProfileNamesAsOptions(); $form['default_encryption_profile'] = [ '#type' => 'select', diff --git a/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php index 29048f03..3dc5df9d 100644 --- a/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php +++ b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php @@ -32,7 +32,7 @@ class Os2FormsEncryptor { /** * The config factory. * - * @var ConfigFactoryInterface + * @var \Drupal\Core\Config\ConfigFactoryInterface */ private ConfigFactoryInterface $configFactory; From 608430c72df629723674b5b413bed2223d5bdd51 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 28 Jun 2024 10:01:15 +0200 Subject: [PATCH 21/32] 1062: Coding standards --- modules/os2forms_encrypt/os2forms_encrypt.module | 9 ++++++--- .../src/Commands/Os2FormsEncryptCommands.php | 1 - modules/os2forms_encrypt/src/Form/SettingsForm.php | 13 +++++++------ modules/os2forms_encrypt/src/Helper/FormHelper.php | 11 ++++++----- .../src/Helper/Os2FormsEncryptor.php | 12 +++++------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/modules/os2forms_encrypt/os2forms_encrypt.module b/modules/os2forms_encrypt/os2forms_encrypt.module index b25fbc70..b6a9003c 100644 --- a/modules/os2forms_encrypt/os2forms_encrypt.module +++ b/modules/os2forms_encrypt/os2forms_encrypt.module @@ -5,6 +5,9 @@ * This module enabled webform submission encryption as a default option. */ +use Drupal\Core\Form\FormStateInterface; +use Drupal\webform\WebformInterface; + /** * Implements hook_entity_type_alter(). * @@ -30,11 +33,11 @@ function os2forms_encrypt_webform_computed_post_save_field_alter(array &$fields) } /** - * Implementes hook_webform_presave(). + * Implements hook_webform_presave(). * * Enable encryption on all webform elements, whenever saved. */ -function os2forms_encrypt_webform_presave(\Drupal\webform\WebformInterface $entity): void { +function os2forms_encrypt_webform_presave(WebformInterface $entity): void { /** @var \Drupal\os2forms_encrypt\Helper\Os2FormsEncryptor $os2formsEncryptor */ $os2formsEncryptor = Drupal::service('os2forms_encrypt.encryptor'); @@ -49,7 +52,7 @@ function os2forms_encrypt_webform_presave(\Drupal\webform\WebformInterface $enti * The hook_webform_presave method ensures all elements are * configured to be encrypted, making this element redundant. */ -function os2forms_encrypt_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, string $form_id) { +function os2forms_encrypt_form_alter(array &$form, FormStateInterface $form_state, string $form_id) { /** @var \Drupal\os2forms_encrypt\Helper\FormHelper $formHelper */ $formHelper = Drupal::service('os2forms_encrypt.form_helper'); diff --git a/modules/os2forms_encrypt/src/Commands/Os2FormsEncryptCommands.php b/modules/os2forms_encrypt/src/Commands/Os2FormsEncryptCommands.php index 5dc4a2d9..a34153e5 100644 --- a/modules/os2forms_encrypt/src/Commands/Os2FormsEncryptCommands.php +++ b/modules/os2forms_encrypt/src/Commands/Os2FormsEncryptCommands.php @@ -64,7 +64,6 @@ public function enabledEncrypt(): void { // Load all webform entities. $webforms = $webformStorage->loadMultiple(); - /** @var \Drupal\webform\Entity\Webform $webform */ foreach ($webforms as $webform) { $elements = $webform->getElementsDecoded(); diff --git a/modules/os2forms_encrypt/src/Form/SettingsForm.php b/modules/os2forms_encrypt/src/Form/SettingsForm.php index 4b6edcb3..e80341a8 100644 --- a/modules/os2forms_encrypt/src/Form/SettingsForm.php +++ b/modules/os2forms_encrypt/src/Form/SettingsForm.php @@ -30,14 +30,15 @@ class SettingsForm extends ConfigFormBase { */ private EncryptionProfileManager $encryptionProfileManager; - public function __construct(ConfigFactoryInterface $config_factory, EncryptionProfileManager $encryptionProfileManager) - { + public function __construct(ConfigFactoryInterface $config_factory, EncryptionProfileManager $encryptionProfileManager) { parent::__construct($config_factory); $this->encryptionProfileManager = $encryptionProfileManager; } - public static function create(ContainerInterface $container) - { + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), $container->get('encrypt.encryption_profile.manager') @@ -93,8 +94,8 @@ public function buildForm(array $form, FormStateInterface $form_state): array { '#states' => [ 'visible' => [ ':input[name="enabled"]' => ['checked' => TRUE], - ] - ] + ], + ], ]; return parent::buildForm($form, $form_state); diff --git a/modules/os2forms_encrypt/src/Helper/FormHelper.php b/modules/os2forms_encrypt/src/Helper/FormHelper.php index 372f2394..9d16b398 100644 --- a/modules/os2forms_encrypt/src/Helper/FormHelper.php +++ b/modules/os2forms_encrypt/src/Helper/FormHelper.php @@ -4,20 +4,20 @@ use Drupal\Core\Form\FormStateInterface; -class FormHelper -{ +/** + * Form helper class. + */ +class FormHelper { /** * Removes 'element_encrypt' element from element forms. * * @param array $form * The form. - * @param FormStateInterface $form_state + * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. * @param string $form_id * The form id. - * - * @return void */ public function formAlter(array &$form, FormStateInterface $form_state, string $form_id) { if ('webform_ui_element_form' === $form_id) { @@ -26,4 +26,5 @@ public function formAlter(array &$form, FormStateInterface $form_state, string $ } } } + } diff --git a/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php index 3dc5df9d..bc40911b 100644 --- a/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php +++ b/modules/os2forms_encrypt/src/Helper/Os2FormsEncryptor.php @@ -77,10 +77,8 @@ public function encryptValue(string $value, string $element, string $webformId): /** * Enables encrypt on all elements of webform. * - * @param WebformInterface $webform + * @param \Drupal\webform\WebformInterface $webform * The webform. - * - * @return void */ public function enableEncryption(WebformInterface $webform): void { @@ -94,13 +92,13 @@ public function enableEncryption(WebformInterface $webform): void { $elements = $webform->getElementsDecoded(); if (empty($elements)) { - return; + return; } $encryptedElements = array_map(static fn () => [ - 'encrypt' => TRUE, - 'encrypt_profile' => $config->get('default_encryption_profile'), - ] , $elements); + 'encrypt' => TRUE, + 'encrypt_profile' => $config->get('default_encryption_profile'), + ], $elements); $webform->setThirdPartySetting('webform_encrypt', 'element', $encryptedElements); } From e60a372ff3b24ba8a16e0d1b7d184dae97dd0194 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 28 Jun 2024 10:03:38 +0200 Subject: [PATCH 22/32] 1062: Updated CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b552c06..e3f76901 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +- [#117](https://github.com/OS2Forms/os2forms/pull/117) + Encrypts all elements if encryption enabled. - [#114](https://github.com/OS2Forms/os2forms/pull/114) Encrypted computed elements. From 6cc8f0d4288099a86b57726a08d11bf787af062e Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 28 Jun 2024 12:52:13 +0200 Subject: [PATCH 23/32] 1062: Hid encryption element --- modules/os2forms_encrypt/src/Helper/FormHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/os2forms_encrypt/src/Helper/FormHelper.php b/modules/os2forms_encrypt/src/Helper/FormHelper.php index 9d16b398..fe2166d4 100644 --- a/modules/os2forms_encrypt/src/Helper/FormHelper.php +++ b/modules/os2forms_encrypt/src/Helper/FormHelper.php @@ -22,7 +22,7 @@ class FormHelper { public function formAlter(array &$form, FormStateInterface $form_state, string $form_id) { if ('webform_ui_element_form' === $form_id) { if (isset($form['element_encrypt'])) { - unset($form['element_encrypt']); + $form['element_encrypt']['#access'] = FALSE; } } } From 3e384badb1c818680a246e43f5db04987c382d4e Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Fri, 28 Jun 2024 15:50:47 +0300 Subject: [PATCH 24/32] OS-74 updating matrikula select population --- CHANGELOG.md | 1 + .../Element/DawaElementAddressMatrikula.php | 23 ++++++++----------- .../src/Entity/DatafordelerMatrikula.php | 10 +++----- .../DataLookup/DatafordelerDataLookup.php | 22 +++++++++++++----- .../DatafordelerDataLookupInterface.php | 12 +++++----- 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3f76901..526ef4c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa Encrypts all elements if encryption enabled. - [#114](https://github.com/OS2Forms/os2forms/pull/114) Encrypted computed elements. +- [OS-74] Updating DAWA matrikula select with Datafordeler select ## [3.15.3] 2024-06-25 diff --git a/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php b/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php index 2e75805f..fdfc0ea9 100644 --- a/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php +++ b/modules/os2forms_dawa/src/Element/DawaElementAddressMatrikula.php @@ -114,22 +114,19 @@ private static function getMatrikulaOptions($addressValue, array $element) { $addressAccessId = $address->getAccessAddressId(); // Find matrikula list from the houseid (husnummer): - $matrikulaIdList = $datafordelerLookup->getMatrikulaIds($addressAccessId); + $matrikulaId = $datafordelerLookup->getMatrikulaId($addressAccessId); - // Find Matrikula entry from matrikulas ID. - if (!empty($matrikulaIdList)) { - foreach ($matrikulaIdList as $matrikulaId) { - $matrikula = $datafordelerLookup->getMatrikulaEntry($matrikulaId); + // Find Matrikula entries from matrikulas ID. + if ($matrikulaId) { + $matrikulaEnties = $datafordelerLookup->getMatrikulaEntries($matrikulaId); + foreach ($matrikulaEnties as $matrikula) { + $matrikulaOption = $matrikula->getMatrikulaNumber() . ' ' . $matrikula->getOwnershipName(); - if ($matrikula) { - $matrikulaOption = $matrikula->getMatrikulaNumber() . ' ' . $matrikula->getOwnershipName(); - - if (isset($element['#remove_code']) && !$element['#remove_code']) { - $matrikulaOption .= ' (' . $matrikula->getOwnerLicenseCode() . ')'; - } - - $options[$matrikulaOption] = $matrikulaOption; + if (isset($element['#remove_code']) && !$element['#remove_code']) { + $matrikulaOption .= ' (' . $matrikula->getOwnerLicenseCode() . ')'; } + + $options[$matrikulaOption] = $matrikulaOption; } } } diff --git a/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php b/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php index 0e05a272..41be22ac 100644 --- a/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php +++ b/modules/os2forms_dawa/src/Entity/DatafordelerMatrikula.php @@ -41,13 +41,9 @@ class DatafordelerMatrikula { * Address properties as JSON metadata. */ public function __construct(array $json) { - if (isset($json['features']) && is_array($json['features'])) { - $jordstykke = $json['features'][0]['properties']['jordstykke'][0]; - - $this->ownerLicenseCode = $jordstykke['properties']['ejerlavskode']; - $this->ownershipName = $jordstykke['properties']['ejerlavsnavn']; - $this->matrikulaNumber = $jordstykke['properties']['matrikelnummer']; - } + $this->ownerLicenseCode = $json['properties']['ejerlavskode']; + $this->ownershipName = $json['properties']['ejerlavsnavn']; + $this->matrikulaNumber = $json['properties']['matrikelnummer']; } /** diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php index a702e2d9..6f7a7cf9 100644 --- a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php @@ -2,6 +2,7 @@ namespace Drupal\os2forms_dawa\Plugin\os2web\DataLookup; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\os2forms_dawa\Entity\DatafordelerMatrikula; @@ -49,7 +50,7 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ - public function getMatrikulaIds(string $addressAccessId) : array { + public function getMatrikulaId(string $addressAccessId) : ?string { $url = "https://services.datafordeler.dk/BBR/BBRPublic/1/rest/grund"; $configuration = $this->getConfiguration(); @@ -64,16 +65,19 @@ public function getMatrikulaIds(string $addressAccessId) : array { $jsonDecoded = json_decode($json, TRUE); if (is_array($jsonDecoded)) { - return $jsonDecoded[0]['jordstykkeList']; + if (NestedArray::keyExists($jsonDecoded, [0, 'jordstykkeList', 0])) { + return NestedArray::getValue($jsonDecoded, [0, 'jordstykkeList', 0]); + } } - return []; + return NULL; } /** * {@inheritdoc} */ - public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula { + public function getMatrikulaEntries(string $matrikulaId) : array { + $matrikulaEntries = []; $url = "https://services.datafordeler.dk/Matriklen2/Matrikel/2.0.0/rest/SamletFastEjendom"; $configuration = $this->getConfiguration(); @@ -86,11 +90,17 @@ public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula ])->getBody(); $jsonDecoded = json_decode($json, TRUE); + if (is_array($jsonDecoded)) { - return new DatafordelerMatrikula($jsonDecoded); + if (NestedArray::keyExists($jsonDecoded, ['features', 0, 'properties', 'jordstykke'])) { + $jordstykker = NestedArray::getValue($jsonDecoded, ['features', 0, 'properties', 'jordstykke']); + foreach ($jordstykker as $jordstyk) { + $matrikulaEntries[] = new DatafordelerMatrikula($jordstyk); + } + } } - return NULL; + return $matrikulaEntries; } /** diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php index 4dd71a3c..5498719e 100644 --- a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php @@ -20,20 +20,20 @@ interface DatafordelerDataLookupInterface extends DataLookupInterface { * @param string $addressAccessId * Address to make search against. * - * @return array + * @return string|null * List if IDs. */ - public function getMatrikulaIds(string $addressAccessId) : array; + public function getMatrikulaId(string $addressAccessId) : ?string; /** - * Returns matrikule entry that is found byt this ID. + * Returns matrikula entries that is found byt this ID. * * @param string $matrikulaId * Id to make search against. * - * @return \Drupal\os2forms_dawa\Entity\DatafordelerMatrikula|null - * Matrikula entry or NULL. + * @return array + * Matrikula entries list. */ - public function getMatrikulaEntry(string $matrikulaId) : ?DatafordelerMatrikula; + public function getMatrikulaEntries(string $matrikulaId) : array; } From fd6a9c23a8ab5040635d98f5365d9a11a3aae744 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Wed, 3 Jul 2024 16:34:51 +0300 Subject: [PATCH 25/32] Updating address fetch service --- .../os2web/DataLookup/DatafordelerDataLookup.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php index 6f7a7cf9..2083fb1a 100644 --- a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookup.php @@ -51,22 +51,18 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function getMatrikulaId(string $addressAccessId) : ?string { - $url = "https://services.datafordeler.dk/BBR/BBRPublic/1/rest/grund"; + $url = "https://services.datafordeler.dk/DAR/DAR/3.0.0/rest/husnummerTilJordstykke"; - $configuration = $this->getConfiguration(); $json = $this->httpClient->request('GET', $url, [ 'query' => [ - 'husnummer' => $addressAccessId, - 'status' => 7, - 'username' => $configuration['username'], - 'password' => $configuration['password'], + 'husnummerid' => $addressAccessId, ], ])->getBody(); $jsonDecoded = json_decode($json, TRUE); if (is_array($jsonDecoded)) { - if (NestedArray::keyExists($jsonDecoded, [0, 'jordstykkeList', 0])) { - return NestedArray::getValue($jsonDecoded, [0, 'jordstykkeList', 0]); + if (NestedArray::keyExists($jsonDecoded, ['gældendeJordstykke', 'jordstykkeLokalId'])) { + return NestedArray::getValue($jsonDecoded, ['gældendeJordstykke', 'jordstykkeLokalId']); } } From cb6512f318ad3d888a7b7287d09aa2dbcc15f549 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Mon, 8 Jul 2024 13:26:04 +0300 Subject: [PATCH 26/32] Codereview fixes --- .../src/Plugin/WebformElement/NemidChildrenRadios.php | 4 ++-- .../src/Plugin/WebformElement/NemidChildrenSelect.php | 4 ++-- .../WebformElement/ServiceplatformenCprElementBase.php | 10 +++++----- modules/os2forms_nemid/src/Service/FormsHelper.php | 10 +--------- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php index 688c6435..b6f38769 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php @@ -142,7 +142,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f if (is_array($children) && !empty($children)) { foreach ($children as $child) { if ($child['nameAddressProtected']) { - $options[$child['cpr']] = $child['cpr'] . ' (Navne- og adressebeskyttet)'; + $options[$child['cpr']] = $child['cpr'] . ' (' . t('Name and address protection') . ')'; $showAddressNameProtectionMessage = TRUE; } else { @@ -156,7 +156,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f $element['#options'] = $options; if ($showAddressNameProtectionMessage) { - $element['#suffix'] = $element['#address_protection_help_text']; + $element['#suffix'] = '
' . $element['#address_protection_help_text'] . '
'; } } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php index b49cc3f4..9b41ff38 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php @@ -142,7 +142,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f if (is_array($children) && !empty($children)) { foreach ($children as $child) { if ($child['nameAddressProtected']) { - $options[$child['cpr']] = $child['cpr'] . ' (Navne- og adressebeskyttet)'; + $options[$child['cpr']] = $child['cpr'] . ' (' . t('Name and address protection') . ')'; $showAddressNameProtectionMessage = TRUE; } else { @@ -156,7 +156,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f $element['#options'] = $options; if ($showAddressNameProtectionMessage) { - $element['#suffix'] = $element['#address_protection_help_text']; + $element['#suffix'] = '
' . $element['#address_protection_help_text'] . '
'; } } diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php b/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php index e77530a3..18e50004 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php @@ -31,23 +31,23 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f // Appending name and address protection. if (!empty($value)) { - $element['#value'] .= $this->appendNameAddressProtectedText($cprLookupResult); + $element['#value'] .= $this->getNameAddressProtectedText($cprLookupResult); } } } } /** - * Appends name/address protected text, if person has name/address protection. + * Returns name/address protected text, if person has name/address protection. * * @param \Drupal\os2web_datalookup\LookupResult\CprLookupResult $cprLookupResult * Initialized CprLooupResult. * * @return string - * String " (Navne- og adressebeskyttet)" or nothing. + * String indicating Name and Address protection or nothing. */ - public function appendNameAddressProtectedText(CprLookupResult $cprLookupResult) { - return $cprLookupResult->isNameAddressProtected() ? ' (Navne- og adressebeskyttet)' : ''; + public function getNameAddressProtectedText(CprLookupResult $cprLookupResult) { + return $cprLookupResult->isNameAddressProtected() ? ' (' . t('Name and address protection') . ')' : ''; } } diff --git a/modules/os2forms_nemid/src/Service/FormsHelper.php b/modules/os2forms_nemid/src/Service/FormsHelper.php index 55ace01b..0fd62550 100644 --- a/modules/os2forms_nemid/src/Service/FormsHelper.php +++ b/modules/os2forms_nemid/src/Service/FormsHelper.php @@ -139,15 +139,7 @@ public function retrieveCprLookupResult(FormStateInterface $form_state) { * CPRLookupResult or NULL. */ public static function retrieveCachedCprLookupResult(FormStateInterface $form_state) { - /** @var \Drupal\os2web_datalookup\LookupResult\CprLookupResult $cprLookupResult */ - $cprLookupResult = NULL; - - // Trying to fetch person data from cache. - if ($form_state->has('cprLookupResult')) { - $cprLookupResult = $form_state->get('cprLookupResult'); - } - - return $cprLookupResult; + return $form_state->has('cprLookupResult') ? $form_state->get('cprLookupResult') : NULL; } /** From 58fe8c1097e915799d690aa3c528c3f38d035ebd Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Mon, 8 Jul 2024 13:40:42 +0300 Subject: [PATCH 27/32] Adding translation options --- .../MitidChildrenSelectAjaxBehaviour.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php index 2badc955..d57e973d 100644 --- a/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php +++ b/modules/os2forms_nemid/src/Element/MitidChildrenSelectAjaxBehaviour.php @@ -59,7 +59,7 @@ public static function mitidChildrenSelectAjax(array &$form, FormStateInterface switch ($element['#type']) { case 'os2forms_mitid_child_name': - $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getName() : 'Navne- og adressebeskyttet'; + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getName() : t('Name and address protected'); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-name', $element)); break; @@ -69,47 +69,47 @@ public static function mitidChildrenSelectAjax(array &$form, FormStateInterface break; case 'os2forms_mitid_child_address': - $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getAddress() : 'Navne- og adressebeskyttet'; + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getAddress() : t('Name and address protected'); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-address', $element)); break; case 'os2forms_mitid_child_apartment_nr': - $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getApartmentNr() : 'Navne- og adressebeskyttet'; + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getApartmentNr() : t('Name and address protected'); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-apartment-nr', $element)); break; case 'os2forms_mitid_child_city': - $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getCity() : 'Navne- og adressebeskyttet'; + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getCity() : t('Name and address protected'); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-city', $element)); break; case 'os2forms_mitid_child_coaddress': - $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getCoName() : 'Navne- og adressebeskyttet'; + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getCoName() : t('Name and address protected'); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-coaddress', $element)); break; case 'os2forms_mitid_child_floor': - $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getFloor() : 'Navne- og adressebeskyttet'; + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getFloor() : t('Name and address protected'); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-floor', $element)); break; case 'os2forms_mitid_child_house_nr': - $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getHouseNr() : 'Navne- og adressebeskyttet'; + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getHouseNr() : t('Name and address protected'); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-house-nr', $element)); break; case 'os2forms_mitid_child_kommunekode': - $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getMunicipalityCode() : 'Navne- og adressebeskyttet'; + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getMunicipalityCode() : t('Name and address protected'); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-kommunekode', $element)); break; case 'os2forms_mitid_child_postal_code': - $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getPostalCode() : 'Navne- og adressebeskyttet'; + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getPostalCode() : t('Name and address protected'); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-postal-code', $element)); break; case 'os2forms_mitid_child_street': - $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getStreet() : 'Navne- og adressebeskyttet'; + $element['#value'] = !$cprLookupResult->isNameAddressProtected() ? $cprLookupResult->getStreet() : t('Name and address protected'); $response->addCommand(new ReplaceCommand('.js-form-type-os2forms-mitid-child-street', $element)); break; From 4359ca976dc8c0f070d5224f3516b496ca850bdf Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Mon, 8 Jul 2024 16:40:00 +0200 Subject: [PATCH 28/32] Release 3.15.4 --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3f76901..1901caaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +## [3.15.4] 2024-07-08 + - [#117](https://github.com/OS2Forms/os2forms/pull/117) Encrypts all elements if encryption enabled. - [#114](https://github.com/OS2Forms/os2forms/pull/114) @@ -239,7 +241,9 @@ before starting to add changes. Use example [placed in the end of the page](#exa - Security in case of vulnerabilities. ``` -[Unreleased]: https://github.com/OS2Forms/os2forms/compare/3.15.2...HEAD +[Unreleased]: https://github.com/OS2Forms/os2forms/compare/3.15.4...HEAD +[3.15.4]: https://github.com/OS2Forms/os2forms/compare/3.15.3...3.15.4 +[3.15.3]: https://github.com/OS2Forms/os2forms/compare/3.15.2...3.15.3 [3.15.2]: https://github.com/OS2Forms/os2forms/compare/3.15.1...3.15.2 [3.15.1]: https://github.com/OS2Forms/os2forms/compare/3.15.0...3.15.1 [3.15.0]: https://github.com/OS2Forms/os2forms/compare/3.14.1...3.15.0 From 4357aa7a7942a7f4a0b825e85ca8f643b4476e38 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Tue, 9 Jul 2024 13:35:39 +0300 Subject: [PATCH 29/32] Removing extra use statement --- .../Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php index 5498719e..16947249 100644 --- a/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php +++ b/modules/os2forms_dawa/src/Plugin/os2web/DataLookup/DatafordelerDataLookupInterface.php @@ -2,7 +2,6 @@ namespace Drupal\os2forms_dawa\Plugin\os2web\DataLookup; -use Drupal\os2forms_dawa\Entity\DatafordelerMatrikula; use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterface; /** From 345a9fdecfce3a26978a0f1ef89416fc6ce1331d Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Thu, 11 Jul 2024 13:45:22 +0300 Subject: [PATCH 30/32] S2FRMS-40 Using $this->t() instead of t() --- .../src/Plugin/WebformElement/NemidChildrenRadios.php | 2 +- .../src/Plugin/WebformElement/NemidChildrenSelect.php | 2 +- .../Plugin/WebformElement/ServiceplatformenCprElementBase.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php index b6f38769..fb88f35f 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenRadios.php @@ -142,7 +142,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f if (is_array($children) && !empty($children)) { foreach ($children as $child) { if ($child['nameAddressProtected']) { - $options[$child['cpr']] = $child['cpr'] . ' (' . t('Name and address protection') . ')'; + $options[$child['cpr']] = $child['cpr'] . ' (' . $this->t('Name and address protection') . ')'; $showAddressNameProtectionMessage = TRUE; } else { diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php index 9b41ff38..eb12d834 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidChildrenSelect.php @@ -142,7 +142,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f if (is_array($children) && !empty($children)) { foreach ($children as $child) { if ($child['nameAddressProtected']) { - $options[$child['cpr']] = $child['cpr'] . ' (' . t('Name and address protection') . ')'; + $options[$child['cpr']] = $child['cpr'] . ' (' . $this->t('Name and address protection') . ')'; $showAddressNameProtectionMessage = TRUE; } else { diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php b/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php index 18e50004..3b23d518 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/ServiceplatformenCprElementBase.php @@ -47,7 +47,7 @@ public function handleElementPrepopulate(array &$element, FormStateInterface &$f * String indicating Name and Address protection or nothing. */ public function getNameAddressProtectedText(CprLookupResult $cprLookupResult) { - return $cprLookupResult->isNameAddressProtected() ? ' (' . t('Name and address protection') . ')' : ''; + return $cprLookupResult->isNameAddressProtected() ? ' (' . $this->t('Name and address protection') . ')' : ''; } } From 0a39f1da264d61da133509eccc22fbdfd7c750e0 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Fri, 12 Jul 2024 11:47:18 +0300 Subject: [PATCH 31/32] Release 3.15.5 --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c78e07c..52b59afb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,10 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] -- [S2FRMS-40] Adding child select autopopulate fields +## [3.15.5] 2024-07-12 + +- [#111](https://github.com/OS2Forms/os2forms/pull/111) + Adding child select autopopulate fields ## [3.15.4] 2024-07-08 From 8d92335e2bc27292a3e4770ffa31974af3349255 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Tue, 16 Jul 2024 11:30:50 +0300 Subject: [PATCH 32/32] Release 3.15.6 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfb1418e..6763596e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +## [3.15.6] 2024-07-16 + +- [#120](https://github.com/OS2Forms/os2forms/pull/120) + S2FRMS-100 / OS-74 - changing address fetch API + ## [3.15.5] 2024-07-12 - [#111](https://github.com/OS2Forms/os2forms/pull/111)