Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
13 use Magento\Cms\Model\BlockFactory;
17 
19 {
23  protected $dataPersistor;
24 
28  private $blockFactory;
29 
33  private $blockRepository;
34 
42  public function __construct(
43  Context $context,
44  Registry $coreRegistry,
46  BlockFactory $blockFactory = null,
47  BlockRepositoryInterface $blockRepository = null
48  ) {
49  $this->dataPersistor = $dataPersistor;
50  $this->blockFactory = $blockFactory
51  ?: \Magento\Framework\App\ObjectManager::getInstance()->get(BlockFactory::class);
52  $this->blockRepository = $blockRepository
53  ?: \Magento\Framework\App\ObjectManager::getInstance()->get(BlockRepositoryInterface::class);
54  parent::__construct($context, $coreRegistry);
55  }
56 
63  public function execute()
64  {
66  $resultRedirect = $this->resultRedirectFactory->create();
67  $data = $this->getRequest()->getPostValue();
68  if ($data) {
69  if (isset($data['is_active']) && $data['is_active'] === 'true') {
70  $data['is_active'] = Block::STATUS_ENABLED;
71  }
72  if (empty($data['block_id'])) {
73  $data['block_id'] = null;
74  }
75 
77  $model = $this->blockFactory->create();
78 
79  $id = $this->getRequest()->getParam('block_id');
80  if ($id) {
81  try {
82  $model = $this->blockRepository->getById($id);
83  } catch (LocalizedException $e) {
84  $this->messageManager->addErrorMessage(__('This block no longer exists.'));
85  return $resultRedirect->setPath('*/*/');
86  }
87  }
88 
89  $model->setData($data);
90 
91  try {
92  $this->blockRepository->save($model);
93  $this->messageManager->addSuccessMessage(__('You saved the block.'));
94  $this->dataPersistor->clear('cms_block');
95  return $this->processBlockReturn($model, $data, $resultRedirect);
96  } catch (LocalizedException $e) {
97  $this->messageManager->addErrorMessage($e->getMessage());
98  } catch (\Exception $e) {
99  $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the block.'));
100  }
101 
102  $this->dataPersistor->set('cms_block', $data);
103  return $resultRedirect->setPath('*/*/edit', ['block_id' => $id]);
104  }
105  return $resultRedirect->setPath('*/*/');
106  }
107 
116  private function processBlockReturn($model, $data, $resultRedirect)
117  {
118  $redirect = $data['back'] ?? 'close';
119 
120  if ($redirect ==='continue') {
121  $resultRedirect->setPath('*/*/edit', ['block_id' => $model->getId()]);
122  } else if ($redirect === 'close') {
123  $resultRedirect->setPath('*/*/');
124  } else if ($redirect === 'duplicate') {
125  $duplicateModel = $this->blockFactory->create(['data' => $data]);
126  $duplicateModel->setId(null);
127  $duplicateModel->setIdentifier($data['identifier'] . '-' . uniqid());
128  $duplicateModel->setIsActive(Block::STATUS_DISABLED);
129  $this->blockRepository->save($duplicateModel);
130  $id = $duplicateModel->getId();
131  $this->messageManager->addSuccessMessage(__('You duplicated the block.'));
132  $this->dataPersistor->set('cms_block', $data);
133  $resultRedirect->setPath('*/*/edit', ['block_id' => $id]);
134  }
135  return $resultRedirect;
136  }
137 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
__construct(Context $context, Registry $coreRegistry, DataPersistorInterface $dataPersistor, BlockFactory $blockFactory=null, BlockRepositoryInterface $blockRepository=null)
Definition: Save.php:42