diff --git a/src/Plugin/WebformHandler/WebformPipedrive.php b/src/Plugin/WebformHandler/WebformPipedrive.php index b298772120311ae3a3577c72e823914cfab4f0e7..89163fd3cd3a9e7211987a9af7cdda2c233c87b0 100644 --- a/src/Plugin/WebformHandler/WebformPipedrive.php +++ b/src/Plugin/WebformHandler/WebformPipedrive.php @@ -2,9 +2,12 @@ namespace Drupal\webform_pipedrive\Plugin\WebformHandler; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Language\LanguageManagerInterface; use Drupal\webform\Plugin\WebformHandlerBase; use Drupal\webform\WebformSubmissionInterface; +use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -15,13 +18,10 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * id = "webform_pipedrive", * label = @Translation("WebformPipedrive"), * category = @Translation("WebformPipedrive"), - * description = @Translation("Send the values to the api on - * form submission."), cardinality = - * \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED, - * results = - * \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED, - * submission = - * \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_OPTIONAL, + * description = @Translation("Send the values to the api on form submission."), + * cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED, + * results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED, + * submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_OPTIONAL, * tokens = TRUE, * ) */ @@ -32,29 +32,37 @@ class WebformPipedrive extends WebformHandlerBase { * * @var \Drupal\Core\Entity\EntityFieldManagerInterface */ - protected $entityFieldManager; + protected EntityFieldManagerInterface $entityFieldManager; /** * The language Manager. * * @var \Drupal\Core\Language\LanguageManagerInterface */ - protected $languageManager; + protected LanguageManagerInterface $languageManager; + + /** + * The HTTP client. + * + * @var \GuzzleHttp\ClientInterface + */ + protected ClientInterface $httpClient; /** * {@inheritdoc} */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self { $plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition); $plugin->entityFieldManager = $container->get('entity_field.manager'); $plugin->languageManager = $container->get('language_manager'); + $plugin->httpClient = $container->get('http_client'); return $plugin; } /** * {@inheritdoc} */ - public function defaultConfiguration() { + public function defaultConfiguration(): array { return [ 'enable_mapping' => FALSE, 'enable_orgdeals' => FALSE, @@ -67,8 +75,7 @@ class WebformPipedrive extends WebformHandlerBase { /** * {@inheritdoc} */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - + public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { $mapping_options = $this->getMappingOptions(); // Checkbox to enable or disable mapping. @@ -173,12 +180,10 @@ class WebformPipedrive extends WebformHandlerBase { /** * {@inheritdoc} - * - * Validate the configuration form. */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - + public function validateConfigurationForm(array &$form, FormStateInterface $form_state): void { parent::validateConfigurationForm($form, $form_state); + // Validate Person field mapping. if (empty($form_state->getValue('user_field_mapping'))) { $form_state->setErrorByName('user_field_mapping', $this->t('At least one Person mapping field must be selected.')); @@ -198,47 +203,46 @@ class WebformPipedrive extends WebformHandlerBase { /** * {@inheritdoc} - * - * Submit the configuration form. */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { parent::submitConfigurationForm($form, $form_state); - // You can access form values like this: - $tokenized_text = $form_state->getValue(['settings', 'tokenized_text']); - - $user_field_mapping = $form_state->getValue(['settings', 'user_field_mapping']); - - // Optionally, save the custom configuration values to the handler. - $this->configuration['tokenized_text'] = $tokenized_text; - - $this->configuration['user_field_mapping'] = $user_field_mapping; + + // You can access form values like this: + $tokenized_text = $form_state->getValue(['settings', 'tokenized_text']); + $user_field_mapping = $form_state->getValue(['settings', 'user_field_mapping']); + + // Optionally, save the custom configuration values to the handler. + $this->configuration['tokenized_text'] = $tokenized_text; + $this->configuration['user_field_mapping'] = $user_field_mapping; + $this->applyFormStateToConfiguration($form_state); } /** * {@inheritdoc} - * - * Send data to Pipedrive after form submission. */ - public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) { + public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE): void { // Fetch submission data and configuration settings. $webform_data = $webform_submission->getData(); $user_field_mapping = $this->configuration['user_field_mapping']; $enableMapping = $this->configuration['enable_mapping']; $enable_deals = $this->configuration['enable_deals']; $enable_orgdeals = $this->configuration['enable_orgdeals']; + // To get configuration for the pipedrive from service. $configsettings = \Drupal::service('webform_pipedrive.get_access_token')->accessToken(); $endPoint = $configsettings['endpoint']; $accessToken = $configsettings['accessToken']; $orgId = 0; $user_field_mapping_org = []; + // Initialize the formatted output. $formatted_output = ''; // Format the submission values as key: value pairs with new lines. foreach ($webform_data as $key => $value) { $formatted_output .= ucfirst(str_replace('_', ' ', $key)) . ': ' . $value . "\n"; } + // Replace the token in the tokenized text. $token_service = \Drupal::token(); $replaced_text = $token_service->replace($this->configuration['tokenized_text']['value'], ['webform_submission' => $webform_submission]); @@ -249,13 +253,14 @@ class WebformPipedrive extends WebformHandlerBase { // Process organization data. $orgData = []; - if ($enableMapping != FALSE) { + if ($enableMapping) { $user_field_mapping_org = $this->configuration['user_field_mapping_org']; foreach ($user_field_mapping_org as $webform_key => $user_field) { $orgData[$user_field] = $webform_data[$webform_key]; } $orgId = \Drupal::service('webform_pipedrive.pipe_drive_integration')->sentWebformSubmissionToPipeDriveForOrganisation($orgData, $accessToken, $endPoint); } + // Process person data. $personData = []; $personData['org_id'] = $orgId; @@ -272,7 +277,7 @@ class WebformPipedrive extends WebformHandlerBase { * @return array * Array of source and destination options. */ - protected function getMappingOptions() { + protected function getMappingOptions(): array { // Get all Webform elements. $webform = $this->getWebform(); $elements = $webform->getElementsInitializedFlattenedAndHasValue(); @@ -280,9 +285,11 @@ class WebformPipedrive extends WebformHandlerBase { foreach ($elements as $key => $element) { $source_options[$key] = $element['#admin_title'] ?: $element['#title'] ?: $key; } + // Fetch dynamic Pipedrive fields from the API. $destination_options = $this->fetchPipedriveFields(); - $orgdestination_options = $this->fetchorgPipedriveFields(); + $orgdestination_options = $this->fetchOrgPipedriveFields(); + return [ 'source' => $source_options, 'destination' => $destination_options, @@ -292,10 +299,11 @@ class WebformPipedrive extends WebformHandlerBase { /** * Fetch the available Pipedrive fields from the API dynamically. + * + * @return array + * Array of Pipedrive fields. */ - protected function fetchPipedriveFields() { - - $client = \Drupal::service('http_client'); + protected function fetchPipedriveFields(): array { // To get configuration for the Pipedrive from the service. $configsettings = \Drupal::service('webform_pipedrive.get_access_token')->accessToken(); $endPoint = $configsettings['endpoint']; @@ -304,7 +312,7 @@ class WebformPipedrive extends WebformHandlerBase { $url = $endPoint . '/v1/personFields?api_token=' . $accessToken; try { - $response = $client->get($url); + $response = $this->httpClient->get($url); $response_body = $response->getBody()->getContents(); $data = json_decode($response_body, TRUE); @@ -326,18 +334,23 @@ class WebformPipedrive extends WebformHandlerBase { /** * Fetch the available Pipedrive organization fields from the API dynamically. + * + * @return array + * Array of Pipedrive organization fields. */ - protected function fetchOrgPipedriveFields() { - $client = \Drupal::service('http_client'); + protected function fetchOrgPipedriveFields(): array { // To get configuration for the Pipedrive from the service. $configsettings = \Drupal::service('webform_pipedrive.get_access_token')->accessToken(); $endPoint = $configsettings['endpoint']; $accessToken = $configsettings['accessToken']; + $url = $endPoint . '/v1/organizationFields?api_token=' . $accessToken; + try { - $response = $client->get($url); + $response = $this->httpClient->get($url); $response_body = $response->getBody()->getContents(); $data = json_decode($response_body, TRUE); + if (isset($data['data']) && is_array($data['data'])) { $destination_options = []; foreach ($data['data'] as $field) { @@ -349,6 +362,7 @@ class WebformPipedrive extends WebformHandlerBase { catch (RequestException $e) { \Drupal::logger('webform_pipedrive')->error('Error fetching Pipedrive fields: ' . $e->getMessage()); } + // Return empty array if there is an error fetching fields. return []; } diff --git a/webform_pipedrive.info.yml b/webform_pipedrive.info.yml index e3843ce4eead8251d0c5f473140cbea87c1db9bd..e8e59f704c5e6bfb3642ae7ceafb755d20e6bfa5 100644 --- a/webform_pipedrive.info.yml +++ b/webform_pipedrive.info.yml @@ -2,7 +2,7 @@ name: Webform Integration Pipedrive type: module description: Use to connect webform with pipedrive API and configuration for API package: Webform -core_version_requirement: ^9 || ^10 +core_version_requirement: ^9 || ^10 || ^11 dependencies: - webform:webform configure: webform_pipedrive.config