Link.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Widget to display catalog link
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Catalog\Block\Widget;
  12. use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
  13. use Magento\UrlRewrite\Model\UrlFinderInterface;
  14. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
  15. class Link extends \Magento\Framework\View\Element\Html\Link implements \Magento\Widget\Block\BlockInterface
  16. {
  17. /**
  18. * Entity model name which must be used to retrieve entity specific data.
  19. * @var null|\Magento\Catalog\Model\ResourceModel\AbstractResource
  20. */
  21. protected $_entityResource = null;
  22. /**
  23. * Prepared href attribute
  24. *
  25. * @var string
  26. */
  27. protected $_href;
  28. /**
  29. * Prepared anchor text
  30. *
  31. * @var string
  32. */
  33. protected $_anchorText;
  34. /**
  35. * Url finder for category
  36. *
  37. * @var UrlFinderInterface
  38. */
  39. protected $urlFinder;
  40. /**
  41. * @param \Magento\Framework\View\Element\Template\Context $context
  42. * @param UrlFinderInterface $urlFinder
  43. * @param \Magento\Catalog\Model\ResourceModel\AbstractResource $entityResource
  44. * @param array $data
  45. */
  46. public function __construct(
  47. \Magento\Framework\View\Element\Template\Context $context,
  48. UrlFinderInterface $urlFinder,
  49. \Magento\Catalog\Model\ResourceModel\AbstractResource $entityResource = null,
  50. array $data = []
  51. ) {
  52. parent::__construct($context, $data);
  53. $this->urlFinder = $urlFinder;
  54. $this->_entityResource = $entityResource;
  55. }
  56. /**
  57. * Prepare url using passed id path and return it
  58. * or return false if path was not found in url rewrites.
  59. *
  60. * @throws \RuntimeException
  61. * @return string|false
  62. * @SuppressWarnings(PHPMD.NPathComplexity)
  63. */
  64. public function getHref()
  65. {
  66. if ($this->_href === null) {
  67. if (!$this->getData('id_path')) {
  68. throw new \RuntimeException('Parameter id_path is not set.');
  69. }
  70. $rewriteData = $this->parseIdPath($this->getData('id_path'));
  71. $href = false;
  72. $store = $this->hasStoreId() ? $this->_storeManager->getStore($this->getStoreId())
  73. : $this->_storeManager->getStore();
  74. $filterData = [
  75. UrlRewrite::ENTITY_ID => $rewriteData[1],
  76. UrlRewrite::ENTITY_TYPE => $rewriteData[0],
  77. UrlRewrite::STORE_ID => $store->getId(),
  78. ];
  79. if (!empty($rewriteData[2]) && $rewriteData[0] == ProductUrlRewriteGenerator::ENTITY_TYPE) {
  80. $filterData[UrlRewrite::METADATA]['category_id'] = $rewriteData[2];
  81. }
  82. $rewrite = $this->urlFinder->findOneByData($filterData);
  83. if ($rewrite) {
  84. $href = $store->getUrl('', ['_direct' => $rewrite->getRequestPath()]);
  85. if (strpos($href, '___store') === false) {
  86. $href .= (strpos($href, '?') === false ? '?' : '&') . '___store=' . $store->getCode();
  87. }
  88. }
  89. $this->_href = $href;
  90. }
  91. return $this->_href;
  92. }
  93. /**
  94. * Parse id_path
  95. *
  96. * @param string $idPath
  97. * @throws \RuntimeException
  98. * @return array
  99. */
  100. protected function parseIdPath($idPath)
  101. {
  102. $rewriteData = explode('/', $idPath);
  103. if (!isset($rewriteData[0]) || !isset($rewriteData[1])) {
  104. throw new \RuntimeException('Wrong id_path structure.');
  105. }
  106. return $rewriteData;
  107. }
  108. /**
  109. * Prepare label using passed text as parameter.
  110. * If anchor text was not specified get entity name from DB.
  111. *
  112. * @return string
  113. */
  114. public function getLabel()
  115. {
  116. if (!$this->_anchorText) {
  117. if ($this->getData('anchor_text')) {
  118. $this->_anchorText = $this->getData('anchor_text');
  119. } elseif ($this->_entityResource) {
  120. $idPath = explode('/', $this->_getData('id_path'));
  121. if (isset($idPath[1])) {
  122. $id = $idPath[1];
  123. if ($id) {
  124. $this->_anchorText = $this->_entityResource->getAttributeRawValue(
  125. $id,
  126. 'name',
  127. $this->_storeManager->getStore()
  128. );
  129. }
  130. }
  131. }
  132. }
  133. return $this->_anchorText;
  134. }
  135. /**
  136. * Render block HTML
  137. * or return empty string if url can't be prepared
  138. *
  139. * @return string
  140. */
  141. protected function _toHtml()
  142. {
  143. if ($this->getHref()) {
  144. return parent::_toHtml();
  145. }
  146. return '';
  147. }
  148. }