From 95b758181c586a5b087848126677a5b5db50e249 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Mon, 29 Jan 2024 17:56:35 +0530 Subject: [PATCH] Fix state display on preview --- src/Plugin/WebformElement/CivicrmOptions.php | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Plugin/WebformElement/CivicrmOptions.php b/src/Plugin/WebformElement/CivicrmOptions.php index e8cd2c58c..145c1f050 100644 --- a/src/Plugin/WebformElement/CivicrmOptions.php +++ b/src/Plugin/WebformElement/CivicrmOptions.php @@ -321,4 +321,33 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form } } + /** + * @inheritDoc + */ + protected function format($type, array &$element, WebformSubmissionInterface $webform_submission, array $options = []) { + $value = parent::format($type, $element, $webform_submission, $options); + $format = $this->getItemFormat($element); + if (!str_ends_with($element['#form_key'], '_address_state_province_id')) { + return $value; + } + if ($type === 'Text') { + $state_id = $value; + } + else { + $state_id = $value['#plain_text'] ?? NULL; + } + if ($format === 'raw' || empty($state_id) || !is_numeric($state_id)) { + return $value; + } + $utils = \Drupal::service('webform_civicrm.utils'); + $state = $utils->wf_crm_apivalues('state_province', 'get', ['id' => $state_id], 'name'); + if (!empty($state[$state_id])) { + if ($type === 'Text') { + return $state[$state_id]; + } + $value['#plain_text'] = $state[$state_id]; + } + return $value; + } + }