decoupled_json_log-1.0.x-dev/src/Hook/DecoupledJsonLogHooks.php
src/Hook/DecoupledJsonLogHooks.php
<?php
namespace Drupal\decoupled_json_log\Hook;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Hook\Attribute\Hook;
/**
* OOP hooks for the Decoupled JSON Log module.
*/
class DecoupledJsonLogHooks {
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
) {
}
/**
* Implements hook_theme().
*/
#[Hook('theme')]
public function theme(): array {
return ['log_json' => ['render element' => 'elements']];
}
/**
* Implements hook_user_cancel().
*/
#[Hook('user_cancel')]
public function userCancel(array $edit, UserInterface $account, string $method): void {
if ($method === 'user_cancel_reassign') {
// Anonymize json logs.
$storage = $this->entityTypeManager->getStorage('log_json');
$log_json_ids = $storage->getQuery()->condition('uid', $account->id())->accessCheck(\FALSE)->execute();
foreach ($storage->loadMultiple($log_json_ids) as $log_json) {
/** @var \Drupal\decoupled_json_log\LogJsonInterface $log_json */
$log_json->setOwnerId(0)->save();
}
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
#[Hook('user_predelete')]
public function userPredelete(UserInterface $account): void {
// Delete json logs that belong to this account.
$storage = $this->entityTypeManager->getStorage('log_json');
$log_json_ids = $storage->getQuery()->condition('uid', $account->id())->accessCheck(\FALSE)->execute();
$storage->delete($storage->loadMultiple($log_json_ids));
}
}
