Skip to content

Commit

Permalink
Move breadcrumb json configuration to viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
diedburn authored and gelanivishal committed Jun 30, 2018
1 parent f947cad commit cc5c33b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
27 changes: 26 additions & 1 deletion app/code/Magento/Catalog/ViewModel/Product/Breadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Catalog\Helper\Data;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\View\Element\Block\ArgumentInterface;

/**
Expand All @@ -29,18 +30,26 @@ class Breadcrumbs extends DataObject implements ArgumentInterface
*/
private $scopeConfig;

/**
* @var Json
*/
private $json;

/**
* @param Data $catalogData
* @param ScopeConfigInterface $scopeConfig
* @param Json $json
*/
public function __construct(
Data $catalogData,
ScopeConfigInterface $scopeConfig
ScopeConfigInterface $scopeConfig,
Json $json
) {
parent::__construct();

$this->catalogData = $catalogData;
$this->scopeConfig = $scopeConfig;
$this->json = $json;
}

/**
Expand Down Expand Up @@ -80,4 +89,20 @@ public function getProductName(): string
? $this->catalogData->getProduct()->getName()
: '';
}

/**
* Returns breadcrumb json.
*
* @return string
*/
public function getJsonConfiguration()
{
return $this->json->serialize([
'breadcrumbs' => [
'categoryUrlSuffix' => $this->getCategoryUrlSuffix(),
'userCategoryPathInUrl' => (int)$this->isCategoryUsedInProductUrl(),
'product' => $this->getProductName()
]
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,4 @@
/** @var \Magento\Catalog\ViewModel\Product\Breadcrumbs $viewModel */
$viewModel = $block->getData('viewModel');
?>
<div class="breadcrumbs" data-mage-init='{
"breadcrumbs": {
"categoryUrlSuffix": "<?= $block->escapeHtml($viewModel->getCategoryUrlSuffix()); ?>",
"useCategoryPathInUrl": <?= (int)$viewModel->isCategoryUsedInProductUrl(); ?>,
"product": "<?= $block->escapeHtml($block->escapeJs($viewModel->getProductName())); ?>"
}
}'>
</div>
<div class="breadcrumbs" data-mage-init='<?= $viewModel->getJsonConfiguration() ?>'></div>

0 comments on commit cc5c33b

Please sign in to comment.