Skip to content

Commit cc5c33b

Browse files
diedburngelanivishal
authored andcommitted
Move breadcrumb json configuration to viewmodel
1 parent f947cad commit cc5c33b

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
@@ -10,6 +10,7 @@
1010
use Magento\Catalog\Helper\Data;
1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212
use Magento\Framework\DataObject;
13+
use Magento\Framework\Serialize\Serializer\Json;
1314
use Magento\Framework\View\Element\Block\ArgumentInterface;
1415

1516
/**
@@ -29,18 +30,26 @@ class Breadcrumbs extends DataObject implements ArgumentInterface
2930
*/
3031
private $scopeConfig;
3132

33+
/**
34+
* @var Json
35+
*/
36+
private $json;
37+
3238
/**
3339
* @param Data $catalogData
3440
* @param ScopeConfigInterface $scopeConfig
41+
* @param Json $json
3542
*/
3643
public function __construct(
3744
Data $catalogData,
38-
ScopeConfigInterface $scopeConfig
45+
ScopeConfigInterface $scopeConfig,
46+
Json $json
3947
) {
4048
parent::__construct();
4149

4250
$this->catalogData = $catalogData;
4351
$this->scopeConfig = $scopeConfig;
52+
$this->json = $json;
4453
}
4554

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

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)