You are here

public function LogEventSubscriber::setLogOwner in farmOS 2.x

Set the log owner to the current user, if an owner isn't specified.

Parameters

\Drupal\log\Event\LogEvent $event: The log event.

File

modules/core/owner/src/EventSubscriber/LogEventSubscriber.php, line 49

Class

LogEventSubscriber
Perform actions on log presave.

Namespace

Drupal\farm_owner\EventSubscriber

Code

public function setLogOwner(LogEvent $event) : void {

  // Get the log entity from the event.
  $log = $event->log;

  // If there is no currently logged in user, bail.
  if (empty($this->currentUser
    ->id())) {
    return;
  }

  // If the log already has an owner, bail.
  $owners = $log
    ->get('owner')
    ->referencedEntities();
  if (!empty($owners)) {
    return;
  }

  // Add the current user to the log's owners.
  $log->owner[] = [
    'target_id' => $this->currentUser
      ->id(),
  ];
}