4

The below refers to Craft 3 and the Element API plugin for Craft 3.

I would like to do something when the Element API plugin fires its onBeforeSendData event, I can see in the controller for element there is this:

$this->trigger(self::EVENT_BEFORE_SEND_DATA, new DataEvent([
    'data' => $data,
]));

And based on Craft 3 docs and Yii 2 docs I think I would use the following in my plugins init function:

Event::on(ElementController::class, ElementController::EVENT_BEFORE_SEND_DATA, function(DataEvent $event) {
    var_dump($event->data);
}

I would have expected that to return a League\Fractal\Scope object but instead I am just getting null. Have I implemented this correctly?

Interestingly while trying to work out where I went wrong, if I modify the Element API DefaultController class and change the data property in the trigger to eventData and add the new public property to the DataEvent class then it all works and I am able to see the data in my plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.