Breadcrumbs.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Block\Html;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. use Magento\Framework\View\Element\Template;
  9. /**
  10. * Html page breadcrumbs block
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Breadcrumbs extends \Magento\Framework\View\Element\Template
  16. {
  17. /**
  18. * Current template name
  19. *
  20. * @var string
  21. */
  22. protected $_template = 'Magento_Theme::html/breadcrumbs.phtml';
  23. /**
  24. * List of available breadcrumb properties
  25. *
  26. * @var string[]
  27. */
  28. protected $_properties = ['label', 'title', 'link', 'first', 'last', 'readonly'];
  29. /**
  30. * List of breadcrumbs
  31. *
  32. * @var array
  33. */
  34. protected $_crumbs;
  35. /**
  36. * Cache key info
  37. *
  38. * @var null|array
  39. */
  40. protected $_cacheKeyInfo;
  41. /**
  42. * @var Json
  43. */
  44. private $serializer;
  45. /**
  46. * @param Template\Context $context
  47. * @param array $data
  48. * @param Json|null $serializer
  49. */
  50. public function __construct(
  51. Template\Context $context,
  52. array $data = [],
  53. Json $serializer = null
  54. ) {
  55. parent::__construct($context, $data);
  56. $this->serializer =
  57. $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
  58. }
  59. /**
  60. * Add crumb
  61. *
  62. * @param string $crumbName
  63. * @param array $crumbInfo
  64. * @return $this
  65. */
  66. public function addCrumb($crumbName, $crumbInfo)
  67. {
  68. foreach ($this->_properties as $key) {
  69. if (!isset($crumbInfo[$key])) {
  70. $crumbInfo[$key] = null;
  71. }
  72. }
  73. if (!isset($this->_crumbs[$crumbName]) || !$this->_crumbs[$crumbName]['readonly']) {
  74. $this->_crumbs[$crumbName] = $crumbInfo;
  75. }
  76. return $this;
  77. }
  78. /**
  79. * Get cache key informative items
  80. *
  81. * Provide string array key to share specific info item with FPC placeholder
  82. *
  83. * @return array
  84. */
  85. public function getCacheKeyInfo()
  86. {
  87. if ($this->_cacheKeyInfo === null) {
  88. $this->_cacheKeyInfo = parent::getCacheKeyInfo() + [
  89. 'crumbs' => base64_encode($this->serializer->serialize($this->_crumbs)),
  90. 'name' => $this->getNameInLayout()
  91. ];
  92. }
  93. return $this->_cacheKeyInfo;
  94. }
  95. /**
  96. * Render block HTML
  97. *
  98. * @return string
  99. */
  100. protected function _toHtml()
  101. {
  102. if (is_array($this->_crumbs)) {
  103. reset($this->_crumbs);
  104. $this->_crumbs[key($this->_crumbs)]['first'] = true;
  105. end($this->_crumbs);
  106. $this->_crumbs[key($this->_crumbs)]['last'] = true;
  107. }
  108. $this->assign('crumbs', $this->_crumbs);
  109. return parent::_toHtml();
  110. }
  111. }