View source
<?php
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
function crazyegg_permission() {
return [
'administer crazy egg' => [
'title' => t('Administer Crazy Egg'),
'description' => t('Administer account settings and visibility of Crazy Egg on your site.'),
],
];
}
function crazyegg_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.crazyegg':
return t('<img src="@logo" style="float: right;" alt="Crazy Egg">' . '<strong>Crazy Egg</strong> is an analytics tool that provides website heatmaps and eye tracking.<br/><br/>' . 'Drupal module repository: <a href="@module_url">www.drupal.org/project/crazyegg</a><br/>' . 'Official website: <a href="@ce_url">crazyegg.com</a><br/>' . 'Support: <a href="mailto:@support_email">support@crazyegg.com</a><br/><br/><hr/><br/>', [
'@ce_url' => 'https://www.crazyegg.com',
'@module_url' => 'https://www.drupal.org/project/crazyegg',
'@support_email' => 'support@crazyegg.com',
'@logo' => 'https://ceblog.s3.amazonaws.com/wp-content/uploads/2015/06/Crazy-Egg-logo-small.png',
]);
}
}
function crazyegg_library_info_build() {
$libs = [
'crazyegg' => [],
];
$account_path = crazyegg_get_account_path();
if ($account_path) {
$scope = \Drupal::config('crazyegg.settings')
->get('crazyegg_js_scope');
$libs['crazyegg'] = [
'header' => $scope === 'header',
'js' => [
'https://script.crazyegg.com/pages/scripts/' . $account_path . '.js' => [
'type' => 'external',
'minified' => TRUE,
'weight' => 0,
'attributes' => [
'async' => TRUE,
],
],
],
];
}
return $libs;
}
function crazyegg_page_attachments(array &$attachments) {
$crazyegg_enabled = \Drupal::config('crazyegg.settings')
->get('crazyegg_enabled');
$account_path = crazyegg_get_account_path();
if ($crazyegg_enabled > 0 && $account_path && crazyegg_is_page_allowed() && crazyegg_is_role_allowed()) {
$attachments['#attached']['library'][] = 'crazyegg/crazyegg';
}
}
function crazyegg_get_account_path() {
$account_id = \Drupal::config('crazyegg.settings')
->get('crazyegg_account_id');
if (is_numeric($account_id)) {
$account_path = str_pad($account_id, 8, '0', STR_PAD_LEFT);
$account_path = substr($account_path, 0, 4) . '/' . substr($account_path, 4, 8);
return $account_path;
}
return FALSE;
}
function crazyegg_is_page_allowed() {
$paths = \Drupal::config('crazyegg.settings')
->get('crazyegg_paths');
$url = Url::fromRoute('<current>')
->toString();
return empty($paths) or \Drupal::service('path.matcher')
->matchPath($url, $paths);
}
function crazyegg_is_role_allowed() {
$current_user = User::load(\Drupal::currentUser()
->id());
$roles_excluded = \Drupal::config('crazyegg.settings')
->get('crazyegg_roles_excluded');
if ($roles_excluded) {
foreach ($roles_excluded as $role) {
if (!empty($role) and $current_user
->hasRole($role)) {
return FALSE;
}
}
}
return TRUE;
}