From eafb10e40685452827bb1f3fb824a224848dfabd Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Mon, 23 Mar 2020 11:50:31 +0200 Subject: [PATCH] SGI-974-78952 nemid fields PDF visibility --- .../WebformElement/NemidElementBase.php | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidElementBase.php b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidElementBase.php index 41e70274..5316aabc 100644 --- a/modules/os2forms_nemid/src/Plugin/WebformElement/NemidElementBase.php +++ b/modules/os2forms_nemid/src/Plugin/WebformElement/NemidElementBase.php @@ -5,6 +5,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Form\FormStateInterface; use Drupal\webform\Plugin\WebformElementBase; +use Drupal\webform\WebformSubmissionInterface; /** * Provides a abstract NemID Element. @@ -80,6 +81,27 @@ public function alterForm(array &$element, array &$form, FormStateInterface $for } } + /** + * {@inheritdoc} + */ + protected function build($format, array &$element, WebformSubmissionInterface $webform_submission, array $options = []) { + // Getting webform type settings. + $webform = $webform_submission->getWebform(); + $webformNemidSettings = $webform->getThirdPartySetting('os2forms', 'os2forms_nemid'); + $webform_type = NULL; + + // If webform type is set, handle element visiblity. + if (isset($webformNemidSettings['webform_type'])) { + $webform_type = $webformNemidSettings['webform_type']; + + if (!$this->isVisible($webform_type)) { + return NULL; + } + } + + return parent::build($format, $element, $webform_submission, $options); + } + /** * {@inheritdoc} */ @@ -105,7 +127,10 @@ public function form(array $form, FormStateInterface $form_state) { /** * Handles element visibility on the webform. * - * If element type is not corresponding with the form type, element if hidden. + * If element type is not corresponding with the form type, element #access + * attribute is set to FALSE. + * Otherwise #access is not modified (prevents unwanted display of elements + * that were hidden otherwise). * * @param array $element * Array element info. @@ -113,16 +138,33 @@ public function form(array $form, FormStateInterface $form_state) { * Allowed type of the element. */ protected function handleElementVisibility(array &$element, $allowed_type) { + if (!$this->isVisible($allowed_type)) { + $element['#access'] = FALSE; + } + } + + /** + * Checks is this element is allowed to be displayed. + * + * @param string $allowed_type + * Allowed type of the element. + * + * @return bool + * TRUE if visible, FALSE otherwise. + */ + protected function isVisible($allowed_type) { if ($allowed_type === OS2FORMS_NEMID_WEBFORM_TYPE_PERSONAL) { if ($this instanceof NemidElementCompanyInterface) { - $element['#access'] = FALSE; + return FALSE; } } elseif ($allowed_type === OS2FORMS_NEMID_WEBFORM_TYPE_COMPANY) { if ($this instanceof NemidElementPersonalInterface) { - $element['#access'] = FALSE; + return FALSE; } } + + return TRUE; } }