Skip to content

Commit 342f5cd

Browse files
author
Stanislav Idolov
authored
ENGCOM-2152: [Forwardport] Move breadcrumb json configuration to viewmodel #16466
2 parents 45078f2 + 0e693ce commit 342f5cd

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

app/code/Magento/Catalog/ViewModel/Product/Breadcrumbs.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
use Magento\Catalog\Helper\Data;
1111
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\DataObject;
14+
use Magento\Framework\Serialize\Serializer\Json;
1315
use Magento\Framework\View\Element\Block\ArgumentInterface;
1416

1517
/**
@@ -29,18 +31,26 @@ class Breadcrumbs extends DataObject implements ArgumentInterface
2931
*/
3032
private $scopeConfig;
3133

34+
/**
35+
* @var Json
36+
*/
37+
private $json;
38+
3239
/**
3340
* @param Data $catalogData
3441
* @param ScopeConfigInterface $scopeConfig
42+
* @param Json $json
3543
*/
3644
public function __construct(
3745
Data $catalogData,
38-
ScopeConfigInterface $scopeConfig
46+
ScopeConfigInterface $scopeConfig,
47+
Json $json = null
3948
) {
4049
parent::__construct();
4150

4251
$this->catalogData = $catalogData;
4352
$this->scopeConfig = $scopeConfig;
53+
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
4454
}
4555

4656
/**
@@ -80,4 +90,20 @@ public function getProductName(): string
8090
? $this->catalogData->getProduct()->getName()
8191
: '';
8292
}
93+
94+
/**
95+
* Returns breadcrumb json.
96+
*
97+
* @return string
98+
*/
99+
public function getJsonConfiguration()
100+
{
101+
return $this->json->serialize([
102+
'breadcrumbs' => [
103+
'categoryUrlSuffix' => $this->getCategoryUrlSuffix(),
104+
'userCategoryPathInUrl' => (int)$this->isCategoryUsedInProductUrl(),
105+
'product' => $this->getProductName()
106+
]
107+
]);
108+
}
83109
}

app/code/Magento/Catalog/view/frontend/templates/product/breadcrumbs.phtml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,4 @@
77
/** @var \Magento\Catalog\ViewModel\Product\Breadcrumbs $viewModel */
88
$viewModel = $block->getData('viewModel');
99
?>
10-
<div class="breadcrumbs" data-mage-init='{
11-
"breadcrumbs": {
12-
"categoryUrlSuffix": "<?= $block->escapeHtml($viewModel->getCategoryUrlSuffix()); ?>",
13-
"useCategoryPathInUrl": <?= (int)$viewModel->isCategoryUsedInProductUrl(); ?>,
14-
"product": "<?= $block->escapeHtml($block->escapeJs($viewModel->getProductName())); ?>"
15-
}
16-
}'>
17-
</div>
10+
<div class="breadcrumbs" data-mage-init='<?= /* @escapeNotVerified */ $viewModel->getJsonConfiguration() ?>'></div>

0 commit comments

Comments
 (0)