Skip to content

Commit 213e11d

Browse files
committed
Move breadcrumb json configuration to viewmodel
1 parent 4405582 commit 213e11d

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Catalog\Helper\Data;
99
use Magento\Framework\App\Config\ScopeConfigInterface;
1010
use Magento\Framework\DataObject;
11+
use Magento\Framework\Serialize\Serializer\Json;
1112
use Magento\Framework\View\Element\Block\ArgumentInterface;
1213

1314
/**
@@ -27,18 +28,26 @@ class Breadcrumbs extends DataObject implements ArgumentInterface
2728
*/
2829
private $scopeConfig;
2930

31+
/**
32+
* @var Json
33+
*/
34+
private $json;
35+
3036
/**
3137
* @param Data $catalogData
3238
* @param ScopeConfigInterface $scopeConfig
39+
* @param Json $json
3340
*/
3441
public function __construct(
3542
Data $catalogData,
36-
ScopeConfigInterface $scopeConfig
43+
ScopeConfigInterface $scopeConfig,
44+
Json $json
3745
) {
3846
parent::__construct();
3947

4048
$this->catalogData = $catalogData;
4149
$this->scopeConfig = $scopeConfig;
50+
$this->json = $json;
4251
}
4352

4453
/**
@@ -78,4 +87,20 @@ public function getProductName()
7887
? $this->catalogData->getProduct()->getName()
7988
: '';
8089
}
90+
91+
/**
92+
* Returns breadcrumb json.
93+
*
94+
* @return string
95+
*/
96+
public function getJsonConfiguration()
97+
{
98+
return $this->json->serialize([
99+
'breadcrumbs' => [
100+
'categoryUrlSuffix' => $this->getCategoryUrlSuffix(),
101+
'userCategoryPathInUrl' => (int)$this->isCategoryUsedInProductUrl(),
102+
'product' => $this->getProductName()
103+
]
104+
]);
105+
}
81106
}

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='<?= $viewModel->getJsonConfiguration() ?>'></div>

0 commit comments

Comments
 (0)