You are here

function mailchimp_page_build in Mailchimp 7.4

Same name and namespace in other branches
  1. 7.5 mailchimp.module \mailchimp_page_build()

Implements hook_page_build().

File

./mailchimp.module, line 1898
Mailchimp module.

Code

function mailchimp_page_build(&$page) {

  // Insert JavaScript for Mailchimp Connected Sites, if enabled.
  if (variable_get('mailchimp_enable_connected', FALSE) && mailchimp_get_api_object() && class_exists('\\Mailchimp\\MailchimpConnectedSites')) {

    // Limit JavaScript embed to pre-configured paths.
    $connected_site_paths = variable_get('mailchimp_connected_paths', FALSE);
    $valid_paths = explode("\r\n", $connected_site_paths);
    $path = current_path();
    if (drupal_is_front_page() && in_array('<front>', $valid_paths) || in_array($path, $valid_paths)) {
      $connected_site_id = variable_get('mailchimp_connected_id', FALSE);
      if (!empty($connected_site_id)) {
        try {

          /* @var \Mailchimp\MailchimpConnectedSites $mc_connected */
          $mc_connected = mailchimp_get_api_object('MailchimpConnectedSites');

          // Verify Connected Site exists on the Mailchimp side and insert JS.
          try {
            $connected_site = $mc_connected
              ->getConnectedSite($connected_site_id);
            if (!empty($connected_site)) {
              $mcjs = array(
                '#type' => 'markup',
                '#markup' => $connected_site->site_script->fragment,
              );
              drupal_add_html_head($mcjs, 'mcjs');
            }
          } catch (Exception $e) {

            // Throw exception only for errors other than member not found.
            if ($e
              ->getCode() != 404) {
              throw new Exception($e
                ->getMessage(), $e
                ->getCode(), $e);
            }
          }
        } catch (Exception $e) {
          watchdog('mailchimp', 'An error occurred requesting connected site information. "%message"', array(
            '%message' => $e
              ->getMessage(),
          ), WATCHDOG_ERROR);
        }
      }
    }
  }
}