Ivan Gretsky Posted August 17, 2020 Share Posted August 17, 2020 Good day! I have a need to replace the core class method with a hook. There is an example of this in docs, It is from the class context. I need it from the template context (admin.php). I want to change the behavior of the core class method a little bit, so do not want to re-write it entirely. So I copy-and-paste the method to the function that should be used to replace it and make my minor changes. But multiple uses of $this cause an error I am not sure how to resolve. Could you please advise how to do it. Replace the method of the core class from template context not having to rewrite the entire method? Link to comment Share on other sites More sharing options...
kongondo Posted August 18, 2020 Share Posted August 18, 2020 14 hours ago, Ivan Gretsky said: the core class method Which method exactly? It would help us reproduce/test. 14 hours ago, Ivan Gretsky said: There is an example of this in docs, It is from the class context. I need it from the template context (admin.php) I don't quite get this. Aren't all hooks called from a class context? Perhaps you meant to say frontend versus backend? 14 hours ago, Ivan Gretsky said: But multiple uses of $this cause an error I am not sure how to resolve. What errors? Where are you calling the hook? Some code to look at would help :-). 2 Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted September 8, 2020 Author Share Posted September 8, 2020 Thanks, @kongondo! Sorry for late reply. But now I am at this once again) This is what I'm trying to do in admin.php: /** * Replace the execute method of ProcessLogin module to replace * the getLoginName() call with getLoginName2(). The former method is not hookable. * The latter method is added via hook/ * * All this is done to allow users to login with their email changed on the fly * to user name (replace '@' with '-'). * */ wire()->addHookBefore('ProcessLogin::execute', 'processLoginExecute2'); ... getLoginExecute2() is just a copy of ProcessLogin::execute with minor changes. I copy it to admin.php like this: function processLoginExecute2(HookEvent $event) { $event->replace = true; /** @var Session $session */ $session = $this2->wire('session'); /** @var WireInput $input */ $input = $this2->wire('input'); /** @var User $user */ $user = $this2->wire('user'); if($user->isLoggedin()) { ... And then I get an error with $this variable. It must be it is not in scope or something. Link to comment Share on other sites More sharing options...
kongondo Posted September 8, 2020 Share Posted September 8, 2020 2 hours ago, Ivan Gretsky said: And then I get an error with $this variable. It must be it is not in scope or something. Yes, from your code, I can see that it isn't. You are inside a function. Use wire instead (wire('session'), etc). Did you try that? Why $this2, by the way? 1 Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted September 8, 2020 Author Share Posted September 8, 2020 4 minutes ago, kongondo said: Yes, from your code, I can see that it isn't. You are inside a function. Use wire instead (wire('session'), etc). Did you try that? Why $this2, by the way? That's the point of it all - I do not want to rewrite all $this occurrences))) Thought there is some smart way to avoid this. $this2 was a try to somehow substitute $this with something I can get api variables from. But I guess this will not help if there is some module method/property calls. Maybe I could inject $this as a dependency from a hook somehow (I do not even understand if I am writing proper terms here))))))? Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 8, 2020 Share Posted September 8, 2020 $this is a special variable within classes to refer to the current instance. It's simply not available in hooks. But $event->object should refer to the current object instance, but I'd expect you'll only have access to public methods and properties that way. So because of the context switch (within the class vs. outside) you might still need to change things. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now