arch-8.x-1.x-dev/modules/logger/arch_logger.module
modules/logger/arch_logger.module
<?php /** * @file * Logger module. */ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Url; /** * Implements hook_entity_operation_alter(). */ function arch_logger_entity_operation_alter(array &$operations, EntityInterface $entity) { if ( $entity->getEntityTypeId() !== 'order' || !$entity->access('view') ) { return; } $current_user = \Drupal::currentUser(); if (!$current_user->hasPermission('view order history')) { return; } $operations['history'] = [ 'title' => t('History'), 'weight' => 60, 'url' => Url::fromRoute('entity.order.history', ['order' => $entity->id()]), ]; return $operations; } /** * Implements hook_ENTITY_TYPE_insert(). */ function arch_logger_order_insert(EntityInterface $entity) { $container = \Drupal::getContainer(); /** @var \Drupal\arch_logger\Services\ArchLogger $logger_service */ $logger_service = $container->get('arch_logger'); /** @var \Drupal\Core\Session\AccountInterface $current_user */ $current_user = $container->get('current_user'); if ($entity->get('uid')->getString() === $current_user->id()) { $logger_service->saveCartLogs($entity); } $logger_service->insert($entity, 'Order created.'); } /** * Implements hook_ENTITY_TYPE_update(). */ function arch_logger_order_update(EntityInterface $entity) { /** @var \Drupal\arch_logger\Services\ArchLogger $logger_service */ $logger_service = \Drupal::getContainer()->get('arch_logger'); $original_status = $entity->original->get('status')->getString(); $new_status = $entity->get('status')->getString(); $message = 'Order changed.'; if ($original_status !== $new_status) { $message = 'Order status changed.'; } $logger_service->insert($entity, $message); }