Skip to content

Commit c7a6a0d

Browse files
author
Eric Bohanon
committed
Merge remote-tracking branch 'remotes/origin/MAGETWO-89292-SDL' into MAGETWO-89292-SDL
2 parents c2e42f4 + 523c44f commit c7a6a0d

File tree

40 files changed

+1596
-76
lines changed

40 files changed

+1596
-76
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\Controller\Adminhtml\Product;
10+
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
13+
/** Returns information about selected product by product id. Returns empty array if product don't exist */
14+
class GetSelected extends \Magento\Backend\App\Action
15+
{
16+
/**
17+
* Authorization level of a basic admin session
18+
*
19+
* @see _isAllowed()
20+
*/
21+
const ADMIN_RESOURCE = 'Magento_Catalog::products';
22+
23+
/**
24+
* @var \Magento\Framework\Controller\Result\JsonFactory
25+
*/
26+
private $resultJsonFactory;
27+
28+
/**
29+
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
30+
*/
31+
private $productCollectionFactory;
32+
33+
/**
34+
* Search constructor.
35+
* @param \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
36+
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
37+
* @param \Magento\Backend\App\Action\Context $context
38+
*/
39+
public function __construct(
40+
\Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
41+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
42+
\Magento\Backend\App\Action\Context $context
43+
) {
44+
$this->resultJsonFactory = $jsonFactory;
45+
$this->productCollectionFactory = $productCollectionFactory;
46+
parent::__construct($context);
47+
}
48+
49+
/**
50+
* @return \Magento\Framework\Controller\ResultInterface
51+
*/
52+
public function execute() : \Magento\Framework\Controller\ResultInterface
53+
{
54+
$productId = $this->getRequest()->getParam('productId');
55+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
56+
$productCollection = $this->productCollectionFactory->create();
57+
$productCollection->addAttributeToSelect(ProductInterface::NAME);
58+
$productCollection->addIdFilter($productId);
59+
$option = [];
60+
/** @var ProductInterface $product */
61+
if (!empty($productCollection->getFirstItem()->getData())) {
62+
$product = $productCollection->getFirstItem();
63+
$option = [
64+
'value' => $productId,
65+
'label' => $product->getName(),
66+
'is_active' => $product->getStatus(),
67+
'path' => $product->getSku(),
68+
];
69+
}
70+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
71+
$resultJson = $this->resultJsonFactory->create();
72+
return $resultJson->setData($option);
73+
}
74+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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\Controller\Adminhtml\Product;
10+
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
13+
/**
14+
* Controller to search product for ui-select component
15+
*/
16+
class Search extends \Magento\Backend\App\Action
17+
{
18+
/**
19+
* Authorization level of a basic admin session
20+
*
21+
* @see _isAllowed()
22+
*/
23+
const ADMIN_RESOURCE = 'Magento_Catalog::products';
24+
25+
/**
26+
* @var \Magento\Framework\Controller\Result\JsonFactory
27+
*/
28+
private $resultJsonFactory;
29+
30+
/**
31+
* @var \Magento\Catalog\Model\ProductLink\Search
32+
*/
33+
private $productSearch;
34+
35+
/**
36+
* @param \Magento\Framework\Controller\Result\JsonFactory $resultFactory
37+
* @param \Magento\Catalog\Model\ProductLink\Search $productSearch
38+
* @param \Magento\Backend\App\Action\Context $context
39+
*/
40+
public function __construct(
41+
\Magento\Framework\Controller\Result\JsonFactory $resultFactory,
42+
\Magento\Catalog\Model\ProductLink\Search $productSearch,
43+
\Magento\Backend\App\Action\Context $context
44+
) {
45+
$this->resultJsonFactory = $resultFactory;
46+
$this->productSearch = $productSearch;
47+
parent::__construct($context);
48+
}
49+
50+
/**
51+
* @return \Magento\Framework\Controller\ResultInterface
52+
*/
53+
public function execute() : \Magento\Framework\Controller\ResultInterface
54+
{
55+
$searchKey = $this->getRequest()->getParam('searchKey');
56+
$pageNum = (int)$this->getRequest()->getParam('page');
57+
$limit = (int)$this->getRequest()->getParam('limit');
58+
59+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
60+
$productCollection = $this->productSearch->prepareCollection($searchKey, $pageNum, $limit);
61+
$totalValues = $productCollection->getSize();
62+
$productById = [];
63+
/** @var ProductInterface $product */
64+
foreach ($productCollection as $product) {
65+
$productId = $product->getId();
66+
$productById[$productId] = [
67+
'value' => $productId,
68+
'label' => $product->getName(),
69+
'is_active' => $product->getStatus(),
70+
'path' => $product->getSku(),
71+
'optgroup' => false
72+
];
73+
}
74+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
75+
$resultJson = $this->resultJsonFactory->create();
76+
return $resultJson->setData([
77+
'options' => $productById,
78+
'total' => empty($productById) ? 0 : $totalValues
79+
]);
80+
}
81+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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(
55+
string $searchKey,
56+
int $pageNum,
57+
int $limit
58+
): \Magento\Catalog\Model\ResourceModel\Product\Collection {
59+
$productCollection = $this->productCollectionFactory->create();
60+
$productCollection->addAttributeToSelect(ProductInterface::NAME);
61+
$productCollection->setVisibility($this->catalogVisibility->getVisibleInCatalogIds());
62+
$productCollection->setPage($pageNum, $limit);
63+
$this->filter->addFilter($productCollection, 'fulltext', ['fulltext' => $searchKey]);
64+
$productCollection->setPage($pageNum, $limit);
65+
return $productCollection;
66+
}
67+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Ui\Component\UrlInput;
9+
10+
use Magento\Ui\Model\UrlInput\ConfigInterface;
11+
12+
/**
13+
* Returns configuration for category Url Input type
14+
*/
15+
class Category implements ConfigInterface
16+
{
17+
/**
18+
* @var \Magento\Catalog\Ui\Component\Product\Form\Categories\Options
19+
*/
20+
private $options;
21+
22+
/**
23+
* @param \Magento\Catalog\Ui\Component\Product\Form\Categories\Options $options
24+
*/
25+
public function __construct(\Magento\Catalog\Ui\Component\Product\Form\Categories\Options $options)
26+
{
27+
$this->options = $options;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function getConfig(): array
34+
{
35+
return [
36+
'label' => __('Category'),
37+
'component' => 'Magento_Ui/js/form/element/ui-select',
38+
'template' => 'ui/grid/filters/elements/ui-select',
39+
'formElement' => 'select',
40+
'disableLabel' => true,
41+
'multiple' => false,
42+
'chipsEnabled' => false,
43+
'filterOptions' => true,
44+
'levelsVisibility' => '1',
45+
'options' => $this->options->toOptionArray(),
46+
'sortOrder' => 30,
47+
'missingValuePlaceholder' => __('Category with ID: %s doesn\'t exist'),
48+
'isDisplayMissingValuePlaceholder' => true,
49+
];
50+
}
51+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Component\UrlInput;
10+
11+
use Magento\Framework\UrlInterface;
12+
13+
class Product implements \Magento\Ui\Model\UrlInput\ConfigInterface
14+
{
15+
/**
16+
* @var \Magento\Framework\UrlInterface
17+
*/
18+
private $urlBuilder;
19+
20+
/**
21+
* Product constructor.
22+
* @param UrlInterface $urlBuilder
23+
*/
24+
public function __construct(UrlInterface $urlBuilder)
25+
{
26+
$this->urlBuilder = $urlBuilder;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function getConfig(): array
33+
{
34+
return [
35+
'label' => __('Product'),
36+
'component' => 'Magento_Catalog/js/components/product-ui-select',
37+
'disableLabel' => true,
38+
'filterOptions' => true,
39+
'searchOptions' => true,
40+
'chipsEnabled' => true,
41+
'levelsVisibility' => '1',
42+
'options' => [],
43+
'sortOrder' => 25,
44+
'multiple' => false,
45+
'closeBtn' => true,
46+
'template' => 'ui/grid/filters/elements/ui-select',
47+
'searchUrl' => $this->urlBuilder->getUrl('catalog/product/search'),
48+
'filterPlaceholder' => __('Product Name or SKU'),
49+
'isDisplayEmptyPlaceholder' => true,
50+
'emptyOptionsHtml' => __('Start typing to find products'),
51+
'missingValuePlaceholder' => __('Product with ID: %s doesn\'t exist'),
52+
'isDisplayMissingValuePlaceholder' => true,
53+
'validationUrl' => $this->urlBuilder->getUrl('catalog/product/getSelected'),
54+
];
55+
}
56+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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) : void
26+
{
27+
if (isset($condition['fulltext']) && !empty($condition['fulltext'])) {
28+
$collection->addFieldToFilter(
29+
ProductInterface::NAME,
30+
$condition['fulltext']
31+
)->addFieldToFilter(
32+
ProductInterface::SKU,
33+
$condition['fulltext']
34+
);
35+
}
36+
}
37+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,17 @@
207207
<argument name="path" xsi:type="string">web/default_layouts/default_category_layout</argument>
208208
</arguments>
209209
</virtualType>
210+
<type name="Magento\Ui\Model\UrlInput\LinksConfigProvider">
211+
<arguments>
212+
<argument name="linksConfiguration" xsi:type="array">
213+
<item name="product" xsi:type="string">Magento\Catalog\Ui\Component\UrlInput\Product</item>
214+
<item name="category" xsi:type="string">Magento\Catalog\Ui\Component\UrlInput\Category</item>
215+
</argument>
216+
</arguments>
217+
</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>
210223
</config>

app/code/Magento/Catalog/i18n/en_US.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,3 +797,7 @@ Details,Details
797797
"Recently Viewed","Recently Viewed"
798798
"The value of Admin must be unique.", "The value of Admin must be unique."
799799
"The value of Admin must be unique. (%1)", "The value of Admin must be unique. (%1)"
800+
"Product Name or SKU", "Product Name or SKU"
801+
"Start typing to find products", "Start typing to find products"
802+
"Product with ID: (%1) doesn't exist", "Product with ID: (%1) doesn't exist"
803+
"Category with ID: (%1) doesn't exist", "Category with ID: (%1) doesn't exist"

0 commit comments

Comments
 (0)