From bf9db8044f1b51d6a732f8dfbf17b6227855b11b Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Mon, 7 Aug 2023 16:10:02 +0200
Subject: [PATCH 1/2] imp: I-7731 configure email from address
---
config/schema/dsr_civicrm.schema.yml | 5 ++++-
dsr_civicrm.links.menu.yml | 6 ++++++
src/DsrCivicrmService.php | 22 +++++++++++++++++++---
src/Form/DsrCivicrmConfigForm.php | 23 +++++++++++++++++++++++
4 files changed, 52 insertions(+), 4 deletions(-)
create mode 100644 dsr_civicrm.links.menu.yml
diff --git a/config/schema/dsr_civicrm.schema.yml b/config/schema/dsr_civicrm.schema.yml
index 5ad5955..2d5f6cf 100644
--- a/config/schema/dsr_civicrm.schema.yml
+++ b/config/schema/dsr_civicrm.schema.yml
@@ -12,6 +12,8 @@ dsr_civicrm.settings:
type: string
civicrm_pdf_template_id:
type: string
+ from_email_address:
+ type: string
languages:
type: sequence
sequence:
@@ -23,6 +25,8 @@ dsr_civicrm.settings:
type: string
civicrm_pdf_template_id:
type: string
+ from_email_address:
+ type: string
civicrm_custom_fields:
type: sequence
sequence:
@@ -36,4 +40,3 @@ dsr_civicrm.settings:
type: string
civicrm_mobile_field:
type: string
-
diff --git a/dsr_civicrm.links.menu.yml b/dsr_civicrm.links.menu.yml
new file mode 100644
index 0000000..3aa4e54
--- /dev/null
+++ b/dsr_civicrm.links.menu.yml
@@ -0,0 +1,6 @@
+dsr_civicrm.config:
+ title: 'DSR Civicrm Settings'
+ route_name: dsr_civicrm.dsr_civicrm_config_form
+ description: 'Configure DSR Civicrm.'
+ parent: system.admin_config_shareholder_register
+ weight: 50
diff --git a/src/DsrCivicrmService.php b/src/DsrCivicrmService.php
index 612fb38..8b8a62f 100644
--- a/src/DsrCivicrmService.php
+++ b/src/DsrCivicrmService.php
@@ -198,15 +198,31 @@ class DsrCivicrmService implements EventSubscriberInterface, DsrCivicrmServiceIn
'MessageTemplate', [
'sequential' => 1,
'workflow_name' => ['IS NULL' => 1],
- 'options' => [
- 'limit' => 1000,
- ],
+ 'options' => [
+ 'limit' => 1000,
+ ],
]
);
return $templates;
}
+ /**
+ * {@inheritdoc}
+ */
+ function getFromAddresses() {
+ $civi = \Drupal::service('civicrm_tools.api');
+ $addresses = $civi->get(
+ 'OptionValue', [
+ 'sequential' => 1,
+ 'option_group_id' => "from_email_address",
+ ]
+ );
+
+ return $addresses;
+ }
+
+
/**
* {@inheritdoc}
*/
diff --git a/src/Form/DsrCivicrmConfigForm.php b/src/Form/DsrCivicrmConfigForm.php
index 6c92896..db10379 100644
--- a/src/Form/DsrCivicrmConfigForm.php
+++ b/src/Form/DsrCivicrmConfigForm.php
@@ -43,6 +43,12 @@ class DsrCivicrmConfigForm extends ConfigFormBase {
$template_options[$available_template['id']] = $available_template['msg_title'];
}
+ $available_from_addresses = \Drupal::service('dsr_civicrm.default')->getFromAddresses();
+ $from_address_options = [];
+ foreach ($available_from_addresses as $available_from_address) {
+ $from_address_options[$available_from_address['id']] = $available_from_address['label'];
+ }
+
$form['civicrm_phone_field'] = [
'#type' => 'textfield',
'#title' => $this->t('Phone field'),
@@ -87,6 +93,14 @@ class DsrCivicrmConfigForm extends ConfigFormBase {
'#title' => $this->t('Civicrm pdf template ID'),
'#default_value' => $item_id === 'new' ? '' : $item['civicrm_pdf_template_id'],
];
+ $form['mail-templates'][$item_id]['from_email_address'] = [
+ '#type' => 'select',
+ '#options' => $from_address_options,
+ '#empty_value' => '',
+ '#title' => $this->t('Civicrm from email address'),
+ '#default_value' => $item_id === 'new' ? '' : $item['from_email_address'],
+ ];
+
$form['mail-templates'][$item_id]['languages'] = [
'#type' => 'details',
'#title' => $this->t("Language overrides"),
@@ -117,6 +131,14 @@ class DsrCivicrmConfigForm extends ConfigFormBase {
'#title' => $this->t('Civicrm pdf template ID'),
'#default_value' => $item_id === 'new' || !isset($lang_config[$langcode]) ? '' : $lang_config[$langcode]['civicrm_pdf_template_id'],
];
+ $form['mail-templates'][$item_id]['languages'][$langcode]['from_email_address'] = [
+ '#type' => 'select',
+ '#options' => $from_address_options,
+ '#empty_value' => '',
+ '#title' => $this->t('Civicrm from email address'),
+ '#default_value' => $item_id === 'new' || !isset($lang_config[$langcode]) ? '' : $lang_config[$langcode]['from_email_address'],
+ ];
+
}
}
@@ -183,6 +205,7 @@ class DsrCivicrmConfigForm extends ConfigFormBase {
'drupal_mail_id' => $v['drupal_mail_id'],
'civicrm_template_id' => $v['civicrm_template_id'],
'civicrm_pdf_template_id' => $v['civicrm_pdf_template_id'],
+ 'from_email_address' => $v['from_email_address'],
'languages' => $languages,
];
}
--
GitLab
From 25d8df02261d3042cb2082852c063029d05b2a22 Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Mon, 7 Aug 2023 15:22:29 +0000
Subject: [PATCH 2/2] fix: use configured from address in api
---
dsr_civicrm.module | 40 +++++++++++++++++++++++++++------------
src/DsrCivicrmService.php | 13 +++++++++++++
2 files changed, 41 insertions(+), 12 deletions(-)
diff --git a/dsr_civicrm.module b/dsr_civicrm.module
index f76fbf0..f8e51fe 100644
--- a/dsr_civicrm.module
+++ b/dsr_civicrm.module
@@ -566,6 +566,7 @@ function dsr_civicrm_mail_alter(&$message) {
$mail_template = $template['civicrm_template_id'];
$pdf_template = $template['civicrm_pdf_template_id'];
+ $from_address_id = $template['from_email_address'];
// Find language override.
@@ -585,6 +586,9 @@ function dsr_civicrm_mail_alter(&$message) {
if ($lang['civicrm_pdf_template_id']) {
$pdf_template = $lang['civicrm_pdf_template_id'];
}
+ if ($lang['from_email_address']) {
+ $from_address_id = $lang['from_email_address'];
+ }
break;
}
}
@@ -600,24 +604,36 @@ function dsr_civicrm_mail_alter(&$message) {
}
);
+ $api_data = [];
+ if ($from_address_id) {
+ $mail_match = [];
+ $mail_str = \Drupal::service('dsr_civicrm.default')->getFromAddress($from_address_id);
+ preg_match('!"(.*?)"\s+<\s*(.*?)\s*>!', $mail_str, $mail_match);
+ $api_data['from_name'] = $mail_match[1];
+ $api_data['from_email'] = $mail_match[2];
+ }
+
if ($pdf_template) {
+ $pdf_data = $api_data + [
+ 'contact_id' => $contactId,
+ 'extra_data' => $extra_data,
+ 'template_id' => $pdf_template,
+ 'body_template_id' => $mail_template,
+ 'email_activity' => TRUE,
+ ];
$result = civicrm_api3(
- 'Pdf', 'Create', [
- 'contact_id' => $contactId,
- 'extra_data' => $extra_data,
- 'template_id' => $pdf_template,
- 'body_template_id' => $mail_template,
- 'email_activity' => TRUE,
- ]
+ 'Pdf', 'Create', $pdf_data
);
}
else {
+ $mail_data = $api_data + [
+ 'contact_id' => $contactId,
+ 'extra_data' => $extra_data,
+ 'template_id' => $mail_template,
+ // 'from_email_option' => $from_address_id,
+ ];
$result = civicrm_api3(
- 'Email', 'send', [
- 'contact_id' => $contactId,
- 'extra_data' => $extra_data,
- 'template_id' => $mail_template,
- ]
+ 'Email', 'send', $mail_data
);
}
$message['send'] = FALSE;
diff --git a/src/DsrCivicrmService.php b/src/DsrCivicrmService.php
index 8b8a62f..8cc3bad 100644
--- a/src/DsrCivicrmService.php
+++ b/src/DsrCivicrmService.php
@@ -222,6 +222,19 @@ class DsrCivicrmService implements EventSubscriberInterface, DsrCivicrmServiceIn
return $addresses;
}
+ /**
+ * {@inheritdoc}
+ */
+ function getFromAddress($address_id) {
+ $civi = \Drupal::service('civicrm_tools.api');
+ $addresses = $civi->get(
+ 'OptionValue', [
+ 'sequential' => 1,
+ 'id' => $address_id,
+ ]
+ );
+ return $addresses[0]['label'];
+ }
/**
* {@inheritdoc}
--
GitLab