Skip to content

Commit 13ada4f

Browse files
committed
Codereview suggestions, static test fixes
1 parent 2f23b48 commit 13ada4f

File tree

7 files changed

+157
-87
lines changed

7 files changed

+157
-87
lines changed

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

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,77 +8,67 @@
88

99
namespace Magento\Catalog\Controller\Adminhtml\Product;
1010

11-
use Magento\Framework\Controller\ResultInterface;
12-
use Magento\Backend\App\Action\Context;
13-
use Magento\Catalog\Api\ProductRepositoryInterface;
14-
use Magento\Framework\Controller\Result\JsonFactory;
15-
use Magento\Framework\App\Action\HttpGetActionInterface;
16-
use Magento\Backend\App\Action;
11+
use Magento\Catalog\Api\Data\ProductInterface;
1712

18-
/**
19-
* Returns selected product by product id. for ui-select filter
20-
*/
21-
class GetSelected extends Action implements HttpGetActionInterface
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
2215
{
2316
/**
17+
* Authorization level of a basic admin session
18+
*
2419
* @see _isAllowed()
2520
*/
2621
const ADMIN_RESOURCE = 'Magento_Catalog::products';
2722

2823
/**
29-
* @var JsonFactory
24+
* @var \Magento\Framework\Controller\Result\JsonFactory
3025
*/
3126
private $resultJsonFactory;
3227

3328
/**
34-
* @var ProductRepositoryInterface
29+
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
3530
*/
36-
private $productRepository;
31+
private $productCollectionFactory;
3732

3833
/**
39-
* GetSelected constructor.
40-
*
41-
* @param JsonFactory $jsonFactory
42-
* @param ProductRepositoryInterface $productRepository
43-
* @param Context $context
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
4438
*/
4539
public function __construct(
46-
JsonFactory $jsonFactory,
47-
ProductRepositoryInterface $productRepository,
48-
Context $context
40+
\Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
41+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
42+
\Magento\Backend\App\Action\Context $context
4943
) {
5044
$this->resultJsonFactory = $jsonFactory;
51-
$this->productRepository = $productRepository;
45+
$this->productCollectionFactory = $productCollectionFactory;
5246
parent::__construct($context);
5347
}
5448

5549
/**
56-
*
57-
* @return ResultInterface
50+
* @return \Magento\Framework\Controller\ResultInterface
5851
*/
59-
public function execute() : ResultInterface
52+
public function execute() : \Magento\Framework\Controller\ResultInterface
6053
{
61-
$productIds = $this->getRequest()->getParam('ids');
62-
$options = [];
63-
64-
65-
if (!is_array($productIds)) {
66-
return $this->resultJsonFactory->create()->setData('parameter ids must be type of array');
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+
];
6769
}
68-
foreach ($productIds as $id) {
69-
try {
70-
$product = $this->productRepository->getById($id);
71-
$options[] = [
72-
'value' => $product->getId(),
73-
'label' => $product->getName(),
74-
'is_active' => $product->getSatus(),
75-
'path' => $product->getSku()
76-
];
77-
} catch (\Exception $e) {
78-
continue;
79-
}
80-
}
81-
82-
return $this->resultJsonFactory->create()->setData($options);
70+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
71+
$resultJson = $this->resultJsonFactory->create();
72+
return $resultJson->setData($option);
8373
}
8474
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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\MediaGalleryCatalogUi\Controller\Adminhtml\Product;
10+
11+
use Magento\Framework\Controller\ResultInterface;
12+
use Magento\Backend\App\Action\Context;
13+
use Magento\Catalog\Api\ProductRepositoryInterface;
14+
use Magento\Framework\Controller\Result\JsonFactory;
15+
use Magento\Framework\App\Action\HttpGetActionInterface;
16+
use Magento\Backend\App\Action;
17+
18+
/**
19+
* Returns selected product by product id. for ui-select filter
20+
*/
21+
class GetSelected extends Action implements HttpGetActionInterface
22+
{
23+
/**
24+
* @see _isAllowed()
25+
*/
26+
const ADMIN_RESOURCE = 'Magento_Catalog::products';
27+
28+
/**
29+
* @var JsonFactory
30+
*/
31+
private $resultJsonFactory;
32+
33+
/**
34+
* @var ProductRepositoryInterface
35+
*/
36+
private $productRepository;
37+
38+
/**
39+
* GetSelected constructor.
40+
*
41+
* @param JsonFactory $jsonFactory
42+
* @param ProductRepositoryInterface $productRepository
43+
* @param Context $context
44+
*/
45+
public function __construct(
46+
JsonFactory $jsonFactory,
47+
ProductRepositoryInterface $productRepository,
48+
Context $context
49+
) {
50+
$this->resultJsonFactory = $jsonFactory;
51+
$this->productRepository = $productRepository;
52+
parent::__construct($context);
53+
}
54+
55+
/**
56+
*
57+
* @return ResultInterface
58+
*/
59+
public function execute() : ResultInterface
60+
{
61+
$productIds = $this->getRequest()->getParam('ids');
62+
$options = [];
63+
64+
65+
if (!is_array($productIds)) {
66+
return $this->resultJsonFactory->create()->setData('parameter ids must be type of array');
67+
}
68+
foreach ($productIds as $id) {
69+
try {
70+
$product = $this->productRepository->getById($id);
71+
$options[] = [
72+
'value' => $product->getId(),
73+
'label' => $product->getName(),
74+
'is_active' => $product->getSatus(),
75+
'path' => $product->getSku()
76+
];
77+
} catch (\Exception $e) {
78+
continue;
79+
}
80+
}
81+
82+
return $this->resultJsonFactory->create()->setData($options);
83+
}
84+
}

app/code/Magento/MediaGalleryCatalogUi/view/adminhtml/ui_component/media_gallery_listing.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<item name="filterRateLimitMethod" xsi:type="string">notifyWhenChangesStop</item>
3030
<item name="levelsVisibility" xsi:type="number">1</item>
3131
<item name="searchUrl" xsi:type="url" path="catalog/product/search"/>
32-
<item name="validationUrl" xsi:type="url" path="catalog/product/getSelected"/>
32+
<item name="validationUrl" xsi:type="url" path="media_gallery_catalog/product/getSelected"/>
3333
</item>
3434
</argument>
3535
<settings>

app/code/Magento/MediaGalleryCatalogUi/view/adminhtml/ui_component/standalone_media_gallery_listing.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<item name="filterRateLimitMethod" xsi:type="string">notifyWhenChangesStop</item>
3030
<item name="levelsVisibility" xsi:type="number">1</item>
3131
<item name="searchUrl" xsi:type="url" path="catalog/product/search"/>
32-
<item name="validationUrl" xsi:type="url" path="catalog/product/getSelected"/>
32+
<item name="validationUrl" xsi:type="url" path="media_gallery_catalog/product/getSelected"/>
3333
</item>
3434
</argument>
3535
<settings>

app/code/Magento/MediaGalleryCmsUi/view/adminhtml/ui_component/media_gallery_listing.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
name="page_id"
1414
provider="${ $.parentName }"
1515
sortOrder="120"
16-
class="Magento\MediaGalleryCmsUi\Ui\Component\Listing\Filters\UsedInPages"
1716
component="Magento_Ui/js/grid/filters/elements/ui-select"
1817
template="ui/grid/filters/elements/ui-select">
1918
<argument name="data" xsi:type="array">
@@ -39,7 +38,6 @@
3938
name="block_id"
4039
provider="${ $.parentName }"
4140
sortOrder="130"
42-
class="Magento\MediaGalleryCmsUi\Ui\Component\Listing\Filters\UsedInBlocks"
4341
component="Magento_Ui/js/grid/filters/elements/ui-select"
4442
template="ui/grid/filters/elements/ui-select">
4543
<argument name="data" xsi:type="array">

app/code/Magento/MediaGalleryUi/Ui/Component/Listing/Filters/Asset.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Magento\MediaContentApi\Api\GetContentByAssetIdsInterface;
1616
use Magento\Ui\Component\Filters\FilterModifier;
1717
use Magento\Ui\Component\Filters\Type\Select;
18-
use Magento\MediaGalleryApi\Api\GetAssetsByIdsInterface;
1918

2019
/**
2120
* Asset filter
@@ -27,11 +26,6 @@ class Asset extends Select
2726
*/
2827
private $getContentIdentities;
2928

30-
/**
31-
* @var GetAssetsByIdsInterface
32-
*/
33-
private $getAssetsByIds;
34-
3529
/**
3630
* Constructor
3731
*
@@ -40,6 +34,7 @@ class Asset extends Select
4034
* @param FilterBuilder $filterBuilder
4135
* @param FilterModifier $filterModifier
4236
* @param OptionSourceInterface $optionsProvider
37+
* @param GetContentByAssetIdsInterface $getContentIdentities
4338
* @param array $components
4439
* @param array $data
4540
* @SuppressWarnings(PHPMD.ExcessiveParameterList)

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

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,41 +42,44 @@ define([
4242
* Validate initial value actually exists
4343
*/
4444
validateInitialValue: function () {
45-
if (!_.isEmpty(this.value())) {
46-
$.ajax({
47-
url: this.validationUrl,
48-
type: 'GET',
49-
dataType: 'json',
50-
context: this,
51-
data: {
52-
ids: this.value()
53-
},
45+
if (_.isEmpty(this.value())) {
46+
this.validationLoading(false);
5447

55-
/** @param {Object} response */
56-
success: function (response) {
57-
if (!_.isEmpty(response)) {
58-
this.options([]);
59-
this.success({
60-
options: response
61-
});
62-
}
63-
this.filterChips().updateActive();
64-
},
48+
return;
49+
}
6550

66-
/** set empty array if error occurs */
67-
error: function () {
68-
this.options([]);
69-
},
51+
$.ajax({
52+
url: this.validationUrl,
53+
type: 'GET',
54+
dataType: 'json',
55+
context: this,
56+
data: {
57+
ids: this.value()
58+
},
7059

71-
/** stop loader */
72-
complete: function () {
73-
this.validationLoading(false);
74-
this.setCaption();
60+
/** @param {Object} response */
61+
success: function (response) {
62+
if (!_.isEmpty(response)) {
63+
this.options([]);
64+
this.success({
65+
options: response
66+
});
7567
}
76-
});
77-
} else {
78-
this.validationLoading(false);
79-
}
68+
this.filterChips().updateActive();
69+
},
70+
71+
/** set empty array if error occurs */
72+
error: function () {
73+
this.options([]);
74+
},
75+
76+
/** stop loader */
77+
complete: function () {
78+
this.validationLoading(false);
79+
this.setCaption();
80+
}
81+
});
82+
8083
}
8184
});
8285
});

0 commit comments

Comments
 (0)