Links.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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;
  7. /**
  8. * Links list block
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Links extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * Get links
  17. *
  18. * @return \Magento\Framework\View\Element\Html\Link[]
  19. */
  20. public function getLinks()
  21. {
  22. return $this->_layout->getChildBlocks($this->getNameInLayout());
  23. }
  24. /**
  25. * Find link by path
  26. *
  27. * @param string $path
  28. * @return \Magento\Framework\View\Element\Html\Link
  29. */
  30. protected function getLinkByPath($path)
  31. {
  32. foreach ($this->getLinks() as $link) {
  33. if ($link->getPath() == $path) {
  34. return $link;
  35. }
  36. }
  37. }
  38. /**
  39. * Set active link
  40. *
  41. * @param string $path
  42. * @return void
  43. */
  44. public function setActive($path)
  45. {
  46. $link = $this->getLinkByPath($path);
  47. if ($link) {
  48. $link->setIsHighlighted(true);
  49. }
  50. }
  51. /**
  52. * Render Block
  53. *
  54. * @param \Magento\Framework\View\Element\AbstractBlock $link
  55. * @return string
  56. */
  57. public function renderLink(\Magento\Framework\View\Element\AbstractBlock $link)
  58. {
  59. return $this->_layout->renderElement($link->getNameInLayout());
  60. }
  61. /**
  62. * Render block HTML
  63. *
  64. * @return string
  65. */
  66. protected function _toHtml()
  67. {
  68. if (false != $this->getTemplate()) {
  69. return parent::_toHtml();
  70. }
  71. $html = '';
  72. if ($this->getLinks()) {
  73. $html = '<ul' . ($this->hasCssClass() ? ' class="' . $this->escapeHtml(
  74. $this->getCssClass()
  75. ) . '"' : '') . '>';
  76. foreach ($this->getLinks() as $link) {
  77. $html .= $this->renderLink($link);
  78. }
  79. $html .= '</ul>';
  80. }
  81. return $html;
  82. }
  83. }