Skip to content

Commit 44a769e

Browse files
committed
MAGETWO-88962: [UI Component] URL Input: Product Link
1 parent 69872ee commit 44a769e

File tree

9 files changed

+118
-67
lines changed

9 files changed

+118
-67
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Search.php

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,22 @@ class Search extends \Magento\Backend\App\Action
2828
private $resultJsonFactory;
2929

3030
/**
31-
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
31+
* @var \Magento\Catalog\Model\ProductLink\Search
3232
*/
33-
private $productCollectionFactory;
34-
35-
/**
36-
* @var \Magento\Ui\DataProvider\AddFilterToCollectionInterface[]
37-
*/
38-
private $filterStrategies;
39-
40-
/**
41-
* @var \Magento\Catalog\Model\Product\Visibility
42-
*/
43-
private $catalogVisibility;
33+
private $productSearch;
4434

4535
/**
4636
* @param \Magento\Framework\Controller\Result\JsonFactory $resultFactory
47-
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
48-
* @param \Magento\Catalog\Model\Product\Visibility $catalogVisibility
37+
* @param \Magento\Catalog\Model\ProductLink\Search $productSearch
4938
* @param \Magento\Backend\App\Action\Context $context
50-
* @param array $filterStrategies
5139
*/
5240
public function __construct(
5341
\Magento\Framework\Controller\Result\JsonFactory $resultFactory,
54-
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
55-
\Magento\Catalog\Model\Product\Visibility $catalogVisibility,
56-
\Magento\Backend\App\Action\Context $context,
57-
array $filterStrategies = []
42+
\Magento\Catalog\Model\ProductLink\Search $productSearch,
43+
\Magento\Backend\App\Action\Context $context
5844
) {
5945
$this->resultJsonFactory = $resultFactory;
60-
$this->productCollectionFactory = $productCollectionFactory;
61-
$this->filterStrategies = $filterStrategies;
62-
$this->catalogVisibility = $catalogVisibility;
46+
$this->productSearch = $productSearch;
6347
parent::__construct($context);
6448
}
6549

@@ -69,18 +53,12 @@ public function __construct(
6953
public function execute() : \Magento\Framework\Controller\ResultInterface
7054
{
7155
$searchKey = $this->getRequest()->getParam('searchKey');
72-
$pageNum = $this->getRequest()->getParam('page');
73-
$limit = $this->getRequest()->getParam('limit');
56+
$pageNum = (int)$this->getRequest()->getParam('page');
57+
$limit = (int)$this->getRequest()->getParam('limit');
7458

7559
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
76-
$productCollection = $this->productCollectionFactory->create();
77-
$productCollection->addAttributeToSelect(ProductInterface::NAME);
78-
$productCollection->setVisibility($this->catalogVisibility->getVisibleInCatalogIds());
79-
$productCollection->setPage($pageNum, $limit);
80-
$this->addFilter($productCollection, 'fulltext', $searchKey);
81-
$totalVaues = $productCollection->getSize();
82-
$productCollection->setPage($pageNum, $limit);
83-
60+
$productCollection = $this->productSearch->prepareCollection($searchKey, $pageNum, $limit);
61+
$totalValues = $productCollection->getSize();
8462
$productById = [];
8563
/** @var ProductInterface $product */
8664
foreach ($productCollection as $product) {
@@ -97,34 +75,7 @@ public function execute() : \Magento\Framework\Controller\ResultInterface
9775
$resultJson = $this->resultJsonFactory->create();
9876
return $resultJson->setData([
9977
'options' => $productById,
100-
'total' => empty($productById) ? 0 : $totalVaues
78+
'total' => empty($productById) ? 0 : $totalValues
10179
]);
10280
}
103-
104-
/**
105-
* Add filter to collection based on search data
106-
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
107-
* @param string $filterType
108-
* @param string $searchKey
109-
* @return void
110-
*/
111-
private function addFilter(
112-
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection,
113-
string $filterType,
114-
string $searchKey
115-
) : void {
116-
if (isset($this->filterStrategies[$filterType])) {
117-
$this->filterStrategies[$filterType]
118-
->addFilter(
119-
$collection,
120-
$filterType,
121-
[$filterType => $searchKey]
122-
);
123-
} else {
124-
$collection->addAttributeToSelect(
125-
[ProductInterface::NAME, ProductInterface::SKU],
126-
['like' => "%{$searchKey}%"]
127-
);
128-
}
129-
}
13081
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Model\ProductLink;
10+
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
13+
/** Returns collection of product visible in catalog by search key */
14+
class Search
15+
{
16+
/**
17+
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
18+
*/
19+
private $productCollectionFactory;
20+
21+
/**
22+
* @var \Magento\Ui\DataProvider\AddFilterToCollectionInterface[]
23+
*/
24+
private $filter;
25+
26+
/**
27+
* @var \Magento\Catalog\Model\Product\Visibility
28+
*/
29+
private $catalogVisibility;
30+
31+
/**
32+
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
33+
* @param \Magento\Catalog\Model\Product\Visibility $catalogVisibility
34+
* @param \Magento\Ui\DataProvider\AddFilterToCollectionInterface $filter
35+
*/
36+
public function __construct(
37+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
38+
\Magento\Catalog\Model\Product\Visibility $catalogVisibility,
39+
\Magento\Ui\DataProvider\AddFilterToCollectionInterface $filter
40+
) {
41+
$this->productCollectionFactory = $productCollectionFactory;
42+
$this->filter = $filter;
43+
$this->catalogVisibility = $catalogVisibility;
44+
}
45+
46+
/**
47+
* Add required filters and limitations for product collection
48+
*
49+
* @param string $searchKey
50+
* @param int $pageNum
51+
* @param int $limit
52+
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
53+
*/
54+
public function prepareCollection(string $searchKey, int $pageNum, int $limit
55+
): \Magento\Catalog\Model\ResourceModel\Product\Collection {
56+
$productCollection = $this->productCollectionFactory->create();
57+
$productCollection->addAttributeToSelect(ProductInterface::NAME);
58+
$productCollection->setVisibility($this->catalogVisibility->getVisibleInCatalogIds());
59+
$productCollection->setPage($pageNum, $limit);
60+
$this->filter->addFilter($productCollection, 'fulltext', ['fulltext' => $searchKey]);
61+
$productCollection->setPage($pageNum, $limit);
62+
return $productCollection;
63+
}
64+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Ui\DataProvider\Product;
10+
11+
use Magento\Ui\DataProvider\AddFilterToCollectionInterface;
12+
use Magento\Framework\Data\Collection;
13+
use Magento\Catalog\Api\Data\ProductInterface;
14+
15+
/**
16+
* Class AddSearchKeyConditionToCollection
17+
*/
18+
class AddSearchKeyConditionToCollection implements AddFilterToCollectionInterface
19+
{
20+
/**
21+
* {@inheritdoc}
22+
*
23+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
24+
*/
25+
public function addFilter(Collection $collection, $field, $condition = null) {
26+
if (isset($condition['fulltext']) && !empty($condition['fulltext'])) {
27+
$collection->addFieldToFilter(
28+
ProductInterface::NAME, $condition['fulltext']
29+
)->addFieldToFilter(
30+
ProductInterface::SKU, $condition['fulltext']
31+
);
32+
}
33+
}
34+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,9 @@
215215
</argument>
216216
</arguments>
217217
</type>
218+
<type name="Magento\Catalog\Model\ProductLink\Search">
219+
<arguments>
220+
<argument name="filter" xsi:type="object">Magento\Catalog\Ui\DataProvider\Product\AddSearchKeyConditionToCollection</argument>
221+
</arguments>
222+
</type>
218223
</config>

app/code/Magento/Catalog/view/adminhtml/web/js/components/product-ui-select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ define([
3030
* Validate initial value actually exists
3131
*/
3232
validateInitialValue: function () {
33-
if (this.value()) {
33+
if (!_.isEmpty(this.value())) {
3434
$.ajax({
3535
url: this.validationUrl,
3636
type: 'GET',

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@
2626
</argument>
2727
</arguments>
2828
</type>
29-
<type name="Magento\Catalog\Controller\Adminhtml\Product\Search">
29+
<type name="Magento\Catalog\Model\ProductLink\Search">
3030
<arguments>
31-
<argument name="filterStrategies" xsi:type="array">
32-
<item name="fulltext" xsi:type="object">Magento\CatalogSearch\Ui\DataProvider\Product\AddFulltextFilterToCollection</item>
33-
</argument>
31+
<argument name="filter" xsi:type="object">Magento\CatalogSearch\Ui\DataProvider\Product\AddFulltextFilterToCollection</argument>
3432
</arguments>
3533
</type>
3634
</config>

app/code/Magento/Ui/view/base/web/js/form/element/ui-select.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,6 @@ define([
11841184
* @return {Object}
11851185
*/
11861186
getCachedSearchResults: function (searchKey) {
1187-
11881187
if (this.cachedSearchResults.hasOwnProperty(searchKey)) {
11891188
return this.cachedSearchResults[searchKey];
11901189
}

app/code/Magento/Ui/view/base/web/templates/grid/filters/elements/ui-select.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
class="admin__action-multiselect-search-count">
140140
</div>
141141
</div>
142-
<div if="options().length == 0"
142+
<div ifnot="options().length"
143143
class="admin__action-multiselect-empty-area">
144144
<ul data-bind="html: emptyOptionsHtml"/>
145145
</div>

0 commit comments

Comments
 (0)