turbodrop-1.0.x-dev/turbodrop.module
turbodrop.module
<?php
/**
* @file
* Handle hooks for the Turbodrop module.
*/
/**
* Implements hook_page_attachments().
*/
function turbodrop_page_attachments(array &$page) {
$turbodrop_settings = \Drupal::config('turbodrop.settings');
$current_user = \Drupal::currentUser();
// Check if Turbodrop is enabled and user has permission to it.
if ($turbodrop_settings->get('enabled') && $current_user->hasPermission('access turbodrop')) {
// Check if administrator should access the Turbodrop functionality.
if (in_array('administrator', $current_user->getRoles())) {
if (!$turbodrop_settings->get('access_role_administrator')) {
return;
}
}
// Get Turbodrop integration settings.
$turbodrop_area = $turbodrop_settings->get('integration_area');
// Get the default theme.
$default_theme_name = \Drupal::config('system.theme')->get('default');
// Get the current theme.
$active_theme_name = \Drupal::service('theme.manager')->getActiveTheme()->getName();
// Get the backend theme.
$admin_theme_name = \Drupal::config('system.theme')->get('admin');
// Get admin route.
$is_admin_route = \Drupal::service('router.admin_context')->isAdminRoute();
// Check area settings.
if (($turbodrop_area['theme_frontend'] === 'theme_frontend' && $active_theme_name === $default_theme_name) ||
($turbodrop_area['theme_backend'] === 'theme_backend' && $active_theme_name === $admin_theme_name) ||
($turbodrop_area['route_admin'] === 'route_admin' && $is_admin_route)) {
// Set correct library.
$integration_type = $turbodrop_settings->get('integration_type');
$library = match ($integration_type) {
'custom' => $turbodrop_settings->get('integration_library'),
default => 'turbodrop/turbodrop-local',
};
$page['#attached']['library'][] = $library;
}
}
$page['#attached']['drupalSettings']['turbodropSettings'] = $turbodrop_settings->getRawData();
$page['#attached']['library'][] = 'turbodrop/turbodrop-configuration';
}
