Skip to content

Commit c5035d4

Browse files
committed
magento#7698: Admin Global Search was build in a hurry
1 parent 5eba0f3 commit c5035d4

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

app/code/Magento/Backend/Block/GlobalSearch.php

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@
1616
*/
1717
class GlobalSearch extends \Magento\Backend\Block\Template
1818
{
19-
const ENTITY_TYPE_PRODUCTS = 'Products';
20-
const ENTITY_TYPE_ORDERS = 'Orders';
21-
const ENTITY_TYPE_CUSTOMERS = 'Customers';
22-
const ENTITY_TYPE_PAGES = 'Pages';
23-
24-
/**
25-
* Affiliation between entity types for global search and corresponding admin resources.
26-
*
27-
* @var array
28-
*/
29-
private $entityTypes = [
30-
self::ENTITY_TYPE_PRODUCTS => \Magento\Catalog\Controller\Adminhtml\Product::ADMIN_RESOURCE,
31-
self::ENTITY_TYPE_ORDERS => \Magento\Sales\Controller\Adminhtml\Order::ADMIN_RESOURCE,
32-
self::ENTITY_TYPE_CUSTOMERS => \Magento\Customer\Controller\Adminhtml\Index::ADMIN_RESOURCE,
33-
self::ENTITY_TYPE_PAGES => \Magento\Cms\Controller\Adminhtml\Page\Index::ADMIN_RESOURCE,
34-
];
35-
3619
/**
3720
* @var SearchEntityFactory
3821
*/
@@ -43,19 +26,34 @@ class GlobalSearch extends \Magento\Backend\Block\Template
4326
*/
4427
protected $_template = 'Magento_Backend::system/search.phtml';
4528

29+
/**
30+
* @var array
31+
*/
32+
private $entityResources;
33+
34+
/**
35+
* @var array
36+
*/
37+
private $entityPaths;
38+
4639
/**
4740
* @param Template\Context $context
4841
* @param array $data
42+
* @param array $entityResources
43+
* @param array $entityPaths
4944
* @param SearchEntityFactory|null $searchEntityFactory
5045
*/
5146
public function __construct(
5247
Template\Context $context,
5348
array $data = [],
49+
array $entityResources = [],
50+
array $entityPaths = [],
5451
SearchEntityFactory $searchEntityFactory = null
5552
) {
56-
$this->searchEntityFactory = $searchEntityFactory ?: ObjectManager::getInstance()->get(
57-
SearchEntityFactory::class
58-
);
53+
$this->entityResources = $entityResources;
54+
$this->entityPaths = $entityPaths;
55+
$this->searchEntityFactory = $searchEntityFactory ?: ObjectManager::getInstance()
56+
->get(SearchEntityFactory::class);
5957

6058
parent::__construct($context, $data);
6159
}
@@ -89,7 +87,7 @@ public function getEntitiesToShow()
8987
$allowedEntityTypes = [];
9088
$entitiesToShow = [];
9189

92-
foreach ($this->entityTypes as $entityType => $resource) {
90+
foreach ($this->entityResources as $entityType => $resource) {
9391
if ($this->getAuthorization()->isAllowed($resource)) {
9492
$allowedEntityTypes[] = $entityType;
9593
}
@@ -118,22 +116,7 @@ public function getEntitiesToShow()
118116
*/
119117
private function getUrlEntityType(string $entityType)
120118
{
121-
$urlPath = '';
122-
123-
switch ($entityType) {
124-
case self::ENTITY_TYPE_PRODUCTS:
125-
$urlPath = 'catalog/product/index/';
126-
break;
127-
case self::ENTITY_TYPE_ORDERS:
128-
$urlPath = 'sales/order/index/';
129-
break;
130-
case self::ENTITY_TYPE_CUSTOMERS:
131-
$urlPath = 'customer/index/index/';
132-
break;
133-
case self::ENTITY_TYPE_PAGES:
134-
$urlPath = 'cms/page/index/';
135-
break;
136-
}
119+
$urlPath = $this->entityPaths[$entityType] ?? '';
137120

138121
return $this->getUrl($urlPath);
139122
}

app/code/Magento/Backend/Test/Unit/Block/GlobalSearchTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ class GlobalSearchTest extends \PHPUnit\Framework\TestCase
3535
*/
3636
private $searchEntityFactory;
3737

38+
/**
39+
* @var array
40+
*/
41+
private $entityResources = [
42+
'Products' => \Magento\Catalog\Controller\Adminhtml\Product::ADMIN_RESOURCE,
43+
'Orders' => \Magento\Sales\Controller\Adminhtml\Order::ADMIN_RESOURCE,
44+
'Customers' => \Magento\Customer\Controller\Adminhtml\Index::ADMIN_RESOURCE,
45+
'Pages' => \Magento\Cms\Controller\Adminhtml\Page\Index::ADMIN_RESOURCE,
46+
];
47+
48+
/**
49+
* @var array
50+
*/
51+
private $entityPaths = [
52+
'Products' => 'catalog/product/index/',
53+
'Orders' => 'sales/order/index/',
54+
'Customers' => 'customer/index/index',
55+
'Pages' => 'cms/page/index/',
56+
];
57+
3858
protected function setUp()
3959
{
4060
$objectManager = new ObjectManager($this);
@@ -53,6 +73,8 @@ protected function setUp()
5373
[
5474
'context' => $context,
5575
'searchEntityFactory' => $this->searchEntityFactory,
76+
'entityResources' => $this->entityResources,
77+
'entityPaths' => $this->entityPaths,
5678
]
5779
);
5880
}

app/code/Magento/Backend/etc/adminhtml/di.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,20 @@
155155
<argument name="defaultClass" xsi:type="string">Magento\Backend\Block\Template</argument>
156156
</arguments>
157157
</type>
158+
<type name="Magento\Backend\Block\GlobalSearch">
159+
<arguments>
160+
<argument name="entityResources" xsi:type="array">
161+
<item name="Products" xsi:type="string">Magento_Catalog::products</item>
162+
<item name="Orders" xsi:type="string">Magento_Sales::sales_order</item>
163+
<item name="Customers" xsi:type="string">Magento_Customer::manage</item>
164+
<item name="Pages" xsi:type="string">Magento_Cms::page</item>
165+
</argument>
166+
<argument name="entityPaths" xsi:type="array">
167+
<item name="Products" xsi:type="string">catalog/product/index/</item>
168+
<item name="Orders" xsi:type="string">sales/order/index/</item>
169+
<item name="Customers" xsi:type="string">customer/index/index</item>
170+
<item name="Pages" xsi:type="string">cms/page/index/</item>
171+
</argument>
172+
</arguments>
173+
</type>
158174
</config>

0 commit comments

Comments
 (0)