RelatedPosts.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © 2015 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\View;
  9. use Magento\Catalog\Model\ResourceModel\Product\Collection;
  10. use Magento\Framework\View\Element\AbstractBlock;
  11. /**
  12. * Blog post related posts block
  13. */
  14. class RelatedPosts extends \Magefan\Blog\Block\Post\PostList\AbstractList
  15. {
  16. /**
  17. * Prepare posts collection
  18. *
  19. * @return void
  20. */
  21. protected function _preparePostCollection()
  22. {
  23. $pageSize = (int) $this->_scopeConfig->getValue(
  24. 'mfblog/post_view/related_posts/number_of_posts',
  25. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  26. );
  27. $this->_postCollection = $this->getPost()->getRelatedPosts()
  28. ->addActiveFilter()
  29. ->setPageSize($pageSize ?: 5);
  30. $this->_postCollection->getSelect()->order('rl.position', 'ASC');
  31. }
  32. /**
  33. * Retrieve true if Display Related Posts enabled
  34. * @return boolean
  35. */
  36. public function displayPosts()
  37. {
  38. return (bool) $this->_scopeConfig->getValue(
  39. 'mfblog/post_view/related_posts/enabled',
  40. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  41. );
  42. }
  43. /**
  44. * Retrieve posts instance
  45. *
  46. * @return \Magefan\Blog\Model\Category
  47. */
  48. public function getPost()
  49. {
  50. if (!$this->hasData('post')) {
  51. $this->setData('post',
  52. $this->_coreRegistry->registry('current_blog_post')
  53. );
  54. }
  55. return $this->getData('post');
  56. }
  57. /**
  58. * Get Block Identities
  59. * @return Array
  60. */
  61. public function getIdentities()
  62. {
  63. return [\Magento\Cms\Model\Page::CACHE_TAG . '_relatedposts_'.$this->getPost()->getId() ];
  64. }
  65. }