You are here

function zopim_page_attachments in Zopim Live Chat 8

Implements hook_page_attachments().

File

./zopim.module, line 19

Code

function zopim_page_attachments(array &$page) {

  // Load module settings.
  $config = \Drupal::config('zopim.settings');
  static $already_added = FALSE;

  // Don't add the Zopim Chat widget multiple times.
  if ($already_added) {
    return;
  }

  // Don't add the Zopim Chat widget during installation.
  if (drupal_installation_attempted()) {
    return;
  }

  // Don't add the Zopim Chat widget if it's not configured.
  if (empty($config
    ->get('account_number'))) {
    return;
  }

  // Don't add the Zopim Chat widget on speified paths.
  if (!_zopim_active()) {
    return;
  }

  // Don't add the Zopim Chat widget for speified roles.
  if (!_zopim_role()) {
    return;
  }

  // Insert options as javascript settings.
  $js_settings = [
    'account_number' => $config
      ->get('account_number'),
  ];

  // Add Zopim Chat js settings.
  $page['#attached']['drupalSettings']['zopim'] = $js_settings;

  // Add and initialise the Zopim Chat widget.
  $page['#attached']['library'][] = 'zopim/zopim';
  $already_added = TRUE;
}