3

I am trying to create afterSave plugin for my admin form save action controller of my custom module. The purpose of this is to save some data into second table once all data completely save in first table.

Here is my di.xml file code :

<type name="Namespace\Module\Controller\Adminhtml\Options\Save">
 <plugin name="save_option_label" type="Namespace\Module\Plugin\OptionlabelSave" sortOrder="1" />
</type>

I have also created OptionlabelSave.php file inside \Plugin\ directory.

Here is the code :

<?php
namespace Namespace\Module\Plugin;
/**
 * Class OptionlabelSave
 * @package Namespace\Module\Plugin
 */
class OptionlabelSave
{
 public function afterSave(Namespace\Module\Controller\Adminhtml\Options\Save $subject, $result) {
 echo 'test save'; exit;
 return $result;
 }
}

However, when I click on save button in admin edit form I am not getting 'test save' printed on my screen. Am I missing anything here?

Please help. That would be highly appreciated.

Thanks,

asked Aug 28, 2018 at 6:46
4
  • Can you please add Namespace\Module\Controller\Adminhtml\Options\Save code here? Commented Aug 28, 2018 at 7:56
  • Hello @RutveeSojitra. That is normal save data code using model nothing exceptional. I think I came to know from some googling that we can't use plugin actions after,around and before for Controller. It can be use for model. For controller we need to use Preference that is actually overwrite. Is it true? Commented Aug 28, 2018 at 9:40
  • 1
    No, we can do also in controller please refer for more mageplaza.com/… Commented Aug 28, 2018 at 9:52
  • @RutveeSojitra Thanks for the reference. I will refer it. Commented Aug 28, 2018 at 10:07

1 Answer 1

2

I have found the solution myself. Earlier, I have used afterSave() method inside plugin file. Instead, we need to use either afterExecute() or aroundExecute() depending upon the type of requirement.

That's it!

Thanks,

answered Sep 5, 2018 at 12:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.