You are here

olark.module in Olark Chat 8

Same filename and directory in other branches
  1. 6 olark.module
  2. 7 olark.module

Integrates Olark Chat in a Drupal site.

File

olark.module
View source
<?php

/**
 * @file
 * Integrates Olark Chat in a Drupal site.
 */
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;

/**
 * Implements hook_page_bottom().
 */
function olark_page_bottom(array &$page_bottom) {
  $settings = \Drupal::config('olark.settings');

  // Note that the 'olark_enable' variable can be (un)set in the $conf array in
  // settings.php, or by another module to enable/disable however, there is not
  // a user interface for this variable.
  if (!$settings
    ->get('olark_enable')) {
    return;
  }

  // Exit if olark is not allowed on admin pages and we are on an admin page.
  if (!$settings
    ->get('olark_enable_admin') && \Drupal::service('router.admin_context')
    ->isAdminRoute()) {
    return;
  }
  $user = \Drupal::currentUser();
  $js_settings = [
    'olark' => [
      'disable_ios' => $settings
        ->get('olark_ios'),
      'enabled' => $settings
        ->get('olark_enable'),
    ],
  ];

  // If the user is logged in, let's get some pertinent information and pass it
  // along as well.
  if ($user
    ->id()) {
    $user_page_url = Url::fromRoute('entity.user.canonical', [
      'user' => $user
        ->id(),
    ], [
      'absolute' => TRUE,
    ])
      ->toString();
    $js_settings['olark']['uid'] = $user
      ->id();
    $js_settings['olark']['name'] = $user
      ->getAccountName();
    $js_settings['olark']['mail'] = $user
      ->getEmail();
    $js_settings['olark']['roles'] = t('roles @roles', [
      '@roles' => implode(', ', $user
        ->getRoles(TRUE)),
    ]);
    $js_settings['olark']['userpage'] = $user_page_url;
    $js_settings['olark']['loggedinas'] = t('logged in as <a href=":url">@name</a>', [
      '@name' => $user
        ->getAccountName(),
      ':url' => $user_page_url,
    ]);
  }
  $page_bottom['olark'] = [
    '#markup' => Markup::create($settings
      ->get('olark_code')),
    '#attached' => [
      'library' => [
        'olark/integration',
      ],
      'drupalSettings' => $js_settings,
    ],
    '#cache' => [
      'contexts' => [
        'user',
      ],
      'tags' => [
        'user:' . $user
          ->id(),
      ],
    ],
  ];

  // Add cachability metadata.

  /** @var Drupal\Core\Render\Renderer $renderer */
  $renderer = \Drupal::service('renderer');
  $renderer
    ->addCacheableDependency($page_bottom['olark'], $settings);
}

Functions

Namesort descending Description
olark_page_bottom Implements hook_page_bottom().