Recent.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Widget;
  9. /**
  10. * Blog recent posts widget
  11. */
  12. class Recent extends \Magefan\Blog\Block\Post\PostList\AbstractList implements \Magento\Widget\Block\BlockInterface
  13. {
  14. /**
  15. * @var \Magefan\Blog\Model\CategoryFactory
  16. */
  17. protected $_categoryFactory;
  18. /**
  19. * @var \Magefan\Blog\Model\Category
  20. */
  21. protected $_category;
  22. /**
  23. * Construct
  24. *
  25. * @param \Magento\Framework\View\Element\Context $context
  26. * @param \Magento\Framework\Registry $coreRegistry
  27. * @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
  28. * @param \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory
  29. * @param \Magefan\Blog\Model\Url $url
  30. * @param \Magefan\Blog\Model\CategoryFactory $categoryFactory
  31. * @param array $data
  32. */
  33. public function __construct(
  34. \Magento\Framework\View\Element\Template\Context $context,
  35. \Magento\Framework\Registry $coreRegistry,
  36. \Magento\Cms\Model\Template\FilterProvider $filterProvider,
  37. \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory,
  38. \Magefan\Blog\Model\Url $url,
  39. \Magefan\Blog\Model\CategoryFactory $categoryFactory,
  40. array $data = []
  41. ) {
  42. parent::__construct($context, $coreRegistry, $filterProvider, $postCollectionFactory, $url, $data);
  43. $this->_categoryFactory = $categoryFactory;
  44. }
  45. /**
  46. * Set blog template
  47. *
  48. * @return this
  49. */
  50. public function _toHtml()
  51. {
  52. $this->setTemplate(
  53. $this->getData('custom_template') ?: 'widget/recent.phtml'
  54. );
  55. return parent::_toHtml();
  56. }
  57. /**
  58. * Retrieve block title
  59. *
  60. * @return string
  61. */
  62. public function getTitle()
  63. {
  64. return $this->getData('title') ?: __('Recent Blog Posts');
  65. }
  66. /**
  67. * Prepare posts collection
  68. *
  69. * @return void
  70. */
  71. protected function _preparePostCollection()
  72. {
  73. $size = $this->getData('number_of_posts');
  74. if (!$size) {
  75. $size = (int) $this->_scopeConfig->getValue(
  76. 'mfblog/sidebar/recent_posts/posts_per_page',
  77. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  78. );
  79. }
  80. $this->setPageSize($size);
  81. parent::_preparePostCollection();
  82. if ($category = $this->getCategory()) {
  83. $this->_postCollection->addCategoryFilter($category);
  84. }
  85. }
  86. /**
  87. * Retrieve category instance
  88. *
  89. * @return \Magefan\Blog\Model\Category
  90. */
  91. public function getCategory()
  92. {
  93. if ($this->_category === null) {
  94. if ($categoryId = $this->getData('category_id')) {
  95. $category = $this->_categoryFactory->create();
  96. $category->load($categoryId);
  97. $storeId = $this->_storeManager->getStore()->getId();
  98. if ($category->isVisibleOnStore($storeId)) {
  99. $category->setStoreId($storeId);
  100. return $this->_category = $category;
  101. }
  102. }
  103. $this->_category = false;
  104. }
  105. return $this->_category;
  106. }
  107. /**
  108. * Retrieve post short content
  109. * @param \Magefan\Blog\Model\Post $post
  110. *
  111. * @return string
  112. */
  113. public function getShorContent($post)
  114. {
  115. return $post->getShortFilteredContent();
  116. }
  117. }