notification_popin-1.2.2/notification_popin.module
notification_popin.module
<?php declare(strict_types = 1);
/**
* @file
* Provides a notification entity type.
*/
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
/**
* Implements hook_theme().
*/
function notification_popin_theme(): array {
return [
'notification' => ['render element' => 'elements'],
];
}
/**
* Prepares variables for notification templates.
*
* Default template: notification.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the notification information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_notification(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_user_cancel().
*/
function notification_popin_user_cancel($edit, UserInterface $account, $method): void {
switch ($method) {
case 'user_cancel_block_unpublish':
// Unpublish notifications.
$storage = \Drupal::entityTypeManager()->getStorage('notification');
$notification_ids = $storage->getQuery()
->condition('uid', $account->id())
->condition('status', 1)
->accessCheck(FALSE)
->execute();
foreach ($storage->loadMultiple($notification_ids) as $notification) {
$notification->set('status', FALSE)->save();
}
break;
case 'user_cancel_reassign':
// Anonymize notifications.
$storage = \Drupal::entityTypeManager()->getStorage('notification');
$notification_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
foreach ($storage->loadMultiple($notification_ids) as $notification) {
$notification->setOwnerId(0)->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
function notification_popin_user_predelete(UserInterface $account): void {
// Delete notifications that belong to this account.
$storage = \Drupal::entityTypeManager()->getStorage('notification');
$notification_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
$storage->delete(
$storage->loadMultiple($notification_ids)
);
}
/**
* Implements hook_preprocess_html().
*/
function notification_popin_preprocess_html(&$variables) {
if (!\Drupal::service('router.admin_context')->isAdminRoute()) {
$variables['#attached']['library'][] = 'notification_popin/notification';
$variables['#attached']['drupalSettings']['notification_popin']['apiPath'] = Url::fromRoute("api.notifications.get")->toString();
}
}