You are here

function mailchimp_page_bottom in Mailchimp 2.x

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

Implements hook_page_bottom().

File

./mailchimp.module, line 1418
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');
    $current_path = \Drupal::service('path.current')
      ->getPath();
    $current_alias = \Drupal::service('path_alias.manager')
      ->getAliasByPath($current_path);
    if (\Drupal::service('path.matcher')
      ->matchPath($current_path, $connected_site_paths) || \Drupal::service('path.matcher')
      ->matchPath($current_alias, $connected_site_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(),
          ]);
        }
      }
    }
  }
}