You are here

function mautic_page_attachments in Mautic Integration 8

Implements hook_page_attachments().

Insert JavaScript to the page.

File

./mautic.module, line 38
Drupal Module: Mautic.

Code

function mautic_page_attachments(array &$page) {
  $config = \Drupal::config('mautic.settings');
  $request = \Drupal::request();

  // Add Mautic tracking code if needed
  $status = NULL;
  if ($exception = $request->attributes
    ->get('exception')) {
    $status = $exception
      ->getStatusCode();
  }
  $trackable_status_codes = [
    '403',
    '404',
  ];
  if ($config
    ->get('mautic_enable') && _mautic_visibility_pages() && !in_array($status, $trackable_status_codes)) {

    // Integrate mautic.
    $mautic_settings = [
      'base_url' => $config
        ->get('mautic_base_url'),
    ];
    $page['#attached']['drupalSettings']['mautic'] = $mautic_settings;
    if ($config
      ->get('header')) {
      $page['#attached']['library'][] = 'mautic/mautic-header';
    }
    else {
      $page['#attached']['library'][] = 'mautic/mautic-footer';
    }

    // Add custom field and optional Lift integration JS code\
    if ($config
      ->get('lift_enable')) {
      $moduleHandler = \Drupal::service('module_handler');
      if ($moduleHandler
        ->moduleExists('acquia_lift')) {
        $page['#attached']['library'][] = 'mautic/customFields';
      }
    }
  }
}