Skip to content

Commit 3445c47

Browse files
committed
Create add/edit product tag form block
1 parent e0e0b1d commit 3445c47

File tree

11 files changed

+604
-284
lines changed

11 files changed

+604
-284
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Lof\ProductTags\Block\Adminhtml\Tags\Edit;
7+
8+
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
9+
10+
/**
11+
* Class BackButton
12+
*/
13+
class BackButton extends GenericButton implements ButtonProviderInterface
14+
{
15+
/**
16+
* @return array
17+
*/
18+
public function getButtonData()
19+
{
20+
return [
21+
'label' => __('Back'),
22+
'on_click' => sprintf("location.href = '%s';", $this->getBackUrl()),
23+
'class' => 'back',
24+
'sort_order' => 10
25+
];
26+
}
27+
28+
/**
29+
* Get URL for back (reset) button
30+
*
31+
* @return string
32+
*/
33+
public function getBackUrl()
34+
{
35+
return $this->getUrl('*/*/');
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Lof\ProductTags\Block\Adminhtml\Tags\Edit;
7+
8+
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
9+
10+
/**
11+
* Class DeleteButton
12+
*/
13+
class DeleteButton extends GenericButton implements ButtonProviderInterface
14+
{
15+
/**
16+
* @inheritDoc
17+
*/
18+
public function getButtonData()
19+
{
20+
$data = [];
21+
if ($this->getBlockId()) {
22+
$data = [
23+
'label' => __('Delete Tag'),
24+
'class' => 'delete',
25+
'on_click' => 'deleteConfirm(\'' . __(
26+
'Are you sure you want to do this?'
27+
) . '\', \'' . $this->getDeleteUrl() . '\', {"data": {}})',
28+
'sort_order' => 20,
29+
];
30+
}
31+
return $data;
32+
}
33+
34+
/**
35+
* URL to send delete requests to.
36+
*
37+
* @return string
38+
*/
39+
public function getDeleteUrl()
40+
{
41+
return $this->getUrl('*/*/delete', ['tag_id' => $this->getBlockId()]);
42+
}
43+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Lof\ProductTags\Block\Adminhtml\Tags\Edit;
7+
8+
use Magento\Backend\Block\Widget\Context;
9+
use Magento\Cms\Api\BlockRepositoryInterface;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
12+
/**
13+
* Class GenericButton
14+
*/
15+
class GenericButton
16+
{
17+
/**
18+
* @var Context
19+
*/
20+
protected $context;
21+
22+
/**
23+
* @var BlockRepositoryInterface
24+
*/
25+
protected $blockRepository;
26+
27+
/**
28+
* @param Context $context
29+
* @param BlockRepositoryInterface $blockRepository
30+
*/
31+
public function __construct(
32+
Context $context,
33+
BlockRepositoryInterface $blockRepository
34+
) {
35+
$this->context = $context;
36+
$this->blockRepository = $blockRepository;
37+
}
38+
39+
/**
40+
* Return CMS block ID
41+
*
42+
* @return int|null
43+
*/
44+
public function getBlockId()
45+
{
46+
try {
47+
return $this->blockRepository->getById(
48+
$this->context->getRequest()->getParam('tag_id')
49+
)->getId();
50+
} catch (NoSuchEntityException $e) {
51+
}
52+
return null;
53+
}
54+
55+
/**
56+
* Generate url by route and parameters
57+
*
58+
* @param string $route
59+
* @param array $params
60+
* @return string
61+
*/
62+
public function getUrl($route = '', $params = [])
63+
{
64+
return $this->context->getUrlBuilder()->getUrl($route, $params);
65+
}
66+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Lof\ProductTags\Block\Adminhtml\Tags\Edit;
7+
8+
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
9+
use Magento\Ui\Component\Control\Container;
10+
11+
/**
12+
* Class SaveButton
13+
* @package Lof\ProductTags\Block\Adminhtml\Tags\Edit
14+
*/
15+
class SaveButton extends GenericButton implements ButtonProviderInterface
16+
{
17+
/**
18+
* @return array
19+
*/
20+
public function getButtonData()
21+
{
22+
return [
23+
'label' => __('Save'),
24+
'class' => 'save primary',
25+
'data_attribute' => [
26+
'mage-init' => [
27+
'buttonAdapter' => [
28+
'actions' => [
29+
[
30+
'targetName' => 'cms_block_form.cms_block_form',
31+
'actionName' => 'save',
32+
'params' => [
33+
true,
34+
[
35+
'back' => 'continue'
36+
]
37+
]
38+
]
39+
]
40+
]
41+
]
42+
],
43+
'class_name' => Container::SPLIT_BUTTON,
44+
'options' => $this->getOptions(),
45+
];
46+
}
47+
48+
/**
49+
* Retrieve options
50+
*
51+
* @return array
52+
*/
53+
private function getOptions()
54+
{
55+
$options = [
56+
[
57+
'label' => __('Save & Duplicate'),
58+
'id_hard' => 'save_and_duplicate',
59+
'data_attribute' => [
60+
'mage-init' => [
61+
'buttonAdapter' => [
62+
'actions' => [
63+
[
64+
'targetName' => 'cms_block_form.cms_block_form',
65+
'actionName' => 'save',
66+
'params' => [
67+
true,
68+
[
69+
'back' => 'duplicate'
70+
]
71+
]
72+
]
73+
]
74+
]
75+
]
76+
]
77+
],
78+
[
79+
'id_hard' => 'save_and_close',
80+
'label' => __('Save & Close'),
81+
'data_attribute' => [
82+
'mage-init' => [
83+
'buttonAdapter' => [
84+
'actions' => [
85+
[
86+
'targetName' => 'producttags_tag_form.producttags_tag_form',
87+
'actionName' => 'save',
88+
'params' => [
89+
true,
90+
[
91+
'back' => 'close'
92+
]
93+
]
94+
]
95+
]
96+
]
97+
]
98+
]
99+
]
100+
];
101+
102+
return $options;
103+
}
104+
}

Controller/Adminhtml/Tag/Save.php

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

0 commit comments

Comments
 (0)