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,33 @@ public function execute()
4846 if (empty ($ data ['tag_id ' ])) {
4947 $ data ['tag_id ' ] = null ;
5048 }
51-
49+ if (!empty ($ data ['identifier ' ])) {
50+ $ data ['identifier ' ] = preg_replace ('/(#)|(%)|(&)|({)|(})|(!)|(@)|(:)|(;)|(,)|(<)|(>)|(=)/ ' , '' , $ data ['identifier ' ]);
51+ $ data ['identifier ' ] = str_replace (" " ,"- " ,trim ($ data ['identifier ' ]));
52+ $ data ['identifier ' ] = strtolower ($ data ['identifier ' ]);
53+ }
5254 /** @var \Lof\ProductTags\Model\Tag $model */
5355 $ model = $ this ->TagFactory ->create ();
5456 $ id = $ this ->getRequest ()->getParam ('tag_id ' );
5557 if ($ id ) {
5658 try {
57- $ model = $ this -> tagRepository -> getById ($ id );
59+ $ model = $ model -> load ($ id );
5860 } catch (LocalizedException $ e ) {
5961 $ this ->messageManager ->addErrorMessage (__ ('This tag no longer exists. ' ));
6062 return $ resultRedirect ->setPath ('*/*/ ' );
6163 }
6264 }
63-
6465 $ model ->setData ($ data );
66+ if (isset ($ data ['tag_products ' ])
67+ && is_string ($ data ['tag_products ' ])) {
68+ $ products = json_decode ($ data ['tag_products ' ], true );
69+ $ model ->setPostedProducts ($ products );
70+ }
71+ $ this ->_eventManager ->dispatch (
72+ 'lof_producttags_prepare_save ' ,
73+ ['tag ' => $ model , 'request ' => $ this ->getRequest ()]
74+ );
75+ $ products = $ model ->getPostedProducts ();
6576 try {
6677 $ model ->save ($ model );
6778 $ this ->messageManager ->addSuccessMessage (__ ('You saved the tag. ' ));
@@ -72,7 +83,6 @@ public function execute()
7283 } catch (\Exception $ e ) {
7384 $ this ->messageManager ->addExceptionMessage ($ e , __ ('Something went wrong while saving the tag. ' ));
7485 }
75-
7686 $ this ->dataPersistor ->set ('lof_productags_tag ' , $ data );
7787 return $ resultRedirect ->setPath ('*/*/edit ' , ['tag_id ' => $ id ]);
7888 }
@@ -81,7 +91,6 @@ public function execute()
8191 private function processBlockReturn ($ model , $ data , $ resultRedirect )
8292 {
8393 $ redirect = $ data ['back ' ] ?? 'close ' ;
84-
8594 if ($ redirect ==='continue ' ) {
8695 $ resultRedirect ->setPath ('*/*/edit ' , ['tag_id ' => $ model ->getId ()]);
8796 } else if ($ redirect === 'close ' ) {
@@ -99,4 +108,4 @@ private function processBlockReturn($model, $data, $resultRedirect)
99108 }
100109 return $ resultRedirect ;
101110 }
102- }
111+ }
0 commit comments