Skip to content

Commit 847caf1

Browse files
ENGCOM-1703: Move breadcrumb json configuration to viewmodel #15521
2 parents d4caf2f + 40e2672 commit 847caf1

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
use Magento\Catalog\Helper\Data;
99
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\DataObject;
12+
use Magento\Framework\Escaper;
13+
use Magento\Framework\Serialize\Serializer\Json;
1114
use Magento\Framework\View\Element\Block\ArgumentInterface;
1215

1316
/**
@@ -27,18 +30,33 @@ class Breadcrumbs extends DataObject implements ArgumentInterface
2730
*/
2831
private $scopeConfig;
2932

33+
/**
34+
* @var Json
35+
*/
36+
private $json;
37+
/**
38+
* @var Escaper
39+
*/
40+
private $escaper;
41+
3042
/**
3143
* @param Data $catalogData
3244
* @param ScopeConfigInterface $scopeConfig
45+
* @param Json $json
46+
* @param Escaper $escaper
3347
*/
3448
public function __construct(
3549
Data $catalogData,
36-
ScopeConfigInterface $scopeConfig
50+
ScopeConfigInterface $scopeConfig,
51+
Json $json = null,
52+
Escaper $escaper = null
3753
) {
3854
parent::__construct();
3955

4056
$this->catalogData = $catalogData;
4157
$this->scopeConfig = $scopeConfig;
58+
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
59+
$this->escaper = $escaper ?: ObjectManager::getInstance()->get(Escaper::class);
4260
}
4361

4462
/**
@@ -78,4 +96,20 @@ public function getProductName()
7896
? $this->catalogData->getProduct()->getName()
7997
: '';
8098
}
99+
100+
/**
101+
* Returns breadcrumb json.
102+
*
103+
* @return string
104+
*/
105+
public function getJsonConfiguration()
106+
{
107+
return $this->escaper->escapeHtml($this->json->serialize([
108+
'breadcrumbs' => [
109+
'categoryUrlSuffix' => $this->escaper->escapeHtml($this->getCategoryUrlSuffix()),
110+
'userCategoryPathInUrl' => (int)$this->isCategoryUsedInProductUrl(),
111+
'product' => $this->getProductName()
112+
]
113+
]));
114+
}
81115
}

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)