PostList.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Block\Post;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Blog post list block
  12. */
  13. class PostList extends \Magefan\Blog\Block\Post\PostList\AbstractList
  14. {
  15. /**
  16. * Block template file
  17. * @var string
  18. */
  19. protected $_defaultToolbarBlock = 'Magefan\Blog\Block\Post\PostList\Toolbar';
  20. /**
  21. * Preparing global layout
  22. *
  23. * @return $this
  24. */
  25. protected function _prepareLayout()
  26. {
  27. $page = $this->_request->getParam(
  28. \Magefan\Blog\Block\Post\PostList\Toolbar::PAGE_PARM_NAME
  29. );
  30. if ($page > 1) {
  31. $this->pageConfig->setRobots('NOINDEX,FOLLOW');
  32. }
  33. return parent::_prepareLayout();
  34. }
  35. /**
  36. * Retrieve post html
  37. * @param \Magefan\Blog\Model\Post $post
  38. * @return string
  39. */
  40. public function getPostHtml($post)
  41. {
  42. return $this->getChildBlock('blog.posts.list.item')->setPost($post)->toHtml();
  43. }
  44. /**
  45. * Retrieve Toolbar Block
  46. * @return \Magefan\Blog\Block\Post\PostList\Toolbar
  47. */
  48. public function getToolbarBlock()
  49. {
  50. $blockName = $this->getToolbarBlockName();
  51. if ($blockName) {
  52. $block = $this->getLayout()->getBlock($blockName);
  53. if ($block) {
  54. return $block;
  55. }
  56. }
  57. $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
  58. return $block;
  59. }
  60. /**
  61. * Retrieve Toolbar Html
  62. * @return string
  63. */
  64. public function getToolbarHtml()
  65. {
  66. return $this->getChildHtml('toolbar');
  67. }
  68. /**
  69. * Before block to html
  70. *
  71. * @return $this
  72. */
  73. protected function _beforeToHtml()
  74. {
  75. $toolbar = $this->getToolbarBlock();
  76. // called prepare sortable parameters
  77. $collection = $this->getPostCollection();
  78. // set collection to toolbar and apply sort
  79. $toolbar->setCollection($collection);
  80. $this->setChild('toolbar', $toolbar);
  81. return parent::_beforeToHtml();
  82. }
  83. /**
  84. * Prepare breadcrumbs
  85. *
  86. * @param string $title
  87. * @param string $key
  88. * @throws \Magento\Framework\Exception\LocalizedException
  89. * @return void
  90. */
  91. protected function _addBreadcrumbs($title = null, $key = null)
  92. {
  93. if ($breadcrumbsBlock = $this->getBreadcrumbsBlock()) {
  94. $breadcrumbsBlock->addCrumb(
  95. 'home',
  96. [
  97. 'label' => __('Home'),
  98. 'title' => __('Go to Home Page'),
  99. 'link' => $this->_storeManager->getStore()->getBaseUrl()
  100. ]
  101. );
  102. $blogTitle = $this->_scopeConfig->getValue(
  103. 'mfblog/index_page/title',
  104. ScopeInterface::SCOPE_STORE
  105. );
  106. $breadcrumbsBlock->addCrumb(
  107. 'blog',
  108. [
  109. 'label' => __($blogTitle),
  110. 'title' => __($blogTitle),
  111. 'link' => $title ? $this->_url->getBaseUrl() : null,
  112. ]
  113. );
  114. if ($title) {
  115. $breadcrumbsBlock->addCrumb($key ?: 'blog_item', ['label' => $title, 'title' => $title]);
  116. }
  117. }
  118. }
  119. /**
  120. * Retrieve breadcrumbs block
  121. *
  122. * @return mixed
  123. */
  124. protected function getBreadcrumbsBlock()
  125. {
  126. if ($this->_scopeConfig->getValue('web/default/show_cms_breadcrumbs', ScopeInterface::SCOPE_STORE)) {
  127. return $this->getLayout()->getBlock('breadcrumbs');
  128. }
  129. return false;
  130. }
  131. }