Skip to content

Commit 7fe1b9d

Browse files
committed
Fix filter placeholders for ui-select filter (frontend implementation)
1 parent 1ce8bcb commit 7fe1b9d

File tree

17 files changed

+333
-505
lines changed

17 files changed

+333
-505
lines changed

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

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

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

11-
use Magento\Catalog\Api\Data\ProductInterface;
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;
1217

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

2328
/**
24-
* @var \Magento\Framework\Controller\Result\JsonFactory
29+
* @var JsonFactory
2530
*/
2631
private $resultJsonFactory;
2732

2833
/**
29-
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
34+
* @var ProductRepositoryInterface
3035
*/
31-
private $productCollectionFactory;
36+
private $productRepository;
3237

3338
/**
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
39+
* GetSelected constructor.
40+
*
41+
* @param JsonFactory $jsonFactory
42+
* @param ProductRepositoryInterface $productRepository
43+
* @param Context $context
3844
*/
3945
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
46+
JsonFactory $jsonFactory,
47+
ProductRepositoryInterface $productRepository,
48+
Context $context
4349
) {
4450
$this->resultJsonFactory = $jsonFactory;
45-
$this->productCollectionFactory = $productCollectionFactory;
51+
$this->productRepository = $productRepository;
4652
parent::__construct($context);
4753
}
4854

4955
/**
50-
* @return \Magento\Framework\Controller\ResultInterface
56+
*
57+
* @return ResultInterface
5158
*/
52-
public function execute() : \Magento\Framework\Controller\ResultInterface
59+
public function execute() : ResultInterface
5360
{
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-
];
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');
6967
}
70-
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
71-
$resultJson = $this->resultJsonFactory->create();
72-
return $resultJson->setData($option);
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);
7383
}
7484
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* See COPYING.txt for license details.
44
*/
55

6+
/**
7+
* @deprecated see Magento/Ui/view/base/web/js/grid/filters/elements/ui-select.js
8+
*/
69
define([
710
'Magento_Ui/js/form/element/ui-select',
811
'jquery',

app/code/Magento/MediaGalleryCatalogUi/Ui/Component/Listing/Filters/UsedInProducts.php

Lines changed: 0 additions & 115 deletions
This file was deleted.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
provider="${ $.parentName }"
5959
sortOrder="10"
6060
class="Magento\MediaGalleryUi\Ui\Component\Listing\Filters\Asset"
61-
component="Magento_Ui/js/form/element/ui-select"
61+
component="Magento_Ui/js/grid/filters/elements/ui-select"
6262
template="Magento_MediaGalleryUi/grid/filters/elements/ui-select">
6363
<argument name="data" xsi:type="array">
6464
<item name="config" xsi:type="array">
@@ -74,6 +74,7 @@
7474
<item name="filterRateLimitMethod" xsi:type="string" translate="true">notifyWhenChangesStop</item>
7575
<item name="searchOptions" xsi:type="boolean">true</item>
7676
<item name="searchUrl" xsi:type="url" path="media_gallery/asset/search" />
77+
<item name="validationUrl" xsi:type="url" path="media_gallery/asset/getSelected"/>
7778
<item name="levelsVisibility" xsi:type="number">1</item>
7879
</item>
7980
</argument>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
name="product_id"
1414
provider="${ $.parentName }"
1515
sortOrder="110"
16-
class="Magento\MediaGalleryCatalogUi\Ui\Component\Listing\Filters\UsedInProducts"
17-
component="Magento_Catalog/js/components/product-ui-select"
16+
component="Magento_Ui/js/grid/filters/elements/ui-select"
1817
template="ui/grid/filters/elements/ui-select">
1918
<argument name="data" xsi:type="array">
2019
<item name="config" xsi:type="array">

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
name="product_id"
1414
provider="${ $.parentName }"
1515
sortOrder="110"
16-
class="Magento\MediaGalleryCatalogUi\Ui\Component\Listing\Filters\UsedInProducts"
17-
component="Magento_Catalog/js/components/product-ui-select"
16+
component="Magento_Ui/js/grid/filters/elements/ui-select"
1817
template="ui/grid/filters/elements/ui-select">
1918
<argument name="data" xsi:type="array">
2019
<item name="config" xsi:type="array">
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\MediaGalleryCmsUi\Controller\Adminhtml\Block;
9+
10+
use Magento\Backend\App\Action;
11+
use Magento\Backend\App\Action\Context;
12+
use Magento\Cms\Api\BlockRepositoryInterface;
13+
use Magento\Framework\App\Action\HttpGetActionInterface;
14+
use Magento\Framework\Controller\Result\JsonFactory;
15+
use Magento\Framework\Controller\ResultInterface;
16+
17+
/**
18+
* Controller to get selected block for ui-select component
19+
*/
20+
class GetSelected extends Action implements HttpGetActionInterface
21+
{
22+
/**
23+
* Authorization level of a basic admin session
24+
*
25+
* @see _isAllowed()
26+
*/
27+
const ADMIN_RESOURCE = 'Magento_Cms::block';
28+
29+
/**
30+
* @var JsonFactory
31+
*/
32+
private $resultJsonFactory;
33+
34+
/**
35+
* @var BlockRepositoryInterface
36+
*/
37+
private $blockRepository;
38+
39+
/**
40+
* @param JsonFactory $resultFactory
41+
* @param BlockRepositoryInterface $blockRepository
42+
* @param Context $context
43+
*/
44+
public function __construct(
45+
JsonFactory $resultFactory,
46+
BlockRepositoryInterface $blockRepository,
47+
Context $context
48+
) {
49+
$this->resultJsonFactory = $resultFactory;
50+
$this->blockRepository = $blockRepository;
51+
parent::__construct($context);
52+
}
53+
54+
/**
55+
* Execute pages search.
56+
*
57+
* @return ResultInterface
58+
*/
59+
public function execute(): ResultInterface
60+
{
61+
$options = [];
62+
$blockIds = $this->getRequest()->getParam('ids');
63+
64+
if (!is_array($blockIds)) {
65+
return $this->resultJsonFactory->create()->setData('parameter ids must be type of array');
66+
}
67+
foreach ($blockIds as $id) {
68+
try {
69+
$block = $this->blockRepository->getById($id);
70+
$options[] = [
71+
'value' => $block->getId(),
72+
'label' => $block->getTitle(),
73+
'is_active' => $block->isActive(),
74+
'optgroup' => false
75+
];
76+
} catch (\Exception $e) {
77+
continue;
78+
}
79+
}
80+
81+
return $this->resultJsonFactory->create()->setData($options);
82+
}
83+
}

0 commit comments

Comments
 (0)