You are here

function mailchimp_page_bottom in Mailchimp 8

Same name and namespace in other branches
  1. 2.x mailchimp.module \mailchimp_page_bottom()

Implements hook_page_bottom().

File

./mailchimp.module, line 1467
Mailchimp module.

Code

function mailchimp_page_bottom(array &$page_bottom) {
  $config = \Drupal::config('mailchimp.settings');

  // Insert JavaScript for Mailchimp Connected Sites, if enabled.
  if (!empty($config
    ->get('enable_connected'))) {

    // Limit JavaScript embed to pre-configured paths.
    $connected_site_paths = $config
      ->get('connected_paths');
    $valid_paths = explode("\r\n", $connected_site_paths);
    $path = \Drupal::service('path.current')
      ->getPath();
    $path = str_replace('/', '', $path);
    if (\Drupal::service('path.matcher')
      ->isFrontPage() && in_array('<front>', $valid_paths) || in_array($path, $valid_paths)) {
      $connected_site_id = $config
        ->get('connected_id');
      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.
          $connected_site = $mc_connected
            ->getConnectedSite($connected_site_id);
          if (!empty($connected_site)) {
            $mcjs = [
              '#type' => 'markup',
              '#markup' => Markup::create($connected_site->site_script->fragment),
            ];
            $page_bottom['mailchimp_connected'] = $mcjs;
          }
        } catch (\Exception $e) {
          \Drupal::logger('mailchimp')
            ->error('An error occurred while getting connected sites. "{message}"', [
            'message' => $e
              ->getMessage(),
          ]);
        }
      }
    }
  }
}