View.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © 2015-2017 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\Category;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Blog category view
  12. */
  13. class View extends \Magefan\Blog\Block\Post\PostList
  14. {
  15. /**
  16. * Prepare posts collection
  17. *
  18. * @return void
  19. */
  20. protected function _preparePostCollection()
  21. {
  22. parent::_preparePostCollection();
  23. if ($category = $this->getCategory()) {
  24. $this->_postCollection->addCategoryFilter($category);
  25. }
  26. }
  27. /**
  28. * Retrieve category instance
  29. *
  30. * @return \Magefan\Blog\Model\Category
  31. */
  32. public function getCategory()
  33. {
  34. return $this->_coreRegistry->registry('current_blog_category');
  35. }
  36. /**
  37. * Preparing global layout
  38. *
  39. * @return $this
  40. */
  41. protected function _prepareLayout()
  42. {
  43. $category = $this->getCategory();
  44. if ($category) {
  45. $this->_addBreadcrumbs($category);
  46. $this->pageConfig->addBodyClass('blog-category-' . $category->getIdentifier());
  47. $this->pageConfig->getTitle()->set($category->getMetaTitle());
  48. $this->pageConfig->setKeywords($category->getMetaKeywords());
  49. $this->pageConfig->setDescription($category->getMetaDescription());
  50. $this->pageConfig->addRemotePageAsset(
  51. $category->getCanonicalUrl(),
  52. 'canonical',
  53. ['attributes' => ['rel' => 'canonical']]
  54. );
  55. $pageMainTitle = $this->getLayout()->getBlock('page.main.title');
  56. if ($pageMainTitle) {
  57. $pageMainTitle->setPageTitle(
  58. $this->escapeHtml($category->getTitle())
  59. );
  60. }
  61. }
  62. return parent::_prepareLayout();
  63. }
  64. /**
  65. * Prepare breadcrumbs
  66. *
  67. * @param string $title
  68. * @param string $key
  69. * @throws \Magento\Framework\Exception\LocalizedException
  70. * @return void
  71. */
  72. protected function _addBreadcrumbs($title = null, $key = null)
  73. {
  74. parent::_addBreadcrumbs();
  75. if ($breadcrumbsBlock = $this->getBreadcrumbsBlock()) {
  76. $category = $this->getCategory();
  77. $parentCategories = [];
  78. while ($parentCategory = $category->getParentCategory()) {
  79. $parentCategories[] = $category = $parentCategory;
  80. }
  81. for ($i = count($parentCategories) - 1; $i >= 0; $i--) {
  82. $category = $parentCategories[$i];
  83. $breadcrumbsBlock->addCrumb('blog_parent_category_' . $category->getId(), [
  84. 'label' => $category->getTitle(),
  85. 'title' => $category->getTitle(),
  86. 'link' => $category->getCategoryUrl()
  87. ]);
  88. }
  89. $category = $this->getCategory();
  90. $breadcrumbsBlock->addCrumb('blog_category',[
  91. 'label' => $category->getTitle(),
  92. 'title' => $category->getTitle()
  93. ]);
  94. }
  95. }
  96. }