1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Cms\Controller\Adminhtml\Block;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Backend\App\Action\Context;
use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Cms\Model\Block;
use Magento\Cms\Model\BlockFactory;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
/**
* Save CMS block action.
*/
class Save extends \Magento\Cms\Controller\Adminhtml\Block implements HttpPostActionInterface
{
/**
* @var DataPersistorInterface
*/
protected $dataPersistor;
/**
* @var BlockFactory
*/
private $blockFactory;
/**
* @var BlockRepositoryInterface
*/
private $blockRepository;
/**
* @param Context $context
* @param Registry $coreRegistry
* @param DataPersistorInterface $dataPersistor
* @param BlockFactory|null $blockFactory
* @param BlockRepositoryInterface|null $blockRepository
*/
public function __construct(
Context $context,
Registry $coreRegistry,
DataPersistorInterface $dataPersistor,
BlockFactory $blockFactory = null,
BlockRepositoryInterface $blockRepository = null
) {
$this->dataPersistor = $dataPersistor;
$this->blockFactory = $blockFactory
?: \Magento\Framework\App\ObjectManager::getInstance()->get(BlockFactory::class);
$this->blockRepository = $blockRepository
?: \Magento\Framework\App\ObjectManager::getInstance()->get(BlockRepositoryInterface::class);
parent::__construct($context, $coreRegistry);
}
/**
* Save action
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->getRequest()->getPostValue();
if ($data) {
if (isset($data['is_active']) && $data['is_active'] === 'true') {
$data['is_active'] = Block::STATUS_ENABLED;
}
if (empty($data['block_id'])) {
$data['block_id'] = null;
}
/** @var \Magento\Cms\Model\Block $model */
$model = $this->blockFactory->create();
$id = $this->getRequest()->getParam('block_id');
if ($id) {
try {
$model = $this->blockRepository->getById($id);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage(__('This block no longer exists.'));
return $resultRedirect->setPath('*/*/');
}
}
$model->setData($data);
try {
$this->blockRepository->save($model);
$this->messageManager->addSuccessMessage(__('You saved the block.'));
$this->dataPersistor->clear('cms_block');
return $this->processBlockReturn($model, $data, $resultRedirect);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the block.'));
}
$this->dataPersistor->set('cms_block', $data);
return $resultRedirect->setPath('*/*/edit', ['block_id' => $id]);
}
return $resultRedirect->setPath('*/*/');
}
/**
* Process and set the block return
*
* @param \Magento\Cms\Model\Block $model
* @param array $data
* @param \Magento\Framework\Controller\ResultInterface $resultRedirect
* @return \Magento\Framework\Controller\ResultInterface
*/
private function processBlockReturn($model, $data, $resultRedirect)
{
$redirect = $data['back'] ?? 'close';
if ($redirect ==='continue') {
$resultRedirect->setPath('*/*/edit', ['block_id' => $model->getId()]);
} else if ($redirect === 'close') {
$resultRedirect->setPath('*/*/');
} else if ($redirect === 'duplicate') {
$duplicateModel = $this->blockFactory->create(['data' => $data]);
$duplicateModel->setId(null);
$duplicateModel->setIdentifier($data['identifier'] . '-' . uniqid());
$duplicateModel->setIsActive(Block::STATUS_DISABLED);
$this->blockRepository->save($duplicateModel);
$id = $duplicateModel->getId();
$this->messageManager->addSuccessMessage(__('You duplicated the block.'));
$this->dataPersistor->set('cms_block', $data);
$resultRedirect->setPath('*/*/edit', ['block_id' => $id]);
}
return $resultRedirect;
}
}