You are here

protected function HubspotWebformHandler::remotePost in HubSpot 8

Same name and namespace in other branches
  1. 3.x src/Plugin/WebformHandler/HubspotWebformHandler.php \Drupal\hubspot\Plugin\WebformHandler\HubspotWebformHandler::remotePost()

Execute a remote post.

Parameters

string $operation: The type of webform submission operation to be posted. Can be 'insert', 'update', or 'delete'.

\Drupal\webform\WebformSubmissionInterface $webform_submission: The webform submission to be posted.

1 call to HubspotWebformHandler::remotePost()
HubspotWebformHandler::postSave in src/Plugin/WebformHandler/HubspotWebformHandler.php
Acts on a saved webform submission before the insert or update hook is invoked.

File

src/Plugin/WebformHandler/HubspotWebformHandler.php, line 302

Class

HubspotWebformHandler
Webform submission remote post handler.

Namespace

Drupal\hubspot\Plugin\WebformHandler

Code

protected function remotePost($operation, WebformSubmissionInterface $webform_submission) {

  // Get the hubspot config settings.
  $request_post_data = $this
    ->getPostData($operation, $webform_submission);
  $entity_type = $request_post_data['entity_type'];
  $context = [];

  // Get webform.
  $webform = $this
    ->getWebform();

  // Get all components.
  $elements = $webform
    ->getElementsDecodedAndFlattened();

  // Loop through components and set new value for checkbox fields.
  foreach ($elements as $component_key => $component) {
    if ($component['#type'] == 'checkbox') {
      $webform_submission
        ->setElementData($component_key, $webform_submission
        ->getElementData($component_key) ? 'true' : 'false');
    }
  }
  if ($entity_type) {
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type);
    $entity = $entity_storage
      ->load($request_post_data['entity_id']);
    $form_title = $entity
      ->label();
    $context['pageUrl'] = Url::fromUserInput($request_post_data['uri'], [
      'absolute' => TRUE,
    ])
      ->toString();
  }
  else {

    // Case 2: Webform it self.
    // Webform title.
    $form_title = $this
      ->getWebform()
      ->label();
    $context['pageUrl'] = $this->webform
      ->toUrl('canonical', [
      'absolute' => TRUE,
    ])
      ->toString();
  }
  $settings = $this
    ->getSettings();
  $form_guid = $settings['form_guid'];
  $field_mapping = $settings['field_mapping'];
  $webform_values = $webform_submission
    ->getData();
  $form_values = [];
  foreach ($field_mapping as $webform_path => $hubspot_field) {
    if ($hubspot_field != '--donotmap--') {
      if (strpos($webform_path, ':') !== FALSE) {

        // Is composite element.
        $composite = explode(':', $webform_path);
        $composite_value = NestedArray::getValue($webform_values, $composite);
        $form_values[$hubspot_field] = $composite_value;
      }
      else {

        // Not a composite element.
        $form_values[$hubspot_field] = $webform_values[$webform_path];
      }
    }
  }
  try {
    $hubspot_response = $this->hubspot
      ->submitHubspotForm($form_guid, $form_values, $context);
    $response = $hubspot_response['response'] ?? NULL;

    // Debugging information.
    $config = $this->configFactory
      ->get('hubspot.settings');
    $hubspot_url = 'https://app.hubspot.com';
    $to = $config
      ->get('hubspot_debug_email');
    $default_language = \Drupal::languageManager()
      ->getDefaultLanguage()
      ->getId();
    $from = $config
      ->get('site_mail');
    if ($response) {
      $data = (string) $response
        ->getBody();
      if ($response
        ->getStatusCode() == '200' || $response
        ->getStatusCode() == '204') {
        $this->loggerFactory
          ->get('HubSpot')
          ->notice('Webform "@form" results successfully submitted to HubSpot.', [
          '@form' => $form_title,
        ]);
      }
      else {
        $this->loggerFactory
          ->get('HubSpot')
          ->notice('HTTP notice when submitting HubSpot data from Webform "@form". @code: <pre>@msg</pre>', [
          '@form' => $form_title,
          '@code' => $response
            ->getStatusCode(),
          '@msg' => $response
            ->getBody()
            ->getContents(),
        ]);
      }
      if ($config
        ->get('hubspot_debug_on')) {
        $this->mailManager
          ->mail('hubspot', 'hub_error', $to, $default_language, [
          'errormsg' => $data,
          'hubspot_url' => $hubspot_url,
          'node_title' => $form_title,
        ], $from);
      }
    }
    else {
      $this->loggerFactory
        ->get('HubSpot')
        ->notice('HTTP error when submitting HubSpot data from Webform "@form": <pre>@msg</pre>', [
        '@form' => $form_title,
        '@msg' => $hubspot_response['error'],
      ]);
    }
  } catch (RequestException $e) {
    $this->loggerFactory
      ->get('HubSpot')
      ->notice('HTTP error when submitting HubSpot data from Webform "@form": <pre>@error</pre>', [
      '@form' => $form_title,
      '@error' => $e
        ->getResponse()
        ->getBody()
        ->getContents(),
    ]);
    watchdog_exception('HubSpot', $e);
  } catch (GuzzleException $e) {
    $this->loggerFactory
      ->get('HubSpot')
      ->notice('HTTP error when submitting HubSpot data from Webform "@form": <pre>@error</pre>', [
      '@form' => $form_title,
      '@error' => $e
        ->getMessage(),
    ]);
    watchdog_exception('HubSpot', $e);
  }
}