Advertisement
Guest User

M2 Current

a guest
Aug 23rd, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Sho\CurrentLinks\Framework\View\Element\Html\Link;
  7.  
  8. /**
  9. * Block representing link with two possible states.
  10. * "Current" state means link leads to URL equivalent to URL of currently displayed page.
  11. *
  12. * @api
  13. * @method string getLabel()
  14. * @method string getPath()
  15. * @method string getTitle()
  16. * @method null|array getAttributes()
  17. * @method null|bool getCurrent()
  18. * @method \Magento\Framework\View\Element\Html\Link\Current setCurrent(bool $value)
  19. */
  20. class Current extends \Magento\Framework\View\Element\Template
  21. {
  22. /**
  23. * Default path
  24. *
  25. * @var \Magento\Framework\App\DefaultPathInterface
  26. */
  27. protected $_defaultPath;
  28.  
  29. /**
  30. * Constructor
  31. *
  32. * @param \Magento\Framework\View\Element\Template\Context $context
  33. * @param \Magento\Framework\App\DefaultPathInterface $defaultPath
  34. * @param array $data
  35. */
  36. public function __construct(
  37. \Magento\Framework\View\Element\Template\Context $context,
  38. \Magento\Framework\App\DefaultPathInterface $defaultPath,
  39. array $data = []
  40. ) {
  41. parent::__construct($context, $data);
  42. $this->_defaultPath = $defaultPath;
  43. }
  44.  
  45. /**
  46. * Get href URL
  47. *
  48. * @return string
  49. */
  50. public function getHref()
  51. {
  52. return $this->getUrl($this->getPath());
  53. }
  54.  
  55. /**
  56. * Get current mca
  57. *
  58. * @return string
  59. */
  60. private function getMca()
  61. {
  62. $routeParts = [
  63. 'module' => $this->_request->getModuleName(),
  64. 'controller' => $this->_request->getControllerName(),
  65. 'action' => $this->_request->getActionName(),
  66. ];
  67.  
  68. $parts = [];
  69. foreach ($routeParts as $key => $value) {
  70. if (!empty($value) && $value != $this->_defaultPath->getPart($key)) {
  71. $parts[] = $value;
  72. }
  73. }
  74. return implode('/', $parts);
  75. }
  76.  
  77. /**
  78. * Check if link leads to URL equivalent to URL of currently displayed page
  79. *
  80. * @return bool
  81. */
  82. public function isCurrent()
  83. {
  84. return $this->getCurrent() || $this->getUrl($this->getPath()) == $this->getUrl($this->getMca());
  85. }
  86.  
  87. /**
  88. * Render block HTML
  89. *
  90. * @return string
  91. */
  92. protected function _toHtml()
  93. {
  94. if (false != $this->getTemplate()) {
  95. return parent::_toHtml();
  96. }
  97.  
  98. $highlight = '';
  99.  
  100. if ($this->getIsHighlighted()) {
  101. $highlight = ' current';
  102. }
  103.  
  104. if ($this->isCurrent()) {
  105. $html = '<li class="nav item current">';
  106. $html .= '<strong class="' . $this->getTitle() .'">'
  107. . $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getLabel()))
  108. . '</strong>';
  109. $html .= '</li>';
  110. } else {
  111. $html = '<li class="nav item' . $highlight . '"><a href="' . $this->escapeHtml($this->getHref()) . '"';
  112. $html .= $this->getTitle()
  113. ? ' title="' . $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getTitle())) . '"'
  114. : '';
  115. $html .= $this->getAttributesHtml() . '>';
  116.  
  117. if ($this->getIsHighlighted()) {
  118. $html .= '<strong>';
  119. }
  120.  
  121. $html .= $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getLabel()));
  122.  
  123. if ($this->getIsHighlighted()) {
  124. $html .= '</strong>';
  125. }
  126.  
  127. $html .= '</a></li>';
  128. }
  129.  
  130. return $html;
  131. }
  132.  
  133. /**
  134. * Generate attributes' HTML code
  135. *
  136. * @return string
  137. */
  138. private function getAttributesHtml()
  139. {
  140. $attributesHtml = '';
  141. $attributes = $this->getAttributes();
  142. if ($attributes) {
  143. foreach ($attributes as $attribute => $value) {
  144. $attributesHtml .= ' ' . $attribute . '="' . $this->escapeHtml($value) . '"';
  145. }
  146. }
  147.  
  148. return $attributesHtml;
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement