Current.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\Html\Link;
  7. use Magento\Framework\App\DefaultPathInterface;
  8. use Magento\Framework\View\Element\Template;
  9. use Magento\Framework\View\Element\Template\Context;
  10. /**
  11. * Block representing link with two possible states.
  12. * "Current" state means link leads to URL equivalent to URL of currently displayed page.
  13. *
  14. * @api
  15. * @method string getLabel()
  16. * @method string getPath()
  17. * @method string getTitle()
  18. * @method null|array getAttributes()
  19. * @method null|bool getCurrent()
  20. * @method \Magento\Framework\View\Element\Html\Link\Current setCurrent(bool $value)
  21. * @since 100.0.2
  22. */
  23. class Current extends Template
  24. {
  25. /**
  26. * Default path
  27. *
  28. * @var DefaultPathInterface
  29. */
  30. protected $_defaultPath;
  31. /**
  32. * Constructor
  33. *
  34. * @param Context $context
  35. * @param DefaultPathInterface $defaultPath
  36. * @param array $data
  37. */
  38. public function __construct(
  39. Context $context,
  40. DefaultPathInterface $defaultPath,
  41. array $data = []
  42. ) {
  43. parent::__construct($context, $data);
  44. $this->_defaultPath = $defaultPath;
  45. }
  46. /**
  47. * Get href URL
  48. *
  49. * @return string
  50. */
  51. public function getHref()
  52. {
  53. return $this->getUrl($this->getPath());
  54. }
  55. /**
  56. * Get current mca
  57. *
  58. * @return string
  59. * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
  60. */
  61. private function getMca()
  62. {
  63. $routeParts = [
  64. (string)$this->_request->getModuleName(),
  65. (string)$this->_request->getControllerName(),
  66. (string)$this->_request->getActionName(),
  67. ];
  68. $parts = [];
  69. $pathParts = explode('/', trim($this->_request->getPathInfo(), '/'));
  70. foreach ($routeParts as $key => $value) {
  71. if (isset($pathParts[$key]) && $pathParts[$key] === $value) {
  72. $parts[] = $value;
  73. }
  74. }
  75. return implode('/', $parts);
  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. * Render block HTML
  88. *
  89. * @return string
  90. */
  91. protected function _toHtml()
  92. {
  93. if (false != $this->getTemplate()) {
  94. return parent::_toHtml();
  95. }
  96. $highlight = '';
  97. if ($this->getIsHighlighted()) {
  98. $highlight = ' current';
  99. }
  100. if ($this->isCurrent()) {
  101. $html = '<li class="nav item current">';
  102. $html .= '<strong>'
  103. . $this->escapeHtml(__($this->getLabel()))
  104. . '</strong>';
  105. $html .= '</li>';
  106. } else {
  107. $html = '<li class="nav item' . $highlight . '"><a href="' . $this->escapeHtml($this->getHref()) . '"';
  108. $html .= $this->getTitle()
  109. ? ' title="' . $this->escapeHtml(__($this->getTitle())) . '"'
  110. : '';
  111. $html .= $this->getAttributesHtml() . '>';
  112. if ($this->getIsHighlighted()) {
  113. $html .= '<strong>';
  114. }
  115. $html .= $this->escapeHtml(__($this->getLabel()));
  116. if ($this->getIsHighlighted()) {
  117. $html .= '</strong>';
  118. }
  119. $html .= '</a></li>';
  120. }
  121. return $html;
  122. }
  123. /**
  124. * Generate attributes' HTML code
  125. *
  126. * @return string
  127. */
  128. private function getAttributesHtml()
  129. {
  130. $attributesHtml = '';
  131. $attributes = $this->getAttributes();
  132. if ($attributes) {
  133. foreach ($attributes as $attribute => $value) {
  134. $attributesHtml .= ' ' . $attribute . '="' . $this->escapeHtml($value) . '"';
  135. }
  136. }
  137. return $attributesHtml;
  138. }
  139. }