44 * See COPYING.txt for license details.
55 */
66namespace Lof \ProductTags \Controller \Adminhtml \Tag ;
7-
87use Magento \Framework \App \Action \HttpPostActionInterface ;
98use Magento \Backend \App \Action \Context ;
109use Lof \ProductTags \Api \TagRepositoryInterface ;
1312use Magento \Framework \Exception \LocalizedException ;
1413use Magento \Framework \Registry ;
1514use Magento \Framework \Stdlib \DateTime \Filter \Date ;
16-
1715/**
1816 * Save Lof Tag action.
1917 */
@@ -48,20 +46,28 @@ public function execute()
4846 if (empty ($ data ['tag_id ' ])) {
4947 $ data ['tag_id ' ] = null ;
5048 }
51-
5249 /** @var \Lof\ProductTags\Model\Tag $model */
5350 $ model = $ this ->TagFactory ->create ();
5451 $ id = $ this ->getRequest ()->getParam ('tag_id ' );
5552 if ($ id ) {
5653 try {
57- $ model = $ this -> tagRepository -> getById ($ id );
54+ $ model = $ model -> load ($ id );
5855 } catch (LocalizedException $ e ) {
5956 $ this ->messageManager ->addErrorMessage (__ ('This tag no longer exists. ' ));
6057 return $ resultRedirect ->setPath ('*/*/ ' );
6158 }
6259 }
63-
6460 $ model ->setData ($ data );
61+ if (isset ($ data ['tag_products ' ])
62+ && is_string ($ data ['tag_products ' ])) {
63+ $ products = json_decode ($ data ['tag_products ' ], true );
64+ $ model ->setPostedProducts ($ products );
65+ }
66+ $ this ->_eventManager ->dispatch (
67+ 'lof_producttags_prepare_save ' ,
68+ ['tag ' => $ model , 'request ' => $ this ->getRequest ()]
69+ );
70+ $ products = $ model ->getPostedProducts ();
6571 try {
6672 $ model ->save ($ model );
6773 $ this ->messageManager ->addSuccessMessage (__ ('You saved the tag. ' ));
@@ -72,7 +78,6 @@ public function execute()
7278 } catch (\Exception $ e ) {
7379 $ this ->messageManager ->addExceptionMessage ($ e , __ ('Something went wrong while saving the tag. ' ));
7480 }
75-
7681 $ this ->dataPersistor ->set ('lof_productags_tag ' , $ data );
7782 return $ resultRedirect ->setPath ('*/*/edit ' , ['tag_id ' => $ id ]);
7883 }
@@ -81,7 +86,6 @@ public function execute()
8186 private function processBlockReturn ($ model , $ data , $ resultRedirect )
8287 {
8388 $ redirect = $ data ['back ' ] ?? 'close ' ;
84-
8589 if ($ redirect ==='continue ' ) {
8690 $ resultRedirect ->setPath ('*/*/edit ' , ['tag_id ' => $ model ->getId ()]);
8791 } else if ($ redirect === 'close ' ) {
@@ -99,4 +103,4 @@ private function processBlockReturn($model, $data, $resultRedirect)
99103 }
100104 return $ resultRedirect ;
101105 }
102- }
106+ }
0 commit comments