Skip to content

Commit 4b32ad4

Browse files
committed
switch from service to plugin
1 parent df95cfe commit 4b32ad4

File tree

4 files changed

+64
-58
lines changed

4 files changed

+64
-58
lines changed

Block/Adminhtml/Category/Merchandising.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
use Algolia\AlgoliaSearch\Helper\Data;
77
use Magento\Backend\Block\Template\Context;
88
use Magento\Catalog\Model\Category;
9-
use Algolia\AlgoliaSearch\Service\GetCurrentCategoryService;
9+
use Algolia\AlgoliaSearch\Registry\CurrentCategory;
1010

1111
class Merchandising extends \Magento\Backend\Block\Template
1212
{
1313
/** @var string */
1414
protected $_template = 'catalog/category/edit/merchandising.phtml';
1515

16-
/** @var GetCurrentCategoryService */
17-
protected $getCurrentCategoryService;
16+
/** @var CurrentCategory */
17+
protected $currentCategory;
1818

1919
/** @var ConfigHelper */
2020
private $configHelper;
@@ -27,19 +27,19 @@ class Merchandising extends \Magento\Backend\Block\Template
2727

2828
/**
2929
* @param Context $context
30-
* @param GetCurrentCategoryService $getCurrentCategoryService
30+
* @param CurrentCategory $currentCategory
3131
* @param ConfigHelper $configHelper
3232
* @param Data $coreHelper
3333
* @param array $data
3434
*/
3535
public function __construct(
3636
Context $context,
37-
GetCurrentCategoryService $getCurrentCategoryService,
37+
CurrentCategory $currentCategory,
3838
ConfigHelper $configHelper,
3939
Data $coreHelper,
4040
array $data = []
4141
) {
42-
$this->getCurrentCategoryService = $getCurrentCategoryService;
42+
$this->currentCategory = $currentCategory;
4343
$this->configHelper = $configHelper;
4444
$this->coreHelper = $coreHelper;
4545
$this->storeManager = $context->getStoreManager();
@@ -50,11 +50,7 @@ public function __construct(
5050
/** @return Category | null */
5151
public function getCategory()
5252
{
53-
$categoryId = $this->getRequest()->getParam('id');
54-
if (!$categoryId) {
55-
return null;
56-
}
57-
return $this->getCurrentCategoryService->getCategory($categoryId);
53+
return $this->currentCategory->get();
5854
}
5955

6056
/** @return bool */

Plugin/SetAdminCurrentCategory.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Plugin;
4+
5+
use Algolia\AlgoliaSearch\Registry\CurrentCategory;
6+
use Magento\Catalog\Api\CategoryRepositoryInterface;
7+
use Magento\Catalog\Controller\Adminhtml\Category\Edit as EditController;
8+
use Magento\Framework\Exception\NoSuchEntityException;
9+
use Magento\Framework\View\Result\Page;
10+
11+
class SetAdminCurrentCategory
12+
{
13+
/** @var CurrentCategory */
14+
protected $currentCategory;
15+
16+
/**
17+
* @var CategoryRepositoryInterface
18+
*/
19+
private $categoryRepository;
20+
21+
/**
22+
* @param CurrentCategory $currentCategory
23+
* @param CategoryRepositoryInterface $categoryRepository
24+
*/
25+
public function __construct(
26+
CurrentCategory $currentCategory,
27+
CategoryRepositoryInterface $categoryRepository
28+
) {
29+
$this->currentCategory = $currentCategory;
30+
$this->categoryRepository = $categoryRepository;
31+
}
32+
33+
/**
34+
* Set the current category in adminhtml area in the Algolia registry without using Magento registry
35+
* (which is deprecated)
36+
*
37+
* @param EditController $subject
38+
* @param Page $result
39+
*
40+
* @return Page
41+
*/
42+
public function afterExecute(EditController $subject, $result)
43+
{
44+
$categoryId = $subject->getRequest()->getParam('id');
45+
try {
46+
$currentCategory = $this->categoryRepository->get($categoryId);
47+
$this->currentCategory->set($currentCategory);
48+
} catch (NoSuchEntityException $e) {
49+
return null;
50+
}
51+
52+
return $result;
53+
}
54+
}

Service/GetCurrentCategoryService.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

etc/adminhtml/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<type name="Magento\Catalog\Model\Category">
66
<plugin name="algolia_algoliasearch_plugin_category_url" type="Algolia\AlgoliaSearch\Plugin\CategoryUrlPlugin"/>
77
</type>
8+
<type name="Magento\Catalog\Controller\Adminhtml\Category\Edit">
9+
<plugin name="algolia_algoliasearch_plugin_set_admin_current_category" type="Algolia\AlgoliaSearch\Plugin\SetAdminCurrentCategory"/>
10+
</type>
811
<type name="Algolia\AlgoliaSearch\Block\Adminhtml\LandingPage\Renderer\UrlBuilder">
912
<arguments>
1013
<argument name="frontendUrlBuilder" xsi:type="object">Magento\Framework\Url</argument>

0 commit comments

Comments
 (0)