4
4
* See COPYING.txt for license details.
5
5
*/
6
6
namespace Lof \ProductTags \Controller \Adminhtml \Tag ;
7
-
8
7
use Magento \Framework \App \Action \HttpPostActionInterface ;
9
8
use Magento \Backend \App \Action \Context ;
10
9
use Lof \ProductTags \Api \TagRepositoryInterface ;
13
12
use Magento \Framework \Exception \LocalizedException ;
14
13
use Magento \Framework \Registry ;
15
14
use Magento \Framework \Stdlib \DateTime \Filter \Date ;
16
-
17
15
/**
18
16
* Save Lof Tag action.
19
17
*/
@@ -48,20 +46,33 @@ public function execute()
48
46
if (empty ($ data ['tag_id ' ])) {
49
47
$ data ['tag_id ' ] = null ;
50
48
}
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
+ }
52
54
/** @var \Lof\ProductTags\Model\Tag $model */
53
55
$ model = $ this ->TagFactory ->create ();
54
56
$ id = $ this ->getRequest ()->getParam ('tag_id ' );
55
57
if ($ id ) {
56
58
try {
57
- $ model = $ this -> tagRepository -> getById ($ id );
59
+ $ model = $ model -> load ($ id );
58
60
} catch (LocalizedException $ e ) {
59
61
$ this ->messageManager ->addErrorMessage (__ ('This tag no longer exists. ' ));
60
62
return $ resultRedirect ->setPath ('*/*/ ' );
61
63
}
62
64
}
63
-
64
65
$ 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 ();
65
76
try {
66
77
$ model ->save ($ model );
67
78
$ this ->messageManager ->addSuccessMessage (__ ('You saved the tag. ' ));
@@ -72,7 +83,6 @@ public function execute()
72
83
} catch (\Exception $ e ) {
73
84
$ this ->messageManager ->addExceptionMessage ($ e , __ ('Something went wrong while saving the tag. ' ));
74
85
}
75
-
76
86
$ this ->dataPersistor ->set ('lof_productags_tag ' , $ data );
77
87
return $ resultRedirect ->setPath ('*/*/edit ' , ['tag_id ' => $ id ]);
78
88
}
@@ -81,7 +91,6 @@ public function execute()
81
91
private function processBlockReturn ($ model , $ data , $ resultRedirect )
82
92
{
83
93
$ redirect = $ data ['back ' ] ?? 'close ' ;
84
-
85
94
if ($ redirect ==='continue ' ) {
86
95
$ resultRedirect ->setPath ('*/*/edit ' , ['tag_id ' => $ model ->getId ()]);
87
96
} else if ($ redirect === 'close ' ) {
@@ -99,4 +108,4 @@ private function processBlockReturn($model, $data, $resultRedirect)
99
108
}
100
109
return $ resultRedirect ;
101
110
}
102
- }
111
+ }
0 commit comments