123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\View\Element\Html\Link;
- use Magento\Framework\App\DefaultPathInterface;
- use Magento\Framework\View\Element\Template;
- use Magento\Framework\View\Element\Template\Context;
- /**
- * Block representing link with two possible states.
- * "Current" state means link leads to URL equivalent to URL of currently displayed page.
- *
- * @api
- * @method string getLabel()
- * @method string getPath()
- * @method string getTitle()
- * @method null|array getAttributes()
- * @method null|bool getCurrent()
- * @method \Magento\Framework\View\Element\Html\Link\Current setCurrent(bool $value)
- * @since 100.0.2
- */
- class Current extends Template
- {
- /**
- * Default path
- *
- * @var DefaultPathInterface
- */
- protected $_defaultPath;
- /**
- * Constructor
- *
- * @param Context $context
- * @param DefaultPathInterface $defaultPath
- * @param array $data
- */
- public function __construct(
- Context $context,
- DefaultPathInterface $defaultPath,
- array $data = []
- ) {
- parent::__construct($context, $data);
- $this->_defaultPath = $defaultPath;
- }
- /**
- * Get href URL
- *
- * @return string
- */
- public function getHref()
- {
- return $this->getUrl($this->getPath());
- }
- /**
- * Get current mca
- *
- * @return string
- * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
- */
- private function getMca()
- {
- $routeParts = [
- (string)$this->_request->getModuleName(),
- (string)$this->_request->getControllerName(),
- (string)$this->_request->getActionName(),
- ];
- $parts = [];
- $pathParts = explode('/', trim($this->_request->getPathInfo(), '/'));
- foreach ($routeParts as $key => $value) {
- if (isset($pathParts[$key]) && $pathParts[$key] === $value) {
- $parts[] = $value;
- }
- }
- return implode('/', $parts);
- }
- /**
- * Check if link leads to URL equivalent to URL of currently displayed page
- *
- * @return bool
- */
- public function isCurrent()
- {
- return $this->getCurrent() || $this->getUrl($this->getPath()) == $this->getUrl($this->getMca());
- }
- /**
- * Render block HTML
- *
- * @return string
- */
- protected function _toHtml()
- {
- if (false != $this->getTemplate()) {
- return parent::_toHtml();
- }
- $highlight = '';
- if ($this->getIsHighlighted()) {
- $highlight = ' current';
- }
- if ($this->isCurrent()) {
- $html = '<li class="nav item current">';
- $html .= '<strong>'
- . $this->escapeHtml(__($this->getLabel()))
- . '</strong>';
- $html .= '</li>';
- } else {
- $html = '<li class="nav item' . $highlight . '"><a href="' . $this->escapeHtml($this->getHref()) . '"';
- $html .= $this->getTitle()
- ? ' title="' . $this->escapeHtml(__($this->getTitle())) . '"'
- : '';
- $html .= $this->getAttributesHtml() . '>';
- if ($this->getIsHighlighted()) {
- $html .= '<strong>';
- }
- $html .= $this->escapeHtml(__($this->getLabel()));
- if ($this->getIsHighlighted()) {
- $html .= '</strong>';
- }
- $html .= '</a></li>';
- }
- return $html;
- }
- /**
- * Generate attributes' HTML code
- *
- * @return string
- */
- private function getAttributesHtml()
- {
- $attributesHtml = '';
- $attributes = $this->getAttributes();
- if ($attributes) {
- foreach ($attributes as $attribute => $value) {
- $attributesHtml .= ' ' . $attribute . '="' . $this->escapeHtml($value) . '"';
- }
- }
- return $attributesHtml;
- }
- }
|