Skip to content

Commit c9cb1b1

Browse files
ENGCOM-6325: add the possibility to add display mode dependant layout handles #25718
- Merge Pull Request #25718 from brosenberger/magento2:add-displaymode-pagelayouthandles - Merged commits: 1. 88da782 2. ae0ab69 3. 65f7610 4. 890206a 5. 4cd5cd2 6. f37eb05
2 parents dbb159a + f37eb05 commit c9cb1b1

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

app/code/Magento/Catalog/Controller/Category/View.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Catalog\Api\CategoryRepositoryInterface;
1010
use Magento\Catalog\Helper\Category as CategoryHelper;
1111
use Magento\Catalog\Model\Category;
12+
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
1213
use Magento\Catalog\Model\Design;
1314
use Magento\Catalog\Model\Layer\Resolver;
1415
use Magento\Catalog\Model\Product\ProductList\ToolbarMemorizer;
@@ -30,7 +31,6 @@
3031
use Magento\Framework\View\Result\PageFactory;
3132
use Magento\Store\Model\StoreManagerInterface;
3233
use Psr\Log\LoggerInterface;
33-
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
3434

3535
/**
3636
* View a category on storefront. Needs to be accessible by POST because of the store switching.
@@ -208,8 +208,10 @@ protected function _initCategory()
208208
* @return ResultInterface
209209
* @throws NoSuchEntityException
210210
*/
211-
public function execute()
211+
public function execute(): ?ResultInterface
212212
{
213+
$result = null;
214+
213215
if ($this->_request->getParam(ActionInterface::PARAM_NAME_URL_ENCODED)) {
214216
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
215217
}
@@ -239,6 +241,7 @@ public function execute()
239241
$page->addPageLayoutHandles(['type' => $parentPageType], null, false);
240242
}
241243
$page->addPageLayoutHandles(['type' => $pageType], null, false);
244+
$page->addPageLayoutHandles(['displaymode' => strtolower($category->getDisplayMode())], null, false);
242245
$page->addPageLayoutHandles(['id' => $category->getId()]);
243246

244247
// apply custom layout update once layout is loaded
@@ -250,8 +253,9 @@ public function execute()
250253

251254
return $page;
252255
} elseif (!$this->getResponse()->isRedirect()) {
253-
return $this->resultForwardFactory->create()->forward('noroute');
256+
$result = $this->resultForwardFactory->create()->forward('noroute');
254257
}
258+
return $result;
255259
}
256260

257261
/**

app/code/Magento/Catalog/Test/Unit/Controller/Category/ViewTest.php

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,13 @@ protected function setUp()
178178
);
179179
}
180180

181-
public function testApplyCustomLayoutUpdate()
181+
/**
182+
* Apply custom layout update is correct
183+
*
184+
* @dataProvider getInvocationData
185+
* @return void
186+
*/
187+
public function testApplyCustomLayoutUpdate(array $expectedData): void
182188
{
183189
$categoryId = 123;
184190
$pageLayout = 'page_layout';
@@ -199,11 +205,70 @@ public function testApplyCustomLayoutUpdate()
199205
\Magento\Framework\DataObject::class,
200206
['getPageLayout', 'getLayoutUpdates']
201207
);
208+
$this->category->expects($this->at(1))
209+
->method('hasChildren')
210+
->willReturn(true);
211+
$this->category->expects($this->at(2))
212+
->method('hasChildren')
213+
->willReturn($expectedData[1][0]['type'] === 'default' ? true : false);
214+
$this->category->expects($this->once())
215+
->method('getDisplayMode')
216+
->willReturn($expectedData[2][0]['displaymode']);
217+
$this->expectationForPageLayoutHandles($expectedData);
202218
$settings->expects($this->atLeastOnce())->method('getPageLayout')->will($this->returnValue($pageLayout));
203219
$settings->expects($this->once())->method('getLayoutUpdates')->willReturn(['update1', 'update2']);
204-
205220
$this->catalogDesign->expects($this->any())->method('getDesignSettings')->will($this->returnValue($settings));
206221

207222
$this->action->execute();
208223
}
224+
225+
/**
226+
* Expected invocation for Layout Handles
227+
*
228+
* @param array $data
229+
* @return void
230+
*/
231+
private function expectationForPageLayoutHandles($data): void
232+
{
233+
$index = 1;
234+
235+
foreach ($data as $expectedData) {
236+
$this->page->expects($this->at($index))
237+
->method('addPageLayoutHandles')
238+
->with($expectedData[0], $expectedData[1], $expectedData[2]);
239+
$index++;
240+
}
241+
}
242+
243+
/**
244+
* Data provider for execute method.
245+
*
246+
* @return array
247+
*/
248+
public function getInvocationData(): array
249+
{
250+
return [
251+
[
252+
'layoutHandles' => [
253+
[['type' => 'default'], null, false],
254+
[['type' => 'default_without_children'], null, false],
255+
[['displaymode' => 'products'], null, false]
256+
]
257+
],
258+
[
259+
'layoutHandles' => [
260+
[['type' => 'default'], null, false],
261+
[['type' => 'default_without_children'], null, false],
262+
[['displaymode' => 'page'], null, false]
263+
]
264+
],
265+
[
266+
'layoutHandles' => [
267+
[['type' => 'default'], null, false],
268+
[['type' => 'default'], null, false],
269+
[['displaymode' => 'poducts_and_page'], null, false]
270+
]
271+
]
272+
];
273+
}
209274
}

0 commit comments

Comments
 (0)