Tuesday, February 22, 2022

[FIXED] Passing a message from a listener

Issue

I have an event that I'm creating in my model's beforeSave function:

  public function beforeSave(Event $event, Entity $entity) {
    $event = new Event('Model.Transaction.createTransaction', $this, [
      'transaction' => $entity
    ]);
    $this->eventManager()->dispatch($event);

    if ($event->isStopped()) {
      return FALSE;
    }
  }

Which is grabbed by a Listener I have created:

public function implementedEvents() {
  return [
    'Model.Transaction.createTransaction' => 'createHistorical'
  ];
}

This all works fine. What I was wondering is if there's any way to pass a message back to the model where the event was registered. So that if there's a failure or something, I can pass a message back to the model and add it to the errors to be displayed on the form?


Solution

You can return a value from the callback method (createHistorical in your case) which would be accessible using $event->result after dispatching is done.



Answered By - ADmad

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.