Custom theme for Pléiade dashboard project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
pleiade-custom-theme/pleiadebv.theme

330 lines
12 KiB

<?php
/**
* @file
* Pléiade BV theme file.
*/
/**
* Implements template_preprocess_html().
*/
use Drupal\module_api_pleiade\ApiPleiadeManager;
function pleiadebv_preprocess(&$variables, $hook)
{
// Récupérer le path du logo
// $variables['logo_path'] = '/' . \Drupal::config('b5coll1.settings')->get('logo.path');
$config = \Drupal::config('module_general_pleiade.settings');
$url_lien_documentation = $config->get('url_lien_documentation');
$colorTheme = $config->get('color_theme');
$numero_telephone_support = $config->get('numero_telephone_support');
$adresse_mail_support = $config->get('adresse_mail_support');
$horaire_support = $config->get('horaire_support');
$imageFooter = '/themes/custom/pleiadebv/assets/images/logo_footer.png';
$imageLeftSidebar = '/themes/custom/pleiadebv/assets/images/Logo_pleiade_fond_blanc.png';
$imageLeftSidebarReduced = '/themes/custom/pleiadebv/assets/images/logo_reduced_black.png';
if (!empty($url_lien_documentation)) {
$variables['url_lien_documentation'] = $url_lien_documentation;
}
if (!empty($horaire_support)) {
$variables['horaire_support'] = $horaire_support;
}
if (!empty($numero_telephone_support)) {
$variables['numero_telephone_support'] = $numero_telephone_support;
}
if (!empty($adresse_mail_support)) {
$variables['adresse_mail_support'] = $adresse_mail_support;
} else {
$variables['url_lien_documentation'] = "*";
}
$variables['imageFooterUrl'] = $imageFooter;
$variables['colorTheme'] = $colorTheme;
$variables['imageLeftSidebarUrl'] = $imageLeftSidebar;
$variables['imageLeftSidebarReducedUrl'] = $imageLeftSidebarReduced;
// Récupérer le path de base
$variables['base_path'] = \Drupal::request()->getHost();
// Variable is_front pour frontpage
try {
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
} catch (Exception $e) {
$variables['is_front'] = FALSE;
}
// Ensure the cache varies correctly (new in Drupal 8.3).
$variables['#cache']['contexts'][] = 'url.path.is_front';
// User : pic de profil et autres
}
function pleiadebv_preprocess_html(&$variables)
{
$roles = \Drupal::currentUser()->getRoles();
foreach ($roles as $role) {
$variables['attributes']['class'][] = "role-{$role}";
}
$variables['userId'] = \Drupal::currentUser()->id();
}
function pleiadebv_preprocess_page(&$variables)
{
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
if ($route_match->getParameter('node')) {
$page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
$variables['pleiade_node_title'] = $page_title;
}
$variables['userId'] = $variables['user']->id();
$user_logged_in_name = $variables['user']->getDisplayName();
$variables['user_logged_in_name'] = ucfirst($user_logged_in_name);
$user_email = $variables['user']->getEmail();
$variables['user_email'] = $user_email;
if (isset($user->get('user_picture')->entity)) {
$variables['user_picture'] = $user->get('user_picture')->entity->createFileUrl();
} else {
// default image
$field = \Drupal\field\Entity\FieldConfig::loadByName('user', 'user', 'user_picture');
$default_image = $field->getSetting('default_image');
if ($default_image) {
$file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $default_image['uuid']);
if ($file != '' || $file != null) {
try {
$picture = \Drupal::service('file_url_generator')
->generateAbsoluteString($file->getFileUri());
} catch (InvalidStreamWrapperException $e) {
return FALSE;
}
$variables['user_picture'] = $picture;
} else {
$variables['user_picture'] = '/themes/custom/pleiadebv/assets/images/users/img_user.png';
}
} else {
$variables['user_picture'] = '/themes/custom/pleiadebv/assets/images/users/img_user.png';
}
}
$configLemon = \Drupal::config('api_lemon_pleiade.settings');
$configGeneral = \Drupal::config('module_general_pleiade.settings');
if ($configGeneral) {
$menu_ecoll = $configGeneral->get('menu_ecollectivites');
$variables['menu_ecoll'] = $menu_ecoll;
}
if ($configLemon) {
$lemonBlockId = $configLemon->get('field_lemon_block_id');
$variables['lemonBlockId'] = $lemonBlockId;
$field_lemon_url = $configLemon->get('field_lemon_url');
$variables['field_lemon_url'] = $field_lemon_url;
}
$configMoodle = \Drupal::config('api_moodle_pleiade.settings');
if ($configMoodle) {
$moodleBlockId = $configMoodle->get('field_moodle_block_id');
$variables['moodleBlockId'] = $moodleBlockId;
}
$settings_glpi = \Drupal::config('api_glpi_pleiade.settings');
// // API endpoint URL
$tempstore = \Drupal::service('tempstore.private')->get('api_lemon_pleiade');
$groupData = $tempstore->get('groups');
if ($groupData !== NULL) {
$groupDataArray = explode(', ', $groupData);
}
if (is_array($groupDataArray) && in_array($settings_glpi->get('glpi_group'), $groupDataArray)) {
$variables['inGlpiGroup'] = true ;
}
else
{
$variables['inGlpiGroup'] = false ;
}
$entityTypeManager = \Drupal::entityTypeManager();
$query = $entityTypeManager->getStorage('node')->getQuery();
$query->condition('type', 'message_informatif');
$query->condition('status', 1); // Published content condition
$query->accessCheck(FALSE);
$entityIds = $query->execute();
$messages = $entityTypeManager->getStorage('node')->loadMultiple($entityIds);
if ($messages) {
$variables['message_enabled'] = true;
}
/* $query1 = $entityTypeManager->getStorage('node')->getQuery();
$query1->accessCheck(TRUE);
$query1->condition('type', 'article');
$query1->condition('status', 1); // Published content condition
$entityIdsActu = $query1->execute();
$messagesActu = $entityTypeManager->getStorage('node')->loadMultiple($entityIdsActu);
*/
$getArticles = new ApiPleiadeManager();
$articles = $getArticles->getEcollArticles();
if ($articles) {
$variables['actu_enabled'] = true;
}
$variables['glpi_enabled'] = \Drupal::moduleHandler()->moduleExists('api_glpi_pleiade');
$variables['post_it_enabled'] = \Drupal::moduleHandler()->moduleExists('module_postit_pleiade');
$variables['lemon_enabled'] = \Drupal::moduleHandler()->moduleExists('api_lemon_pleiade');
$variables['pastell_enabled'] = \Drupal::moduleHandler()->moduleExists('api_pastell_pleiade');
$variables['zimbra_enabled'] = \Drupal::moduleHandler()->moduleExists('api_zimbra_pleiade');
$variables['nextcloud_enabled'] = \Drupal::moduleHandler()->moduleExists('api_nextcloud_pleiade');
$variables['moodle_enabled'] = \Drupal::moduleHandler()->moduleExists('api_moodle_pleiade');
$variables['guide_utilisateur_enabled'] = \Drupal::moduleHandler()->moduleExists('guide_utilisateur_pleiade');
$variables['humhub_enabled'] = \Drupal::moduleHandler()->moduleExists('api_humhub_pleiade');
}
function pleiadebv_preprocess_user(&$variables)
{
// get user Id
$variables['userId'] = $variables['user']->id();
// gets current user name
$user_logged_in_name = $variables['user']->getDisplayName();
// creates value to ouput in the DOM & capitalize first letter
$variables['user_logged_in_name'] = $user_logged_in_name;
// gets user email
$user_email = $variables['user']->getEmail();
$variables['user_email'] = $user_email;
// Check if 'mail' exists and set default value if not
$variables['mail'] = isset($variables['user']->get('mail')->value) ? $variables['user']->get('mail')->value : "Non renseigné par l'utilisateur";
// Check if 'field_nom' exists and set default value if not
$variables['user_name'] = isset($variables['user']->get('field_nom')->value) ? $variables['user']->get('field_nom')->value : "Non renseigné par l'utilisateur";
// Check if 'field_prenom' exists and set default value if not
$variables['user_firstname'] = isset($variables['user']->get('field_prenom')->value) ? $variables['user']->get('field_prenom')->value : "Non renseigné par l'utilisateur";
if (isset($variables['user']->get('user_picture')->entity)) {
$variables['user_picture'] = $variables['user']->get('user_picture')->entity->createFileUrl();
} else {
// default image
$field = \Drupal\field\Entity\FieldConfig::loadByName('user', 'user', 'user_picture');
$default_image = $field->getSetting('default_image');
if ($default_image) {
$file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $default_image['uuid']);
if ($file) {
try {
$picture = \Drupal::service('file_url_generator')
->generateAbsoluteString($file->getFileUri());
} catch (InvalidStreamWrapperException $e) {
return FALSE;
}
$variables['user_picture'] = $picture;
} else {
$variables['user_picture'] = '/themes/custom/pleiadebv/assets/images/users/img_user.png';
}
} else {
$variables['user_picture'] = '/themes/custom/pleiadebv/assets/images/users/img_user.png';
}
}
}
function pleiadebv_preprocess_node(&$variables) {
// Vérifie si le type de contenu est un article.
if ($variables['node']->getType() === 'article') {
$query = \Drupal::entityQuery('node')
->condition('status', 1) // Articles publiés uniquement
->condition('type', 'article') // Type de contenu "article"
->accessCheck(TRUE)
->sort('created', 'DESC');
$result = $query->execute();
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($result);
$articles = [];
foreach ($nodes as $node) {
$title = $node->getTitle();
$image_url = '';
if ($node->hasField('field_image') && $node->get('field_image')->entity) {
$file = $node->get('field_image')->entity;
$image_url = \Drupal::service('file_url_generator')
->generateAbsoluteString($file->getFileUri());
}
else {
// Si aucun fichier n'est associé au champ field_image, récupérer l'image par défaut
$default_image = $node->get('field_image')->getFieldDefinition()->getSetting('default_image');
$file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $default_image['uuid']);
if ($file) {
try {
$image_url = \Drupal::service('file_url_generator')->generateAbsoluteString($file->getFileUri());
} catch (InvalidStreamWrapperException $e) {
// Handle exception if needed.
}
}
}
$created_date = \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'custom', 'd/m/Y H:i');
$tag_id = '';
if ($node->hasField('field_tags') && !$node->get('field_tags')->isEmpty()) {
$tags = $node->get('field_tags')->referencedEntities();
if($tags){
$first_tag = reset($tags);
$tag_id = $first_tag->id();
}
}
// Obtenir l'URL de l'article.
$article_url = $node->toUrl()->toString();
// Ajoutez les informations de l'article au tableau des articles.
$articles[] = [
'id' => $node->id(),
'title' => $title,
'image_url' => $image_url,
'created_date' => $created_date,
'tag_id' => $tag_id,
'article_url' => $article_url,
];
}
// Ajoutez le tableau d'articles à la variable de modèle.
$variables['all_articles'] = $articles;
}
}
function pleiadebv_theme_suggestions_page_alter(array &$suggestions, array $variables) {
$route_name = \Drupal::routeMatch()->getRouteName();
switch ($route_name) {
case 'system.401':
// Unauthorized Access.
$error = 401;
break;
case 'system.403':
// Access Denied.
$error = 403;
break;
case 'system.404':
// Page Not Found.
$error = 404;
break;
}
if (isset($error)) {
$suggestions[] = 'page__' . $error;
}
}