You are here

class User in Audit Log 8

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

Processes User entity events.

@package Drupal\audit_log\EventSubscriber

Hierarchy

Expanded class hierarchy of User

1 string reference to 'User'
audit_log.services.yml in ./audit_log.services.yml
audit_log.services.yml
1 service uses User
audit_log.event_subscriber.user in ./audit_log.services.yml
Drupal\audit_log\EventSubscriber\User

File

src/EventSubscriber/User.php, line 13

Namespace

Drupal\audit_log\EventSubscriber
View source
class User implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members