diff --git a/src/Plugin/WebformElement/CivicrmOptions.php b/src/Plugin/WebformElement/CivicrmOptions.php index e8cd2c58c..14cd9de79 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'] ?? $value['#markup'] ?? 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; + } + } diff --git a/tests/src/FunctionalJavascript/ExistingContactElementTest.php b/tests/src/FunctionalJavascript/ExistingContactElementTest.php index cc34856e1..5cc42d780 100644 --- a/tests/src/FunctionalJavascript/ExistingContactElementTest.php +++ b/tests/src/FunctionalJavascript/ExistingContactElementTest.php @@ -317,6 +317,12 @@ public function testTokensInEmail() { $this->assertSession()->checkboxChecked("civicrm_1_contact_1_email_email"); $this->getSession()->getPage()->selectFieldOption('civicrm_1_contact_1_email_location_type_id', 'Main'); + // Enable Address fields. + $this->getSession()->getPage()->selectFieldOption('contact_1_number_of_address', 1); + $this->assertSession()->assertWaitOnAjaxRequest(); + $this->getSession()->getPage()->checkField('Country'); + $this->assertSession()->checkboxChecked('Country'); + $this->getSession()->getPage()->clickLink('Activities'); $this->getSession()->getPage()->selectFieldOption('activity_number_of_activity', 2); $this->assertSession()->assertWaitOnAjaxRequest(); @@ -326,7 +332,7 @@ public function testTokensInEmail() { $email = [ 'to_mail' => '[webform_submission:values:civicrm_1_contact_1_email_email:raw]', - 'body' => 'Submitted Values Are - [webform_submission:values] Existing Contact - [webform_submission:values:civicrm_1_contact_1_contact_existing]. Activity 1 ID - [webform_submission:activity-id:1]. Activity 2 ID - [webform_submission:activity-id:2]. Webform CiviCRM Contacts IDs - [webform_submission:contact-id:1]. Webform CiviCRM Contacts Links - [webform_submission:contact-link:1].', + 'body' => 'Submitted Values Are - [webform_submission:values] Existing Contact - [webform_submission:values:civicrm_1_contact_1_contact_existing]. Activity 1 ID - [webform_submission:activity-id:1]. Activity 2 ID - [webform_submission:activity-id:2]. Webform CiviCRM Contacts IDs - [webform_submission:contact-id:1]. Webform CiviCRM Contacts Links - [webform_submission:contact-link:1] Country - [webform_submission:values:civicrm_1_contact_1_address_country_id]. State/Province - [webform_submission:values:civicrm_1_contact_1_address_state_province_id].', ]; $this->addEmailHandler($email); $this->drupalGet($this->webform->toUrl('handlers')); @@ -342,6 +348,24 @@ public function testTokensInEmail() { $this->getSession()->getPage()->fillField('Last Name', 'Pabst'); $this->getSession()->getPage()->fillField('Email', 'frederick@pabst.io'); + $countryID = $this->utils->wf_civicrm_api4('Country', 'get', [ + 'where' => [ + ['name', '=', 'United States'], + ], + ], 0)['id']; + $stateProvinceID = $this->utils->wf_civicrm_api4('StateProvince', 'get', [ + 'where' => [ + ['abbreviation', '=', 'NJ'], + ['country_id', '=', $countryID], + ], + ], 0)['id']; + $this->getSession()->getPage()->fillField('Street Address', '123 Milwaukee Ave'); + $this->getSession()->getPage()->fillField('City', 'Milwaukee'); + $this->getSession()->getPage()->fillField('Postal Code', '53177'); + $this->getSession()->getPage()->selectFieldOption('Country', $countryID); + $this->getSession()->wait(1000); + $this->getSession()->getPage()->selectFieldOption('State/Province', $stateProvinceID); + $this->getSession()->getPage()->pressButton('Submit'); $this->assertPageNoErrorMessages(); $this->assertSession()->pageTextContains('New submission added to CiviCRM Webform Test.'); @@ -366,11 +390,22 @@ public function testTokensInEmail() { Frederick *Last Name* Pabst +*Street Address* +123 Milwaukee Ave +*City* +Milwaukee +*Postal Code* +53177 +*Country* +United States +*State/Province* +New Jersey *Email* frederick@pabst.io [1] Existing Contact - Frederick Pabst. Activity 1 ID - {$actID1}. Activity 2 ID - {$actID2}. Webform CiviCRM Contacts IDs - {$this->rootUserCid}. Webform CiviCRM Contacts Links - -{$cidURL}. +{$cidURL} Country - United +States. State/Province - New Jersey. [1] mailto:frederick@pabst.io ", $sent_email[0]['body']);