Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix state display on preview #941

Merged
merged 4 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Plugin/WebformElement/CivicrmOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
39 changes: 37 additions & 2 deletions tests/src/FunctionalJavascript/ExistingContactElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'));
Expand All @@ -342,6 +348,24 @@ public function testTokensInEmail() {
$this->getSession()->getPage()->fillField('Last Name', 'Pabst');
$this->getSession()->getPage()->fillField('Email', '[email protected]');

$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.');
Expand All @@ -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*
[email protected] [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:[email protected]
", $sent_email[0]['body']);
Expand Down