Skip to content

Commit d0cdd2b

Browse files
committed
fix(adminhtml): fetch current category in category edit page
1 parent 5ad5942 commit d0cdd2b

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

Block/Adminhtml/Category/Merchandising.php

Lines changed: 11 additions & 7 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\Registry\CurrentCategory;
9+
use Algolia\AlgoliaSearch\Service\GetCurrentCategoryService;
1010

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

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

5660
/** @return bool */

Service/GetCurrentCategoryService.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Service;
4+
5+
use Magento\Catalog\Api\CategoryRepositoryInterface;
6+
use Magento\Catalog\Api\Data\CategoryInterface;
7+
use Magento\Framework\Exception\NoSuchEntityException;
8+
9+
class GetCurrentCategoryService
10+
{
11+
/**
12+
* Current Category
13+
*
14+
* @var CategoryInterface
15+
*/
16+
private $currentCategory;
17+
18+
/**
19+
* @var CategoryRepositoryInterface
20+
*/
21+
private $categoryRepository;
22+
23+
/**
24+
* @param CategoryRepositoryInterface $categoryRepository
25+
*/
26+
public function __construct(
27+
CategoryRepositoryInterface $categoryRepository
28+
) {
29+
$this->categoryRepository = $categoryRepository;
30+
}
31+
32+
/**
33+
* @param int $categoryId
34+
* @return CategoryInterface|null
35+
*/
36+
public function getCategory(int $categoryId): ?CategoryInterface
37+
{
38+
if (!$this->currentCategory) {
39+
try {
40+
$this->currentCategory = $this->categoryRepository->get($categoryId);
41+
} catch (NoSuchEntityException $e) {
42+
return null;
43+
}
44+
}
45+
return $this->currentCategory;
46+
}
47+
}

etc/adminhtml/events.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,4 @@
6565
<event name="sales_order_shipment_save_after">
6666
<observer name="algoliasearch_reindex_product_on_last_item_purchase" instance="Algolia\AlgoliaSearch\Observer\ReindexProductOnLastItemPurchase"/>
6767
</event>
68-
<event name="catalog_controller_category_init_after">
69-
<observer name="algoliasearch_current_category" instance="Algolia\AlgoliaSearch\Observer\RegisterCurrentCategoryObserver"/>
70-
</event>
7168
</config>

0 commit comments

Comments
 (0)