9
9
10
10
use Magento \Framework \Exception \LocalizedException ;
11
11
use Magento \Framework \Message \ManagerInterface ;
12
- use Magento \Cms \Model \PageFactory ;
12
+ use Magento \Cms \Model \PageRepository ;
13
13
14
+ /**
15
+ * Class for layout update validation
16
+ */
14
17
class CustomLayoutValidator
15
18
{
16
19
/**
@@ -25,11 +28,11 @@ class CustomLayoutValidator
25
28
26
29
/**
27
30
* CustomLayoutValidator constructor.
28
- * @param PageFactory $pageFactory
31
+ * @param PageRepository $pageFactory
29
32
* @param ManagerInterface $messageManager
30
33
*/
31
34
public function __construct (
32
- PageFactory $ pageFactory ,
35
+ PageRepository $ pageFactory ,
33
36
ManagerInterface $ messageManager
34
37
) {
35
38
$ this ->pageFactory = $ pageFactory ;
@@ -43,18 +46,23 @@ public function __construct(
43
46
* @return bool
44
47
* @throws LocalizedException
45
48
*/
46
- public function validate (array $ data )
49
+ public function validate (array $ data ) : bool
47
50
{
48
51
$ layoutUpdate = isset ($ data ['layout_update_xml ' ]) ? $ data ['layout_update_xml ' ] : null ;
49
52
$ customLayoutUpdate = isset ($ data ['custom_layout_update_xml ' ]) ? $ data ['custom_layout_update_xml ' ] : null ;
50
- $ page = $ this ->pageFactory ->create ();
51
- $ page ->load ($ data ['page_id ' ]);
52
- $ oldLayoutUpdate = $ page ->getId () ? $ page ->getLayoutUpdateXml () : null ;
53
- $ olCustomLayoutUpdate = $ page ->getId () ? $ page ->getCustomLayoutUpdateXml () : null ;
54
- if ($ layoutUpdate && $ oldLayoutUpdate !== $ layoutUpdate
55
- || $ customLayoutUpdate && $ olCustomLayoutUpdate !== $ customLayoutUpdate
56
- ) {
57
- throw new LocalizedException (__ ('Custom layout update text cannot be changed, only removed ' ));
53
+ if (isset ($ data ['page_id ' ])) {
54
+ $ page = $ this ->pageFactory ->getById ($ data ['page_id ' ]);
55
+ $ oldLayoutUpdate = $ page ->getId () ? $ page ->getLayoutUpdateXml () : null ;
56
+ $ oldCustomLayoutUpdate = $ page ->getId () ? $ page ->getCustomLayoutUpdateXml () : null ;
57
+ if ($ layoutUpdate && $ oldLayoutUpdate !== $ layoutUpdate
58
+ || $ customLayoutUpdate && $ oldCustomLayoutUpdate !== $ customLayoutUpdate
59
+ ) {
60
+ throw new LocalizedException (__ ('Custom layout update text cannot be changed, only removed ' ));
61
+ }
62
+ } else {
63
+ if ($ layoutUpdate || $ customLayoutUpdate ) {
64
+ throw new LocalizedException (__ ('Custom layout update text cannot be changed, only removed ' ));
65
+ }
58
66
}
59
67
return true ;
60
68
}
0 commit comments