Skip to content

Commit 62dff6b

Browse files
committed
ACP2E-2392: [On-PREM] Dynamic block issue
- ui components can force a specific render type instead of relying on context
1 parent 3247e3c commit 62dff6b

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

app/code/Magento/Catalog/Block/Widget/RecentlyViewed.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
*/
1414
class RecentlyViewed extends Wrapper implements \Magento\Widget\Block\BlockInterface
1515
{
16+
protected const RENDER_TYPE = 'html';
1617
}

app/code/Magento/Ui/Block/Wrapper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
class Wrapper extends \Magento\Framework\View\Element\Template
1616
{
17+
protected const RENDER_TYPE = '';
18+
1719
/**
1820
* @var UiComponentGenerator
1921
*/
@@ -87,10 +89,10 @@ private function addDataToChildComponents(UiComponentInterface $uiComponent, arr
8789
public function renderApp($data = [])
8890
{
8991
/** @var \Magento\Ui\Component\AbstractComponent $uiComponent */
90-
$uiComponent = $this->uiComponentGenerator
92+
$uiComponent = $this->uiComponentGenerator //vezi generateXML aici de la layout
9193
->generateUiComponent($this->getData('uiComponent'), $this->getLayout());
9294
$this->injectDataInDataSource($uiComponent, $this->getData());
9395
$this->addDataToChildComponents($uiComponent, $data);
94-
return (string) $uiComponent->render();
96+
return (string) $uiComponent->render(static::RENDER_TYPE);
9597
}
9698
}

app/code/Magento/Ui/Component/Listing.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Ui\Component;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeFactory;
10+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
811
use Magento\Ui\Component\Listing\Columns;
912

1013
/**
@@ -20,6 +23,27 @@ class Listing extends AbstractComponent
2023
*/
2124
protected $columns = [];
2225

26+
/**
27+
* @var ContentTypeFactory
28+
*/
29+
protected ContentTypeFactory $contentTypeFactory;
30+
31+
/**
32+
* @param ContextInterface $context
33+
* @param ContentTypeFactory|null $contentTypeFactory
34+
* @param array $components
35+
* @param array $data
36+
*/
37+
public function __construct(
38+
ContextInterface $context,
39+
?ContentTypeFactory $contentTypeFactory = null,
40+
array $components = [],
41+
array $data = []
42+
) {
43+
$this->contentTypeFactory = $contentTypeFactory ?: ObjectManager::getInstance()->get(ContentTypeFactory::class);
44+
parent::__construct($context, $components, $data);
45+
}
46+
2347
/**
2448
* Get component name
2549
*
@@ -37,4 +61,16 @@ public function getDataSourceData()
3761
{
3862
return ['data' => $this->getContext()->getDataProvider()->getData()];
3963
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function render(string $contentType = '')
69+
{
70+
if ($contentType) {
71+
return $this->contentTypeFactory->get($contentType)->render($this, $this->getTemplate());
72+
}
73+
74+
return parent::render();
75+
}
4076
}

app/code/Magento/Ui/Test/Unit/Component/ListingTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
namespace Magento\Ui\Test\Unit\Component;
99

1010
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Framework\View\Element\UiComponent\ContentType\AbstractContentType;
12+
use Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeFactory;
13+
use Magento\Framework\View\Element\UiComponent\ContentType\Html;
1114
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1215
use Magento\Framework\View\Element\UiComponent\Processor;
1316
use Magento\Ui\Component\Listing;
@@ -26,6 +29,11 @@ class ListingTest extends TestCase
2629
*/
2730
protected $objectManager;
2831

32+
/**
33+
* @var ContentTypeFactory|MockObject
34+
*/
35+
private ContentTypeFactory $contentTypeFactory;
36+
2937
/**
3038
* @inheritdoc
3139
*/
@@ -39,6 +47,8 @@ protected function setUp(): void
3947
'',
4048
false
4149
);
50+
51+
$this->contentTypeFactory = $this->createMock(ContentTypeFactory::class);
4252
}
4353

4454
/**
@@ -103,4 +113,48 @@ public function testPrepare(): void
103113

104114
$listing->prepare();
105115
}
116+
117+
/**
118+
* @return void
119+
*/
120+
public function testRenderSpecificContentType(): void
121+
{
122+
$html = 'html output';
123+
$renderer = $this->createMock(Html::class);
124+
$renderer->expects($this->once())->method('render')->willReturn($html);
125+
$this->contentTypeFactory->expects($this->once())
126+
->method('get')
127+
->with('html')
128+
->willReturn($renderer);
129+
130+
/** @var Listing $listing */
131+
$listing = $this->objectManager->getObject(
132+
Listing::class,
133+
[
134+
'context' => $this->contextMock,
135+
'contentTypeFactory' => $this->contentTypeFactory
136+
]
137+
);
138+
$this->assertSame($html, $listing->render('html'));
139+
}
140+
141+
/**
142+
* @return void
143+
*/
144+
public function testRenderParent()
145+
{
146+
$html = 'html output';
147+
$renderer = $this->createMock(AbstractContentType::class);
148+
$renderer->expects($this->once())->method('render')->willReturn($html);
149+
$this->contextMock->expects($this->once())->method('getRenderEngine')->willReturn($renderer);
150+
151+
/** @var Listing $listing */
152+
$listing = $this->objectManager->getObject(
153+
Listing::class,
154+
[
155+
'context' => $this->contextMock
156+
]
157+
);
158+
$this->assertSame($html, $listing->render());
159+
}
106160
}

0 commit comments

Comments
 (0)