Skip to content

Commit bf7cea3

Browse files
ENGCOM-3611: Set view models as shareable by default #18541
- Merge Pull Request #18541 from thomas-kl1/magento2:patch/layout-block-argument-shareable - Merged commits: 1. 9a7f3e6 2. 415288f 3. 32f2813 4. 02d475c 5. 3959725
2 parents ff6d03c + 3959725 commit bf7cea3

File tree

4 files changed

+562
-23
lines changed

4 files changed

+562
-23
lines changed

app/code/Magento/Sales/Block/Adminhtml/Order/Create/Search/Grid.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
*/
66
namespace Magento\Sales\Block\Adminhtml\Order\Create\Search;
77

8+
use Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\DataProvider\ProductCollection
9+
as ProductCollectionDataProvider;
10+
use Magento\Framework\App\ObjectManager;
11+
812
/**
913
* Adminhtml sales order create search products block
1014
*
1115
* @api
1216
* @author Magento Core Team <core@magentocommerce.com>
1317
* @since 100.0.2
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1419
*/
1520
class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
1621
{
@@ -42,6 +47,11 @@ class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
4247
*/
4348
protected $_productFactory;
4449

50+
/**
51+
* @var ProductCollectionDataProvider $productCollectionProvider
52+
*/
53+
private $productCollectionProvider;
54+
4555
/**
4656
* @param \Magento\Backend\Block\Template\Context $context
4757
* @param \Magento\Backend\Helper\Data $backendHelper
@@ -50,6 +60,7 @@ class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
5060
* @param \Magento\Backend\Model\Session\Quote $sessionQuote
5161
* @param \Magento\Sales\Model\Config $salesConfig
5262
* @param array $data
63+
* @param ProductCollectionDataProvider|null $productCollectionProvider
5364
*/
5465
public function __construct(
5566
\Magento\Backend\Block\Template\Context $context,
@@ -58,12 +69,15 @@ public function __construct(
5869
\Magento\Catalog\Model\Config $catalogConfig,
5970
\Magento\Backend\Model\Session\Quote $sessionQuote,
6071
\Magento\Sales\Model\Config $salesConfig,
61-
array $data = []
72+
array $data = [],
73+
ProductCollectionDataProvider $productCollectionProvider = null
6274
) {
6375
$this->_productFactory = $productFactory;
6476
$this->_catalogConfig = $catalogConfig;
6577
$this->_sessionQuote = $sessionQuote;
6678
$this->_salesConfig = $salesConfig;
79+
$this->productCollectionProvider = $productCollectionProvider
80+
?: ObjectManager::getInstance()->get(ProductCollectionDataProvider::class);
6781
parent::__construct($context, $backendHelper, $data);
6882
}
6983

@@ -140,20 +154,18 @@ protected function _addColumnFilterToCollection($column)
140154
*/
141155
protected function _prepareCollection()
142156
{
157+
143158
$attributes = $this->_catalogConfig->getProductAttributes();
159+
$store = $this->getStore();
160+
144161
/* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
145-
$collection = $this->_productFactory->create()->getCollection();
146-
$collection->setStore(
147-
$this->getStore()
148-
)->addAttributeToSelect(
162+
$collection = $this->productCollectionProvider->getCollectionForStore($store);
163+
$collection->addAttributeToSelect(
149164
$attributes
150-
)->addAttributeToSelect(
151-
'sku'
152-
)->addStoreFilter()->addAttributeToFilter(
165+
);
166+
$collection->addAttributeToFilter(
153167
'type_id',
154168
$this->_salesConfig->getAvailableProductTypes()
155-
)->addAttributeToSelect(
156-
'gift_message_available'
157169
);
158170

159171
$this->setCollection($collection);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Sales\Block\Adminhtml\Order\Create\Search\Grid\DataProvider;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Store\Model\Store;
13+
14+
/**
15+
* Prepares product collection for the grid
16+
*/
17+
class ProductCollection
18+
{
19+
/**
20+
* @var ProductCollectionFactory
21+
*/
22+
private $collectionFactory;
23+
24+
/**
25+
* @param ProductCollectionFactory $collectionFactory
26+
*/
27+
public function __construct(
28+
ProductCollectionFactory $collectionFactory
29+
) {
30+
$this->collectionFactory = $collectionFactory;
31+
}
32+
33+
/**
34+
* Provide products collection filtered with store
35+
*
36+
* @param Store $store
37+
* @return Collection
38+
*/
39+
public function getCollectionForStore(Store $store):Collection
40+
{
41+
/** @var Collection $collection */
42+
$collection = $this->collectionFactory->create();
43+
44+
$collection->setStore($store);
45+
$collection->addAttributeToSelect(
46+
'gift_message_available'
47+
);
48+
$collection->addAttributeToSelect(
49+
'sku'
50+
);
51+
$collection->addStoreFilter();
52+
53+
return $collection;
54+
}
55+
}

lib/internal/Magento/Framework/View/Layout/etc/elements.xsd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
<xs:sequence>
1515
<xs:element name="updater" type="updaterType" minOccurs="0" maxOccurs="1"/>
1616
</xs:sequence>
17-
<xs:attribute name="shared" type="xs:boolean" default="true" use="optional"/>
17+
</xs:extension>
18+
</xs:complexContent>
19+
</xs:complexType>
20+
<xs:complexType name="object">
21+
<xs:complexContent>
22+
<xs:extension base="argumentType">
23+
<xs:attribute name="shared" use="optional" type="xs:boolean"/>
1824
</xs:extension>
1925
</xs:complexContent>
2026
</xs:complexType>

0 commit comments

Comments
 (0)