Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Link.php
Go to the documentation of this file.
1 <?php
13 
17 
19 {
24  protected $_entityResource = null;
25 
31  protected $_href;
32 
38  protected $_anchorText;
39 
45  protected $urlFinder;
46 
53  public function __construct(
54  \Magento\Framework\View\Element\Template\Context $context,
56  \Magento\Catalog\Model\ResourceModel\AbstractResource $entityResource = null,
57  array $data = []
58  ) {
59  parent::__construct($context, $data);
60  $this->urlFinder = $urlFinder;
61  $this->_entityResource = $entityResource;
62  }
63 
72  public function getHref()
73  {
74  if ($this->_href === null) {
75  if (!$this->getData('id_path')) {
76  throw new \RuntimeException('Parameter id_path is not set.');
77  }
78  $rewriteData = $this->parseIdPath($this->getData('id_path'));
79 
80  $href = false;
81  $store = $this->hasStoreId() ? $this->_storeManager->getStore($this->getStoreId())
82  : $this->_storeManager->getStore();
83  $filterData = [
84  UrlRewrite::ENTITY_ID => $rewriteData[1],
85  UrlRewrite::ENTITY_TYPE => $rewriteData[0],
86  UrlRewrite::STORE_ID => $store->getId(),
87  ];
88  if (!empty($rewriteData[2]) && $rewriteData[0] == ProductUrlRewriteGenerator::ENTITY_TYPE) {
89  $filterData[UrlRewrite::METADATA]['category_id'] = $rewriteData[2];
90  }
91  $rewrite = $this->urlFinder->findOneByData($filterData);
92 
93  if ($rewrite) {
94  $href = $store->getUrl('', ['_direct' => $rewrite->getRequestPath()]);
95 
96  if (strpos($href, '___store') === false) {
97  $href .= (strpos($href, '?') === false ? '?' : '&') . '___store=' . $store->getCode();
98  }
99  }
100  $this->_href = $href;
101  }
102  return $this->_href;
103  }
104 
112  protected function parseIdPath($idPath)
113  {
114  $rewriteData = explode('/', $idPath);
115 
116  if (!isset($rewriteData[0]) || !isset($rewriteData[1])) {
117  throw new \RuntimeException('Wrong id_path structure.');
118  }
119  return $rewriteData;
120  }
121 
128  public function getLabel()
129  {
130  if (!$this->_anchorText) {
131  if ($this->getData('anchor_text')) {
132  $this->_anchorText = $this->getData('anchor_text');
133  } elseif ($this->_entityResource) {
134  $idPath = explode('/', $this->_getData('id_path'));
135  if (isset($idPath[1])) {
136  $id = $idPath[1];
137  if ($id) {
138  $this->_anchorText = $this->_entityResource->getAttributeRawValue(
139  $id,
140  'name',
141  $this->_storeManager->getStore()
142  );
143  }
144  }
145  }
146  }
147 
148  return $this->_anchorText;
149  }
150 
157  protected function _toHtml()
158  {
159  if ($this->getHref()) {
160  return parent::_toHtml();
161  }
162  return '';
163  }
164 }
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14