diff --git a/config/install/ucb_site_configuration.configuration.yml b/config/install/ucb_site_configuration.configuration.yml index 717901c..e66e6d6 100644 --- a/config/install/ucb_site_configuration.configuration.yml +++ b/config/install/ucb_site_configuration.configuration.yml @@ -215,3 +215,14 @@ external_services: availability: content: true sitewide: true + salesforce: + label: Salesforce Enhanced Chat + content_label: Salesforce Enhanced Chat + settings: + - salesforce_id + - embedded_service_name + - endpoint_url + - scrt2_url + availability: + content: true + sitewide: true diff --git a/js/service-salesforce.js b/js/service-salesforce.js new file mode 100644 index 0000000..59bcd8c --- /dev/null +++ b/js/service-salesforce.js @@ -0,0 +1,33 @@ +(function (drupalSettings) { + const settings = drupalSettings.service_salesforce?.[0]; + if (!settings) return; + + function initEmbeddedMessaging() { + try { + embeddedservice_bootstrap.settings.language = 'en_US'; + + embeddedservice_bootstrap.init( + settings.salesforce_id, + settings.embedded_service_name, + settings.endpoint_url, + { + scrt2URL: settings.scrt2_url + } + ); + } catch (err) { + console.error('Error loading Embedded Messaging:', err); + } + } + + function loadSalesforceBootstrap() { + const endpoint = settings.endpoint_url.replace(/\/$/, ''); + const script = document.createElement('script'); + script.src = `${endpoint}/assets/js/bootstrap.min.js`; + script.type = 'text/javascript'; + script.onload = initEmbeddedMessaging; + script.onerror = () => console.error('Failed to load Salesforce bootstrap.min.js'); + document.head.appendChild(script); + } + + loadSalesforceBootstrap(); +})(window.drupalSettings); diff --git a/src/Form/ExternalServiceIncludeEntityForm.php b/src/Form/ExternalServiceIncludeEntityForm.php index e337bc3..d911338 100644 --- a/src/Form/ExternalServiceIncludeEntityForm.php +++ b/src/Form/ExternalServiceIncludeEntityForm.php @@ -194,6 +194,24 @@ public function validateForm(array &$form, FormStateInterface $form_state) { } break; + case 'salesforce': + $idField = $externalServiceName . '__salesforce_id'; + $nameField = $externalServiceName . '__embedded_service_name'; + $endpointField = $externalServiceName . '__endpoint_url'; + $scrt2Field = $externalServiceName . '__scrt2_url'; + + $endpointUrl = $form_state->getValue($endpointField); + if (!filter_var($endpointUrl, FILTER_VALIDATE_URL) || strpos($endpointUrl, 'https://cu.my.site.com') !== 0) { + $form_state->setErrorByName($endpointField, $this->t('The Endpoint URL must start with https://cu.my.site.com')); + } + + $scrt2Url = $form_state->getValue($scrt2Field); + if (!filter_var($scrt2Url, FILTER_VALIDATE_URL) || strpos($scrt2Url, 'https://cu.my.salesforce-scrt.com') !== 0) { + $form_state->setErrorByName($scrt2Field, $this->t('The SCRT2 URL must start with https://cu.my.salesforce-scrt.com')); + } + + break; + default: $form_state->setErrorByName('service_name', $this->t('Unrecognized third-party service selected.')); } @@ -370,7 +388,61 @@ private function buildExternalServiceSiteSettingsForm(array &$form, $externalSer ], ]; break; + case 'salesforce': + $form[$externalServiceName . '__salesforce_id'] = [ + '#type' => 'textfield', + '#size' => 24, + '#maxlength' => 24, + '#title' => $this->t('Salesforce Org ID'), + '#default_value' => $externalServiceSettings['salesforce_id'] ?? '', + '#required' => TRUE, + '#states' => [ + 'visible' => [ + ':input[name="service_name"]' => ['value' => $externalServiceName], + ], + ], + ]; + $form[$externalServiceName . '__embedded_service_name'] = [ + '#type' => 'textfield', + '#size' => 64, + '#maxlength' => 64, + '#title' => $this->t('Embedded Service Name'), + '#default_value' => $externalServiceSettings['embedded_service_name'] ?? '', + '#required' => TRUE, + '#states' => [ + 'visible' => [ + ':input[name="service_name"]' => ['value' => $externalServiceName], + ], + ], + ]; + + $form[$externalServiceName . '__endpoint_url'] = [ + '#type' => 'url', + '#title' => $this->t('Embedded Service Endpoint URL'), + '#default_value' => $externalServiceSettings['endpoint_url'] ?? '', + '#required' => TRUE, + '#description' => $this->t('Example: https://cu.my.site.com/EmbeddedServiceName1234'), + '#states' => [ + 'visible' => [ + ':input[name="service_name"]' => ['value' => $externalServiceName], + ], + ], + ]; + + $form[$externalServiceName . '__scrt2_url'] = [ + '#type' => 'url', + '#title' => $this->t('SCRT2 URL'), + '#default_value' => $externalServiceSettings['scrt2_url'] ?? '', + '#required' => TRUE, + '#description' => $this->t('Example: https://cu.my.salesforce-scrt.com'), + '#states' => [ + 'visible' => [ + ':input[name="service_name"]' => ['value' => $externalServiceName], + ], + ], + ]; + break; default: } } diff --git a/ucb_site_configuration.install b/ucb_site_configuration.install index 21ae471..b41ed00 100644 --- a/ucb_site_configuration.install +++ b/ucb_site_configuration.install @@ -227,3 +227,14 @@ function ucb_site_configuration_update_9513() { 'site_affiliation_options', ]); } + +/** + * Updates the Third-party services to add the Salesforce chatbot option. + */ + +function ucb_site_configuration_update_9514() { + _ucb_site_configuration_update_config([ + 'external_services', + ]); +} + diff --git a/ucb_site_configuration.libraries.yml b/ucb_site_configuration.libraries.yml index 4f5964b..d05399f 100644 --- a/ucb_site_configuration.libraries.yml +++ b/ucb_site_configuration.libraries.yml @@ -28,3 +28,8 @@ service-goodkind: version: '1.0' js: js/service-goodkind.js: {} + +service-salesforce: + version: '1.0' + js: + js/service-salesforce.js: {}