This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

JimRed
JimRed

Hello Guys,

I have a plugin that uses model event to fire a custom event.

public function beforeCreate()
{
    Event::fire('custom.event', [$this]);
}

It works fine under OctoberCMS v469 but under new OctoberCMS v1.1 (L6) version it throws error that "Call to a member funtion on a non-object".
It seems under OctoberCMS v1.1 version variable $this does not contain the model object.

Do you have any idea what could be the problem with it?

Thanks in advance

Last updated

mjauvin
mjauvin

I'll investigate, but can you confirm the actual version? v469 === 1.0.469 and the new (L6) branch is 1.1.x ...

So maybe use 1.0.469 in composer or use 1.0. if you want the old branch, otherwise use 1.1.

JimRed
JimRed

I'll investigate, but can you confirm the actual version? v469 === 1.0.469 and the new (L6) branch is 1.1.x ...

Old composer: "october/backend": "~1.0",
New composer: "october/backend": "1.1.*",

Misstyped in my previous comment, sorry.
I think I used latest versions of both build (October with Larave 5 and Laravel 6) during my tests.
Thanks if you can check what could be the problem with model Events in new version.

Ps: Also a bit strange because I receive the following result under new version when check build number (I do not know if it should return this):

php artisan october:version
*** Detecting October CMS build...
*** Detected a modified version of October CMS build 469.

Last updated

mjauvin
mjauvin

Yeah, that's normal.

mjauvin
mjauvin

Can you show the event handler you're using for this?

JimRed
JimRed

Of course. Here it is:

...Models\User.php

class User extends Model
{
    public function beforeCreate()
    {
        $password = str_random(10);
        Event::fire('user.password.new', [$this, $password]);
    }
}

Plugin's init.php

Event::listen('user.password.*', function($user, $password) {
    $user->password = $password;
    return $password;
});
mjauvin
mjauvin

Ok, I'll try to reproduce.

mjauvin
mjauvin

I was able to reproduce. Interestingly enough, if you use the full event name (no wildcards), it works fine...

Can you confirm?

mjauvin
mjauvin

Ok, I found the change that creates this problem:

commit 8b61480f1f79069195896b9aed934b953977b2f0
Author: Luke Towers <luke@luketowers.ca>
Date:   2020-03-02 20:30:27 -0600

    Bring event dispatcher more in line with Laravel's dispatcher.

    This brings October's event dispatcher more in line with Laravel's by changing the parameters sent to wild card listeners (introduced in https://github.com/laravel/framework/commit/dbbfc62beff1625b0d45bbf39650d047555cf4fa but accidentally overlooked in the 5.5 update release).

    This changes wildcard event listener signatures from function (...$params) {} to function ($event, $params) {}. Note that this change basically obsolutes the Event::firing() functionality, so a decision to keep or remove that will be required as well.

    Additionally, this adds in the wildcard event listener cache added in https://github.com/laravel/framework/commit/c86efa6caade8aa6a1507012ccb9522eb554dfe1
mjauvin
mjauvin

For now, I suggest not using wilcard listeners until this gets fixed.

Last updated

JimRed
JimRed

You are right. It works fine without wildcards.
I noticed that I can specify event name as array so it can be used as an alternative instead of wildcards:

Event::listen(['user.password.new','user.password.reset'], function($user, $password) {
    $user->password = $password;
    return $password;
});

Thank you very much for your help!

mjauvin
mjauvin

The other alternative is to use the new format (still not documented it seems):

Event::listen('user.password.*', function($event, $data) {
    list($user, $password) = $data;
    $user->password = $password;
    return $password;
});

Last updated

JenniferLopez
JenniferLopez

@JimRed the wildcard event handlers were changed in October 1.1.0 (L6) - you can review the release notes here for the documented change: https://octobercms.com/support/article/rn-11#upgrade-wildcard-listeners

mjauvin
mjauvin

@JimRed is the plugin in question in the marketplace or is it a private plugin?

JimRed
JimRed

It is only a private plugin used for frontend ldap authentication and user access management.
Thanks for the help and the detailed code sample.

1-15 of 15

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.