cancel
Showing results for 
Search instead for 
Did you mean: 

Skip function execution using before plugin

SOLVED
Highlighted

Skip function execution using before plugin

Hi there,

 

How can I skip function execution (execute() method) using a plugin with a beforeExecute() method? 

 

Basically what I want is to do a redirect in beforeExecute() method if a condition is met and never trigger the execute() method. If that condition is not met, then it should follow the path: beforeExecute(), execute() and so on. 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: Skip function execution using before plugin

1) Before Plugin:

Before Plugin is used for changing the arguments of an original method or add some behavior before an original method called.To use before plugin, we need to use before prefix and its added to the name of an original method.The before plugin methods do not need to have a return value.

Prototype: beforeMethodname()

2) After Plugin:

After Plugin is used for changing the arguments of an original method or add some behavior before an original method called.To use after plugin, we need to use after prefix and its added to the name of an original method.The after plugin methods do not need to have a return value.

Prototype: afterMethodname()

3) Around Plugin:

After Plugin is used for changing both the arguments and returned values of an original method or add some behavior before and after an original method is called. We need to use after prefix and its added to the name of an original method.The around plugin must have a return value.


Note: If the around method does not call the callable ($proceed), it will prevent the execution of all the plugins next in the chain and the original method call. (That means next around and before and after plugins into other modules will not call if this situation occur in your case )


if issue solved and help ,Click Kudos & Accept as Solution

View solution in original post

4 REPLIES 4
Highlighted

Re: Skip function execution using before plugin

Hi @Anonymous 

 

As per my understanding you want to add a skip condition in the beforeExecute method. Can you please tell me which method you are using to override default execute method?

 

Are you using an observer to override? If yes can you please share some more details.

 

Thanks

Highlighted

Re: Skip function execution using before plugin

Hi @PankajS_Magento,

 

I'm using a plugin for this. 

 

Basically, I have a controller which has multiple methods alongside the main one which is execute(). In this method there are some code lines which update some customer data like first name, last name, email, password and so on.

 

About what I've said above ... it's part of a module which I don't intend to override (core files), but I try to extend it somehow using a plugin from my custom module, due to lack of some validation. That's why I want to skip the execution call in a beforeExecute() interceptor. 

 

What I intend to do is to make a redirect to a specific page if a condition isn't met and stop calling the execute() method.

 

I succeeded somehow using the aroundExecute() method. In the attached picture is a piece of code from the around interceptor which highlights the way I did it. Basically, if those conditions are met, the redirect is triggered and the execution is not moving forward to execute() method. If it's not triggered, then return $proceed() is executed which continues the execution with the execute() method.

It's working as I did it, but I'm wondering if I can accomplish the same thing using beforeExecute() method. 

 

screenshot_1.png
PS. This is just a piece of code, but I have the constructor and all the other stuff included.

Highlighted

Re: Skip function execution using before plugin

1) Before Plugin:

Before Plugin is used for changing the arguments of an original method or add some behavior before an original method called.To use before plugin, we need to use before prefix and its added to the name of an original method.The before plugin methods do not need to have a return value.

Prototype: beforeMethodname()

2) After Plugin:

After Plugin is used for changing the arguments of an original method or add some behavior before an original method called.To use after plugin, we need to use after prefix and its added to the name of an original method.The after plugin methods do not need to have a return value.

Prototype: afterMethodname()

3) Around Plugin:

After Plugin is used for changing both the arguments and returned values of an original method or add some behavior before and after an original method is called. We need to use after prefix and its added to the name of an original method.The around plugin must have a return value.


Note: If the around method does not call the callable ($proceed), it will prevent the execution of all the plugins next in the chain and the original method call. (That means next around and before and after plugins into other modules will not call if this situation occur in your case )


if issue solved and help ,Click Kudos & Accept as Solution

View solution in original post

Highlighted

Re: Skip function execution using before plugin

@prakash786,

 

Thank you for your answer. Seem that I was right about using the around method, but also misunderstood a little bit these concepts because I thought that I can do it somehow using the before method.