Link.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Block\Widget\Page;
  7. /**
  8. * Widget to display link to CMS page
  9. */
  10. class Link extends \Magento\Framework\View\Element\Html\Link implements \Magento\Widget\Block\BlockInterface
  11. {
  12. /**
  13. * Prepared href attribute
  14. *
  15. * @var string
  16. */
  17. protected $_href;
  18. /**
  19. * Prepared title attribute
  20. *
  21. * @var string
  22. */
  23. protected $_title;
  24. /**
  25. * Prepared anchor text
  26. *
  27. * @var string
  28. */
  29. protected $_anchorText;
  30. /**
  31. * @var \Magento\Cms\Model\ResourceModel\Page
  32. */
  33. protected $_resourcePage;
  34. /**
  35. * Cms page
  36. *
  37. * @var \Magento\Cms\Helper\Page
  38. */
  39. protected $_cmsPage;
  40. /**
  41. * @param \Magento\Framework\View\Element\Template\Context $context
  42. * @param \Magento\Cms\Model\ResourceModel\Page $resourcePage
  43. * @param \Magento\Cms\Helper\Page $cmsPage
  44. * @param array $data
  45. */
  46. public function __construct(
  47. \Magento\Framework\View\Element\Template\Context $context,
  48. \Magento\Cms\Model\ResourceModel\Page $resourcePage,
  49. \Magento\Cms\Helper\Page $cmsPage,
  50. array $data = []
  51. ) {
  52. parent::__construct($context, $data);
  53. $this->_resourcePage = $resourcePage;
  54. $this->_cmsPage = $cmsPage;
  55. }
  56. /**
  57. * Prepare page url. Use passed identifier
  58. * or retrieve such using passed page id.
  59. *
  60. * @return string
  61. */
  62. public function getHref()
  63. {
  64. if (!$this->_href) {
  65. $this->_href = '';
  66. if ($this->getData('href')) {
  67. $this->_href = $this->getData('href');
  68. } elseif ($this->getData('page_id')) {
  69. $this->_href = $this->_cmsPage->getPageUrl($this->getData('page_id'));
  70. }
  71. }
  72. return $this->_href;
  73. }
  74. /**
  75. * Prepare anchor title attribute using passed title
  76. * as parameter or retrieve page title from DB using passed identifier or page id.
  77. *
  78. * @return string
  79. */
  80. public function getTitle()
  81. {
  82. if (!$this->_title) {
  83. $this->_title = '';
  84. if ($this->getData('title') !== null) {
  85. // compare to null used here bc user can specify blank title
  86. $this->_title = $this->getData('title');
  87. } elseif ($this->getData('page_id')) {
  88. $this->_title = $this->_resourcePage->getCmsPageTitleById($this->getData('page_id'));
  89. } elseif ($this->getData('href')) {
  90. $this->_title = $this->_resourcePage->setStore($this->_storeManager->getStore())
  91. ->getCmsPageTitleByIdentifier($this->getData('href'));
  92. }
  93. }
  94. return $this->_title;
  95. }
  96. /**
  97. * Prepare label using passed text as parameter.
  98. * If anchor text was not specified use title instead and
  99. * if title will be blank string, page identifier will be used.
  100. *
  101. * @return string
  102. */
  103. public function getLabel()
  104. {
  105. if ($this->getData('anchor_text')) {
  106. $this->_anchorText = $this->getData('anchor_text');
  107. } elseif ($this->getData('href')) {
  108. $this->_anchorText = $this->_resourcePage->setStore(
  109. $this->_storeManager->getStore()
  110. )->getCmsPageTitleByIdentifier(
  111. $this->getData('href')
  112. );
  113. } elseif ($this->getData('page_id')) {
  114. $this->_anchorText = $this->_resourcePage->getCmsPageTitleById($this->getData('page_id'));
  115. } elseif ($this->getTitle()) {
  116. $this->_anchorText = $this->getTitle();
  117. } else {
  118. $this->_anchorText = $this->getData('href');
  119. }
  120. return $this->_anchorText;
  121. }
  122. }