Skip to content

Commit 86fb80c

Browse files
#274 Admin Global Search: Search categories
1 parent d8ca81c commit 86fb80c

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Model\Search;
7+
/**
8+
* Search model for backend search
9+
*
10+
* @method Category setQuery(string $query)
11+
* @method string|null getQuery()
12+
* @method bool hasQuery()
13+
* @method Category setStart(int $startPosition)
14+
* @method int|null getStart()
15+
* @method bool hasStart()
16+
* @method Category setLimit(int $limit)
17+
* @method int|null getLimit()
18+
* @method bool hasLimit()
19+
* @method Category setResults(array $results)
20+
* @method array getResults()
21+
* @api
22+
*/
23+
class Category extends \Magento\Framework\DataObject
24+
{
25+
/**
26+
* Adminhtml data
27+
*
28+
* @var \Magento\Backend\Helper\Data
29+
*/
30+
protected $_adminhtmlData = null;
31+
32+
/**
33+
* @var \Magento\Catalog\Api\CategoryListInterface
34+
*/
35+
protected $categoryRepository;
36+
37+
/**
38+
* @var \Magento\Framework\Api\SearchCriteriaBuilder
39+
*/
40+
protected $searchCriteriaBuilder;
41+
42+
/**
43+
* @var \Magento\Framework\Api\FilterBuilder
44+
*/
45+
protected $filterBuilder;
46+
47+
/**
48+
* Magento string lib
49+
*
50+
* @var \Magento\Framework\Stdlib\StringUtils
51+
*/
52+
protected $string;
53+
54+
/**
55+
* Initialize dependencies.
56+
*
57+
* @param \Magento\Backend\Helper\Data $adminhtmlData
58+
* @param \Magento\Catalog\Api\CategoryListInterface $categoryRepository
59+
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
60+
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
61+
* @param \Magento\Framework\Stdlib\StringUtils $string
62+
*/
63+
public function __construct(
64+
\Magento\Backend\Helper\Data $adminhtmlData,
65+
\Magento\Catalog\Api\CategoryListInterface $categoryRepository,
66+
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
67+
\Magento\Framework\Api\FilterBuilder $filterBuilder,
68+
\Magento\Framework\Stdlib\StringUtils $string
69+
)
70+
{
71+
$this->_adminhtmlData = $adminhtmlData;
72+
$this->categoryRepository = $categoryRepository;
73+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
74+
$this->filterBuilder = $filterBuilder;
75+
$this->string = $string;
76+
}
77+
78+
/**
79+
* Load search results
80+
*
81+
* @return $this
82+
*/
83+
public function load()
84+
{
85+
$result = [];
86+
if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) {
87+
$this->setResults($result);
88+
return $this;
89+
}
90+
91+
$this->searchCriteriaBuilder->setCurrentPage($this->getStart());
92+
$this->searchCriteriaBuilder->setPageSize($this->getLimit());
93+
$searchFields = ['name'];
94+
95+
$filters = [];
96+
foreach ($searchFields as $field) {
97+
$filters[] = $this->filterBuilder
98+
->setField($field)
99+
->setConditionType('like')
100+
->setValue('%' . $this->getQuery() . '%')
101+
->create();
102+
}
103+
$this->searchCriteriaBuilder->addFilters($filters);
104+
105+
$searchCriteria = $this->searchCriteriaBuilder->create();
106+
$searchResults = $this->categoryRepository->getList($searchCriteria);
107+
108+
foreach ($searchResults->getItems() as $category) {
109+
$description = strip_tags($category->getDescription());
110+
$result[] = [
111+
'id' => 'category/1/' . $category->getId(),
112+
'type' => __('Category'),
113+
'name' => $category->getName(),
114+
'description' => $this->string->substr($description, 0, 30),
115+
'url' => $this->_adminhtmlData->getUrl('catalog/category/edit', ['id' => $category->getId()]),
116+
];
117+
}
118+
$this->setResults($result);
119+
return $this;
120+
}
121+
}

app/code/Magento/CatalogSearch/etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
<item name="class" xsi:type="string">Magento\CatalogSearch\Model\Search\Catalog</item>
6767
<item name="acl" xsi:type="string">Magento_Catalog::catalog</item>
6868
</item>
69+
<item name="categories" xsi:type="array">
70+
<item name="class" xsi:type="string">Magento\CatalogSearch\Model\Search\Category</item>
71+
<item name="acl" xsi:type="string">Magento_Catalog::categories</item>
72+
</item>
6973
</argument>
7074
</arguments>
7175
</type>

0 commit comments

Comments
 (0)