2

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,

  • Can you please add Namespace\Module\Controller\Adminhtml\Options\Save code here? – Rutvee Sojitra Aug 28 '18 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? – Patrick W Aug 28 '18 at 9:40
  • 1
    No, we can do also in controller please refer for more mageplaza.com/… – Rutvee Sojitra Aug 28 '18 at 9:52
  • @RutveeSojitra Thanks for the reference. I will refer it. – Patrick W Aug 28 '18 at 10:07
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,

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.