parent
972e438613
commit
c79868282c
@ -1,12 +0,0 @@ |
|||||||
name: 'Module Alerte Pleiade' |
|
||||||
type: module |
|
||||||
description: 'Message entête pour Pléiade' |
|
||||||
package: Pleiade |
|
||||||
# version: 8.x + |
|
||||||
core: 8.x |
|
||||||
core_version_requirement: ^9.5 || ^10 || ^10 |
|
||||||
|
|
||||||
# Information |
|
||||||
version: '1.0.x-dev' |
|
||||||
project: 'alerte_module_pleiade' |
|
||||||
datestamp: 1624364473 |
|
@ -1,117 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
function alerte_module_pleiade_install() |
|
||||||
{ |
|
||||||
$existing_type = \Drupal\node\Entity\NodeType::load('message_informatif'); |
|
||||||
|
|
||||||
if (!$existing_type) { |
|
||||||
// Le type de contenu n'existe pas, le créer. |
|
||||||
$news_type = \Drupal\node\Entity\NodeType::create([ |
|
||||||
'type' => 'message_informatif', |
|
||||||
'name' => 'Message informatif', |
|
||||||
'description' => 'Type de contenu pour les messages informatif à afficher en haut du bureau.', |
|
||||||
'display_submitted' => TRUE, |
|
||||||
'preview_mode' => 'default', |
|
||||||
'help' => '', |
|
||||||
]); |
|
||||||
$news_type->save(); |
|
||||||
|
|
||||||
// Ajouter un champ 'description'. |
|
||||||
$field_description_storage = \Drupal\field\Entity\FieldStorageConfig::create([ |
|
||||||
'field_name' => 'field_message_a_afficher', |
|
||||||
'entity_type' => 'node', |
|
||||||
'type' => 'text_long', |
|
||||||
]); |
|
||||||
$field_description_storage->save(); |
|
||||||
|
|
||||||
$field_description_instance = \Drupal\field\Entity\FieldConfig::create([ |
|
||||||
'field_storage' => $field_description_storage, |
|
||||||
'bundle' => 'message_informatif', |
|
||||||
'label' => 'Description', |
|
||||||
'required' => TRUE, |
|
||||||
// Spécifier le format de texte complet HTML. |
|
||||||
'settings' => [ |
|
||||||
'text_processing' => 0, |
|
||||||
], |
|
||||||
]); |
|
||||||
$field_description_instance->save(); |
|
||||||
|
|
||||||
$field_nom_storage = \Drupal\field\Entity\FieldStorageConfig::create([ |
|
||||||
'field_name' => 'field_importance_du_message', |
|
||||||
'entity_type' => 'node', |
|
||||||
'type' => 'list_string', // Utiliser le type 'list_string' pour une liste de chaînes. |
|
||||||
'settings' => [ |
|
||||||
'allowed_values' => [ |
|
||||||
'Informatif' => 'Informatif', |
|
||||||
'Avertissement' => 'Avertissement', |
|
||||||
'Attention' => 'Attention', |
|
||||||
], |
|
||||||
], |
|
||||||
]); |
|
||||||
$field_nom_storage->save(); |
|
||||||
|
|
||||||
$field_nom_instance = \Drupal\field\Entity\FieldConfig::create([ |
|
||||||
'field_storage' => $field_nom_storage, |
|
||||||
'bundle' => 'message_informatif', |
|
||||||
'label' => 'Importance du message', |
|
||||||
'required' => FALSE, |
|
||||||
]); |
|
||||||
$field_nom_instance->save(); |
|
||||||
|
|
||||||
// Activer les champs sur le formulaire d'affichage du type de contenu. |
|
||||||
$entity_display_repository = \Drupal::service('entity_display.repository'); |
|
||||||
$form_display = $entity_display_repository->getFormDisplay('node', 'message_informatif', 'default'); |
|
||||||
|
|
||||||
// Activer l'affichage du champ 'field_description'. |
|
||||||
$form_display->setComponent('field_message_a_afficher', ['type' => 'text_textarea']); |
|
||||||
|
|
||||||
// Activer l'affichage du champ 'field_importance_du_message'. |
|
||||||
$form_display->setComponent('field_importance_du_message', ['type' => 'text_textfield']); |
|
||||||
|
|
||||||
$form_display->save(); |
|
||||||
// Vider le cache pour appliquer les changements. |
|
||||||
drupal_flush_all_caches(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Implements hook_uninstall(). |
|
||||||
*/ |
|
||||||
function alerte_module_pleiade_uninstall() |
|
||||||
{ |
|
||||||
// Delete all nodes of type 'message_informatif'. |
|
||||||
$query = \Drupal::entityQuery('node') |
|
||||||
->condition('type', 'message_informatif') |
|
||||||
->accessCheck(TRUE); |
|
||||||
$nids = $query->execute(); |
|
||||||
$query = \Drupal::entityQuery('node') |
|
||||||
->condition('type', 'message_informatif') |
|
||||||
->accessCheck(FALSE); // Disable access checks for uninstallation. |
|
||||||
$nids = $query->execute(); |
|
||||||
|
|
||||||
foreach ($nids as $nid) { |
|
||||||
$node = \Drupal\node\Entity\Node::load($nid); |
|
||||||
if ($node) { |
|
||||||
$node->delete(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// Check if the field exists before attempting to delete it. |
|
||||||
if ($field_description = \Drupal\field\Entity\FieldStorageConfig::loadByName('node', 'field_message_a_afficher')) { |
|
||||||
$field_description->delete(); |
|
||||||
} |
|
||||||
if ($field_description = \Drupal\field\Entity\FieldStorageConfig::loadByName('node', 'field_importance_du_message')) { |
|
||||||
$field_description->delete(); |
|
||||||
} |
|
||||||
|
|
||||||
// Delete the type of content. |
|
||||||
if ($node_type = \Drupal::entityTypeManager()->getStorage('node_type')->load('message_informatif')) { |
|
||||||
$node_type->delete(); |
|
||||||
} |
|
||||||
|
|
||||||
// Clear the cache to apply the changes. |
|
||||||
\Drupal::entityTypeManager()->clearCachedDefinitions(); |
|
||||||
// Cache la configuration pour que les changements prennent effet. |
|
||||||
drupal_flush_all_caches(); |
|
||||||
} |
|
||||||
|
|
@ -1,11 +0,0 @@ |
|||||||
alerte_module_pleiade_js: |
|
||||||
js: |
|
||||||
js/alerte_module_pleiade.js: {} |
|
||||||
dependencies: |
|
||||||
- core/drupal |
|
||||||
- core/once |
|
||||||
alerte_module_pleiade_css: |
|
||||||
version: 1.x |
|
||||||
css: |
|
||||||
theme: |
|
||||||
css/alerte_module_pleiade.css: {} |
|
@ -1,7 +0,0 @@ |
|||||||
# alerte_module_pleiade.admin: |
|
||||||
# title: 'API Pléiade - Alerte Module Pléiade' |
|
||||||
# description: 'Page de configuration du module Alerte Pléiade' |
|
||||||
# parent: pleiade.group.admin |
|
||||||
# route_name: alerte_module_pleiade.settings |
|
||||||
# weight: 999 |
|
||||||
|
|
@ -1,14 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
/** |
|
||||||
* @param $variables |
|
||||||
*/ |
|
||||||
|
|
||||||
function alerte_module_pleiade_preprocess_page(&$variables){ |
|
||||||
// Add the CSS library |
|
||||||
$variables['#attached']['library'][] = 'alerte_module_pleiade/alerte_module_pleiade_css'; |
|
||||||
|
|
||||||
// Add the JS library |
|
||||||
$variables['#attached']['library'][] = 'alerte_module_pleiade/alerte_module_pleiade_js'; |
|
||||||
|
|
||||||
} |
|
@ -1,8 +0,0 @@ |
|||||||
alerte_module_pleiade.message_fields: |
|
||||||
path: '/v1/alerte_module_pleiade/message_fields' |
|
||||||
defaults: |
|
||||||
_controller: '\Drupal\alerte_module_pleiade\Controller\AlerteModuleController::message_fields' |
|
||||||
_title: 'Alerte à afficher' |
|
||||||
_format: json |
|
||||||
requirements: |
|
||||||
_access: 'TRUE' |
|
@ -1,10 +0,0 @@ |
|||||||
.message_avertissement p{ |
|
||||||
margin-bottom: 0px!important |
|
||||||
} |
|
||||||
|
|
||||||
.message_avertissement{ |
|
||||||
display:inline-block; |
|
||||||
margin: 0 0 20px 0; |
|
||||||
width: 100%; |
|
||||||
z-index: 9; |
|
||||||
} |
|
@ -1,53 +0,0 @@ |
|||||||
(function (Drupal, drupalSettings, once) { |
|
||||||
"use strict"; |
|
||||||
Drupal.behaviors.APIalerteBlocksBehavior = { |
|
||||||
attach: function (context, settings) { |
|
||||||
// only on frontpage (desktop)
|
|
||||||
once("APIalerteBlocksBehavior", ".message_avertissement", context).forEach( |
|
||||||
function () { |
|
||||||
// make ajax call
|
|
||||||
var xhr = new XMLHttpRequest(); |
|
||||||
xhr.open("GET", Drupal.url("v1/alerte_module_pleiade/message_fields")); |
|
||||||
|
|
||||||
xhr.responseType = "json"; |
|
||||||
xhr.onload = function () { |
|
||||||
if (xhr.status === 200) { |
|
||||||
var donnees = xhr.response; |
|
||||||
|
|
||||||
if (donnees.length > 0) { |
|
||||||
const div_alert = document.querySelector('.message_avertissement'); |
|
||||||
for (var i = 0; i < donnees.length; i++) { |
|
||||||
switch (donnees[i].importance) { |
|
||||||
case "Informatif": |
|
||||||
div_alert.innerHTML += '<div class="py-3 px-5 text-white bg-success d-flex align-items-center justify-content-center ">' + donnees[i].field_message_a_afficher + '</div>'; |
|
||||||
break; |
|
||||||
case "Avertissement": |
|
||||||
div_alert.innerHTML += '<div class="py-3 px-5 text-white bg-warning d-flex align-items-center justify-content-center ">' + donnees[i].field_message_a_afficher + '</div>'; |
|
||||||
break; |
|
||||||
case "Attention": |
|
||||||
div_alert.innerHTML += '<div class="py-3 px-5 text-white bg-danger d-flex align-items-center justify-content-center ">' + donnees[i].field_message_a_afficher + '</div>'; |
|
||||||
break; |
|
||||||
default: |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
}; |
|
||||||
xhr.onerror = function () { |
|
||||||
console.log("Error making AJAX call"); |
|
||||||
}; |
|
||||||
xhr.onabort = function () { |
|
||||||
console.log("AJAX call aborted"); |
|
||||||
}; |
|
||||||
xhr.ontimeout = function () { |
|
||||||
console.log("AJAX call timed out"); |
|
||||||
}; |
|
||||||
xhr.onloadend = function () { |
|
||||||
} |
|
||||||
}; |
|
||||||
xhr.send(); |
|
||||||
}); // end once
|
|
||||||
|
|
||||||
}, |
|
||||||
}; |
|
||||||
})(Drupal, drupalSettings, once); |
|
@ -1,8 +0,0 @@ |
|||||||
Pour fonctionner, il est nécéssaire de créer un type de contenu côté Drupal, pour récupérer et afficher les contenus créés par l'administrateur. |
|
||||||
|
|
||||||
Le type de contenu à créer se nomme "Message Informatif", dont le nom système est "message_informatif". (/admin/structure/types/add) |
|
||||||
|
|
||||||
Pour ce type de contenu, il faut créer deux champs : |
|
||||||
|
|
||||||
- Un champ texte (brut) appelé 'Body' dont le nom sysème est "body" |
|
||||||
- Un champ liste (texte) "Importance du message" dont le nom système est "field_importance_du_message" |
|
@ -1,47 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace Drupal\alerte_module_pleiade\Controller; |
|
||||||
|
|
||||||
use Drupal\Core\Controller\ControllerBase; |
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse; |
|
||||||
use Symfony\Component\HttpFoundation\Request; |
|
||||||
|
|
||||||
|
|
||||||
class AlerteModuleController extends ControllerBase |
|
||||||
{ |
|
||||||
public function __construct() |
|
||||||
{ |
|
||||||
$moduleHandler = \Drupal::service('module_handler'); |
|
||||||
if ($moduleHandler->moduleExists('alerte_module_pleiade')) { |
|
||||||
$this->settings_message = \Drupal::config('alerte_module_pleiade.settings'); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public function message_fields(Request $request) |
|
||||||
{ |
|
||||||
$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); |
|
||||||
|
|
||||||
$msg = []; |
|
||||||
|
|
||||||
foreach ($messages as $message) { |
|
||||||
$body = $message->get('field_message_a_afficher')->value; |
|
||||||
$importance = $message->get('field_importance_du_message')->value; |
|
||||||
|
|
||||||
$msg[] = [ |
|
||||||
'field_message_a_afficher' => $body, |
|
||||||
'importance' => $importance, |
|
||||||
]; |
|
||||||
} |
|
||||||
|
|
||||||
return new JsonResponse(json_encode($msg), 200, [], true); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
} |
|
Loading…
Reference in new issue