You are here

class LogEventSubscriber in farmOS 2.x

Same name in this branch
  1. 2.x modules/log/birth/src/EventSubscriber/LogEventSubscriber.php \Drupal\farm_birth\EventSubscriber\LogEventSubscriber
  2. 2.x modules/core/owner/src/EventSubscriber/LogEventSubscriber.php \Drupal\farm_owner\EventSubscriber\LogEventSubscriber

Perform actions on log presave.

Hierarchy

  • class \Drupal\farm_owner\EventSubscriber\LogEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of LogEventSubscriber

1 string reference to 'LogEventSubscriber'
farm_owner.services.yml in modules/core/owner/farm_owner.services.yml
modules/core/owner/farm_owner.services.yml
1 service uses LogEventSubscriber
farm_owner.log_event_subscriber in modules/core/owner/farm_owner.services.yml
Drupal\farm_owner\EventSubscriber\LogEventSubscriber

File

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

Namespace

Drupal\farm_owner\EventSubscriber
View source
class LogEventSubscriber implements EventSubscriberInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * LogEventSubscriber constructor.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(AccountInterface $current_user) {
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   *
   * @return array
   *   The event names to listen for, and the methods that should be executed.
   */
  public static function getSubscribedEvents() {
    return [
      LogEvent::PRESAVE => 'setLogOwner',
    ];
  }

  /**
   * Set the log owner to the current user, if an owner isn't specified.
   *
   * @param \Drupal\log\Event\LogEvent $event
   *   The log event.
   */
  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(),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LogEventSubscriber::$currentUser protected property The current user.
LogEventSubscriber::getSubscribedEvents public static function
LogEventSubscriber::setLogOwner public function Set the log owner to the current user, if an owner isn't specified.
LogEventSubscriber::__construct public function LogEventSubscriber constructor.