You are here

hubspot.module in HubSpot 3.x

Sends Webform results to HubSpot's Forms API.

File

hubspot.module
View source
<?php

/**
 * @file
 * Sends Webform results to HubSpot's Forms API.
 */
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function hubspot_help($route_name) {
  $output = '';
  switch ($route_name) {
    case 'help.page.hubspot':
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The HubSpot module provides leads integration with the HubSpot API, allowing forms created
        with the Webform module to submit their data to HubSpot for leads tracking. It also allows you to easily embed
        the HubSpot JavaScript tracking code in your site to track the visits of each lead. Further information is
        available in the Drupal <a href="http://drupal.org/node/1195370">handbook pages for this module.</a>') . '</p>';
      $output .= '<h3>' . t('Creating a HubSpot Webform') . '</h3>';
      $output .= '<p>' . t("To have a Webform's data sent to HubSpot for leads tracking, complete the following steps:") . '</p>';
      $output .= '<ol>';
      $output .= '<li>' . t('Connect to your HubSpot account from the HubSpot settings page (links below).') . '</li>';
      $output .= '<li>' . t('From the Webform tab of any webform you will now see the HubSpot "subtab" link below. Click
       on that link.') . '</li>';
      $output .= '<li>' . t('On that page you will see a dropdown listing all of your HubSpot forms (set up from
        HubSpot.com). Select the form you want to map to this Webform.') . '</li>';
      $output .= '<li>' . t('Finally, just go through each Webform fields and map them to the HubSpot form fields
        available in each dropdown. Click save and you\'re done!') . '</li>';
      $output .= '</ol>';
      $output .= '<p>' . t('The Webform will now automatically send any submissions directly to HubSpot form you
        configured.') . '</p>';
      $output .= '<h3>' . t('HubSpot Javascript tracking code') . '</h3>';
      $output .= '<p>' . t('HubSpot supports using a JavaScript tracking system to analyze potential leads on your site.
        You can get the JavaScript code from your HubSpot settings, under External Site Traffic Logging, and paste it
        into the HubSpot integration settings page in Drupal to have it automatically included on every page.') . '</p>';
      break;
    case 'hubspot.form_settings':
      $output .= '<p>' . t("To post this form's results to HubSpot you will need to select the HubSpot form and map the\n        webform fields to the available HubSpot fields.") . '</p>';
      break;
  }
  return $output;
}

/**
 * Returns the default value for the given arguments.
 */
function _hubspot_default_value($id, $hubspot_guid = NULL, $webform_field = NULL) {
  $query = \Drupal::database()
    ->select('hubspot', 'h');
  $query
    ->addField('h', 'hubspot_guid');
  $query
    ->condition('h.id', $id);
  if ($hubspot_guid && $webform_field) {
    $query
      ->condition('h.hubspot_guid', $hubspot_guid);
    $query
      ->condition('h.webform_field', $webform_field);
  }
  $query
    ->range(0, 1);
  return $query
    ->execute()
    ->fetchField();
}

/**
 * Gets the list of forms from HubSpot via the API.
 *
 * Upon failure, it will attempt to reauthenticate.
 */
function _hubspot_get_forms() {
  return \Drupal::service('hubspot.hubspot')
    ->getHubspotForms();
}

/**
 * Implements hook_mail().
 */
function hubspot_mail($key, &$message, $params) {
  $message['subject'] = t('HubSpot leads insertion error');
  switch ($key) {
    case 'http_error':
      $message['body'][] = t('When attempting to submit the form "@form" to HubSpot, a HTTP error occurred.', [
        '@form' => $params['node_title'],
      ]);
      break;
    case 'hub_error':
      $message['body'][] = t('Upon submitting the Webform "@form" to HubSpot, an error was returned.', [
        '@form' => $params['node_title'],
      ]);
      break;
  }
  $message['body'][] = t('Error message: @message', [
    '@message' => $params['errormsg'],
  ]);
  $message['body'][] = t('HubSpot POST URL: @url', [
    '@url' => $params['hubspot_url'],
  ]);
  $message['body'][] = t('To adjust the debugging settings, visit @url', [
    '@url' => Url::fromRoute('hubspot.admin_settings')
      ->toString(),
  ]);
}

/**
 * Implements hook_page_attachments().
 */
function hubspot_page_attachments(&$page) {

  // If tracking code is enabled then Attach the library.
  if (\Drupal::config('hubspot.settings')
    ->get('tracking_code_on')) {
    $page['#attached']['library'][] = 'hubspot/hubspot.code_tracking';
  }
}

/**
 * Implements hook_library_info_build().
 *
 * Creates library info with dynamic script file name.
 */
function hubspot_library_info_build() {
  $portal_id = \Drupal::config('hubspot.settings')
    ->get('hubspot_portal_id');
  $libraries = [];

  // Add a library whose information changes depending on certain conditions.
  $libraries['hubspot.code_tracking'] = [
    'dependencies' => [
      'core/jquery',
      'core/drupalSettings',
    ],
  ];
  $libraries['hubspot.code_tracking'] += [
    'js' => [
      'https://js.hs-scripts.com/' . $portal_id . '.js' => [
        'type' => 'external',
        'attributes' => [
          'id' => "hs-script-loader",
          'defer' => TRUE,
          'async' => TRUE,
        ],
      ],
    ],
  ];
  return $libraries;
}

Functions

Namesort descending Description
hubspot_help Implements hook_help().
hubspot_library_info_build Implements hook_library_info_build().
hubspot_mail Implements hook_mail().
hubspot_page_attachments Implements hook_page_attachments().
_hubspot_default_value Returns the default value for the given arguments.
_hubspot_get_forms Gets the list of forms from HubSpot via the API.