PostList.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Archive;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Blog archive posts list
  12. */
  13. class PostList extends \Magefan\Blog\Block\Post\PostList
  14. {
  15. /**
  16. * Prepare posts collection
  17. * @return \Magefan\Blog\Model\ResourceModel\Post\Collection
  18. */
  19. protected function _preparePostCollection()
  20. {
  21. parent::_preparePostCollection();
  22. $this->_postCollection->addArchiveFilter(
  23. $this->getYear(),
  24. $this->getMonth()
  25. );
  26. }
  27. /**
  28. * Get archive month
  29. * @return string
  30. */
  31. public function getMonth()
  32. {
  33. return (int)$this->_coreRegistry->registry('current_blog_archive_month');
  34. }
  35. /**
  36. * Get archive year
  37. * @return string
  38. */
  39. public function getYear()
  40. {
  41. return (int)$this->_coreRegistry->registry('current_blog_archive_year');
  42. }
  43. /**
  44. * Preparing global layout
  45. *
  46. * @return $this
  47. */
  48. protected function _prepareLayout()
  49. {
  50. $title = $this->_getTitle();
  51. $this->_addBreadcrumbs($title, 'blog_search');
  52. $this->pageConfig->getTitle()->set($title);
  53. $this->pageConfig->addRemotePageAsset(
  54. $this->_url->getUrl(
  55. $this->getYear() . '-' . str_pad($this->getMonth(), 2, '0', STR_PAD_LEFT),
  56. \Magefan\Blog\Model\Url::CONTROLLER_ARCHIVE
  57. ),
  58. 'canonical',
  59. ['attributes' => ['rel' => 'canonical']]
  60. );
  61. $this->pageConfig->setRobots('NOINDEX,FOLLOW');
  62. return parent::_prepareLayout();
  63. }
  64. /**
  65. * Retrieve title
  66. * @return string
  67. */
  68. protected function _getTitle()
  69. {
  70. $time = strtotime($this->getYear().'-'.$this->getMonth().'-01');
  71. return sprintf(
  72. __('Monthly Archives: %s %s'),
  73. __(date('F', $time)), date('Y', $time)
  74. );
  75. }
  76. }