Skip to content

Commit fb0363e

Browse files
committed
update file
1 parent 5ed1f3e commit fb0363e

File tree

7 files changed

+25
-70
lines changed

7 files changed

+25
-70
lines changed

Controller/Adminhtml/Tag.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
* See COPYING.txt for license details.
55
*/
66
declare(strict_types=1);
7-
87
namespace Lof\ProductTags\Controller\Adminhtml;
9-
108
use Magento\Store\Model\Store;
11-
129
/**
1310
* Catalog Tag controller
1411
*/
@@ -20,12 +17,10 @@ abstract class Tag extends \Magento\Backend\App\Action
2017
* @see _isAllowed()
2118
*/
2219
const ADMIN_RESOURCE = 'Lof_ProductTags::Tag';
23-
2420
/**
2521
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
2622
*/
2723
protected $dateFilter;
28-
2924
/**
3025
* @param \Magento\Backend\App\Action\Context $context
3126
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date|null $dateFilter
@@ -37,43 +32,36 @@ public function __construct(
3732
$this->dateFilter = $dateFilter;
3833
parent::__construct($context);
3934
}
40-
4135
/**
4236
* Initialize requested category and put it into registry.
4337
* Root category can be returned, if inappropriate store/category is specified
4438
*
45-
* @param bool $getRootInstead
4639
* @return \Lof\ProductTags\Model\Tag|false
4740
*/
48-
protected function _initCategory($getRootInstead = false)
41+
protected function _initTag()
4942
{
5043
$tagId = $this->resolveTagId();
5144
$storeId = $this->resolveStoreId();
5245
$tag = $this->_objectManager->create(\Lof\ProductTags\Model\Tag::class);
53-
5446
if ($tagId) {
5547
$tag->load($tagId);
5648
}
57-
5849
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('tag', $tag);
5950
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('current_tag', $tag);
6051
$this->_objectManager->get(\Magento\Cms\Model\Wysiwyg\Config::class)
6152
->setStoreId($storeId);
62-
return $category;
53+
return $tag;
6354
}
64-
6555
/**
6656
* Resolve Category Id (from get or from post)
6757
*
6858
* @return int
6959
*/
7060
private function resolveTagId() : int
7161
{
72-
$tagId = (int)$this->getRequest()->getParam('id', false);
73-
62+
$tagId = (int)$this->getRequest()->getParam('tag_id', false);
7463
return $tagId ?: (int)$this->getRequest()->getParam('entity_id', false);
7564
}
76-
7765
/**
7866
* Resolve store id
7967
*
@@ -85,8 +73,6 @@ private function resolveTagId() : int
8573
private function resolveStoreId() : int
8674
{
8775
$storeId = (int)$this->getRequest()->getParam('store', false);
88-
8976
return $storeId ?: (int)$this->getRequest()->getParam('store_id', Store::DEFAULT_STORE_ID);
9077
}
91-
92-
}
78+
}

Controller/Adminhtml/Tag/Edit.php

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,29 @@
44
* See COPYING.txt for license details.
55
*/
66
namespace Lof\ProductTags\Controller\Adminhtml\Tag;
7-
87
use Magento\Framework\App\Action\HttpGetActionInterface;
98
use Magento\Backend\App\Action;
10-
119
/**
1210
* Edit CMS page action.
1311
*/
14-
class Edit extends \Magento\Backend\App\Action implements HttpGetActionInterface
12+
class Edit extends \Lof\ProductTags\Controller\Adminhtml\Tag implements HttpGetActionInterface
1513
{
1614
/**
1715
* Authorization level of a basic admin session
1816
*
1917
* @see _isAllowed()
2018
*/
2119
const ADMIN_RESOURCE = 'Lof_ProductTags::Tag_edit';
22-
2320
/**
2421
* Core registry
2522
*
2623
* @var \Magento\Framework\Registry
2724
*/
2825
protected $_coreRegistry;
29-
3026
/**
3127
* @var \Magento\Framework\View\Result\PageFactory
3228
*/
3329
protected $resultPageFactory;
34-
3530
/**
3631
* @param Action\Context $context
3732
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
@@ -46,7 +41,6 @@ public function __construct(
4641
$this->_coreRegistry = $registry;
4742
parent::__construct($context);
4843
}
49-
5044
/**
5145
* Init actions
5246
*
@@ -62,7 +56,6 @@ protected function _initAction()
6256
->addBreadcrumb(__('Manage Tags'), __('Manage Tags'));
6357
return $resultPage;
6458
}
65-
6659
/**
6760
* Edit CMS page
6861
*
@@ -72,22 +65,9 @@ protected function _initAction()
7265
public function execute()
7366
{
7467
// 1. Get ID and create model
75-
$id = $this->getRequest()->getParam('tag_id');
76-
$model = $this->_objectManager->create(\Lof\ProductTags\Model\Tag::class);
77-
68+
$tag = $this->_initTag();
69+
$id = $tag->getId();
7870
// 2. Initial checking
79-
if ($id) {
80-
$model->load($id);
81-
if (!$model->getId()) {
82-
$this->messageManager->addErrorMessage(__('This tag no longer exists.'));
83-
/** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
84-
$resultRedirect = $this->resultRedirectFactory->create();
85-
return $resultRedirect->setPath('*/*/');
86-
}
87-
}
88-
89-
$this->_coreRegistry->register('product_tag', $model);
90-
9171
// 5. Build edit form
9272
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
9373
$resultPage = $this->_initAction();
@@ -97,8 +77,7 @@ public function execute()
9777
);
9878
$resultPage->getConfig()->getTitle()->prepend(__('Tags'));
9979
$resultPage->getConfig()->getTitle()
100-
->prepend($model->getId() ? $model->getTitle() : __('New Tag'));
101-
80+
->prepend($tag->getId() ? $tag->getTagTitle() : __('New Tag'));
10281
return $resultPage;
10382
}
104-
}
83+
}

Controller/Adminhtml/Tag/Grid.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55
* See COPYING.txt for license details.
66
*/
77
namespace Lof\ProductTags\Controller\Adminhtml\Tag;
8-
98
class Grid extends \Lof\ProductTags\Controller\Adminhtml\Tag
109
{
1110
/**
1211
* @var \Magento\Framework\Controller\Result\RawFactory
1312
*/
1413
protected $resultRawFactory;
15-
1614
/**
1715
* @var \Magento\Framework\View\LayoutFactory
1816
*/
1917
protected $layoutFactory;
20-
2118
/**
2219
* @param \Magento\Backend\App\Action\Context $context
2320
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
@@ -32,7 +29,6 @@ public function __construct(
3229
$this->resultRawFactory = $resultRawFactory;
3330
$this->layoutFactory = $layoutFactory;
3431
}
35-
3632
/**
3733
* Grid Action
3834
* Display list of products related to current category
@@ -41,7 +37,7 @@ public function __construct(
4137
*/
4238
public function execute()
4339
{
44-
$tag = $this->_initTag(true);
40+
$tag = $this->_initTag();
4541
if (!$tag) {
4642
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
4743
$resultRedirect = $this->resultRedirectFactory->create();
@@ -56,4 +52,4 @@ public function execute()
5652
)->toHtml()
5753
);
5854
}
59-
}
55+
}

Controller/Adminhtml/Tag/Save.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* See COPYING.txt for license details.
55
*/
66
namespace Lof\ProductTags\Controller\Adminhtml\Tag;
7-
87
use Magento\Framework\App\Action\HttpPostActionInterface;
98
use Magento\Backend\App\Action\Context;
109
use Lof\ProductTags\Api\TagRepositoryInterface;
@@ -13,7 +12,6 @@
1312
use Magento\Framework\Exception\LocalizedException;
1413
use Magento\Framework\Registry;
1514
use Magento\Framework\Stdlib\DateTime\Filter\Date;
16-
1715
/**
1816
* Save Lof Tag action.
1917
*/
@@ -48,20 +46,28 @@ public function execute()
4846
if (empty($data['tag_id'])) {
4947
$data['tag_id'] = null;
5048
}
51-
5249
/** @var \Lof\ProductTags\Model\Tag $model */
5350
$model = $this->TagFactory->create();
5451
$id = $this->getRequest()->getParam('tag_id');
5552
if ($id) {
5653
try {
57-
$model = $this->tagRepository->getById($id);
54+
$model = $model->load($id);
5855
} catch (LocalizedException $e) {
5956
$this->messageManager->addErrorMessage(__('This tag no longer exists.'));
6057
return $resultRedirect->setPath('*/*/');
6158
}
6259
}
63-
6460
$model->setData($data);
61+
if (isset($data['tag_products'])
62+
&& is_string($data['tag_products'])) {
63+
$products = json_decode($data['tag_products'], true);
64+
$model->setPostedProducts($products);
65+
}
66+
$this->_eventManager->dispatch(
67+
'lof_producttags_prepare_save',
68+
['tag' => $model, 'request' => $this->getRequest()]
69+
);
70+
$products = $model->getPostedProducts();
6571
try{
6672
$model->save($model);
6773
$this->messageManager->addSuccessMessage(__('You saved the tag.'));
@@ -72,7 +78,6 @@ public function execute()
7278
} catch (\Exception $e) {
7379
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the tag.'));
7480
}
75-
7681
$this->dataPersistor->set('lof_productags_tag', $data);
7782
return $resultRedirect->setPath('*/*/edit', ['tag_id' => $id]);
7883
}
@@ -81,7 +86,6 @@ public function execute()
8186
private function processBlockReturn($model, $data, $resultRedirect)
8287
{
8388
$redirect = $data['back'] ?? 'close';
84-
8589
if ($redirect ==='continue') {
8690
$resultRedirect->setPath('*/*/edit', ['tag_id' => $model->getId()]);
8791
} else if ($redirect === 'close') {
@@ -99,4 +103,4 @@ private function processBlockReturn($model, $data, $resultRedirect)
99103
}
100104
return $resultRedirect;
101105
}
102-
}
106+
}

Model/Tag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function loadByIdentifier($tag_code){
100100
}
101101

102102
public function getRelatedReadonly(){
103-
return true;
103+
return false;
104104
}
105105

106106
/**

Model/Tag/Source/IsActive.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,11 @@ class IsActive implements OptionSourceInterface
1717
*/
1818
protected $producttags;
1919

20-
/**
21-
* Constructor
22-
*
23-
* @param \Magento\Cms\Model\Block $cmsBlock
24-
*/
2520
public function __construct(\Lof\ProductTags\Model\Tag $producttags)
2621
{
2722
$this->producttags = $producttags;
2823
}
2924

30-
/**
31-
* Get options
32-
*
33-
* @return array
34-
*/
3525
public function toOptionArray()
3626
{
3727
$availableOptions = $this->producttags->getAvailableStatuses();

view/adminhtml/templates/lof_producttags/tag/edit/assign_products.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ $gridJsObjectName = $blockGrid->getJsObjectName();
77
<script type="text/x-magento-init">
88
{
99
"*": {
10-
"Lof_ProductTags/tag/edit/assign-products": {
10+
"Lof_ProductTags/js/tag/edit/assign-products": {
1111
"selectedProducts": <?= /* @escapeNotVerified */ $block->getProductsJson() ?>,
1212
"gridJsObjectName": <?= /* @escapeNotVerified */ '"' . $gridJsObjectName . '"' ?: '{}' ?>
1313
}

0 commit comments

Comments
 (0)