You are here

public function User::reactTo in Audit Log 8

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/User.php \Drupal\audit_log\EventSubscriber\User::reactTo()

Processes an event.

Parameters

\Drupal\audit_log\AuditLogEventInterface $event: The audit event.

Return value

bool TRUE if the event subscriber handled the event.

Overrides EventSubscriberInterface::reactTo

File

src/EventSubscriber/User.php, line 18

Class

User
Processes User entity events.

Namespace

Drupal\audit_log\EventSubscriber

Code

public function reactTo(AuditLogEventInterface $event) {
  $entity = $event
    ->getEntity();
  if ($entity
    ->getEntityTypeId() != 'user') {
    return FALSE;
  }
  $event_type = $event
    ->getEventType();
  $args = [
    '@name' => $entity
      ->label(),
  ];
  $current_state = $entity->status->value ? 'active' : 'blocked';
  $original_state = NULL;
  if (isset($entity->original) && $entity->original instanceof UserInterface) {
    $original_state = $entity->original->status->value ? 'active' : 'blocked';
  }
  if ($event_type == 'insert') {
    $event
      ->setMessage(t('@name was created.', $args))
      ->setPreviousState(NULL)
      ->setCurrentState($current_state);
    return TRUE;
  }
  if ($event_type == 'update') {
    $event
      ->setMessage(t('@name was updated.', $args))
      ->setPreviousState($original_state)
      ->setCurrentState($current_state);
    return TRUE;
  }
  if ($event_type == 'delete') {
    $event
      ->setMessage(t('@name was updated.', $args))
      ->setPreviousState($original_state)
      ->setCurrentState(NULL);
    return TRUE;
  }
  return FALSE;
}