View.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Post;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Blog post view
  12. */
  13. class View extends AbstractPost
  14. {
  15. /**
  16. * Preparing global layout
  17. *
  18. * @return $this
  19. */
  20. protected function _prepareLayout()
  21. {
  22. $post = $this->getPost();
  23. if ($post) {
  24. $this->_addBreadcrumbs($post->getTitle(), 'blog_post');
  25. $this->pageConfig->addBodyClass('blog-post-' . $post->getIdentifier());
  26. $this->pageConfig->getTitle()->set($post->getMetaTitle());
  27. $this->pageConfig->setKeywords($post->getMetaKeywords());
  28. $this->pageConfig->setDescription($post->getMetaDescription());
  29. $this->pageConfig->addRemotePageAsset(
  30. $post->getCanonicalUrl(),
  31. 'canonical',
  32. ['attributes' => ['rel' => 'canonical']]
  33. );
  34. $pageMainTitle = $this->getLayout()->getBlock('page.main.title');
  35. if ($pageMainTitle) {
  36. $pageMainTitle->setPageTitle(
  37. $this->escapeHtml($post->getTitle())
  38. );
  39. }
  40. if ($post->getIsPreviewMode()) {
  41. $this->pageConfig->setRobots('NOINDEX,FOLLOW');
  42. }
  43. }
  44. return parent::_prepareLayout();
  45. }
  46. /**
  47. * Prepare breadcrumbs
  48. *
  49. * @param string $title
  50. * @param string $key
  51. * @throws \Magento\Framework\Exception\LocalizedException
  52. * @return void
  53. */
  54. protected function _addBreadcrumbs($title = null, $key = null)
  55. {
  56. if ($this->_scopeConfig->getValue('web/default/show_cms_breadcrumbs', ScopeInterface::SCOPE_STORE)
  57. && ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs'))
  58. ) {
  59. $breadcrumbsBlock->addCrumb(
  60. 'home',
  61. [
  62. 'label' => __('Home'),
  63. 'title' => __('Go to Home Page'),
  64. 'link' => $this->_storeManager->getStore()->getBaseUrl()
  65. ]
  66. );
  67. $blogTitle = $this->_scopeConfig->getValue(
  68. 'mfblog/index_page/title',
  69. ScopeInterface::SCOPE_STORE
  70. );
  71. $breadcrumbsBlock->addCrumb(
  72. 'blog',
  73. [
  74. 'label' => __($blogTitle),
  75. 'title' => __($blogTitle),
  76. 'link' => $this->_url->getBaseUrl()
  77. ]
  78. );
  79. $parentCategories = [];
  80. $parentCategory = $this->getPost()->getParentCategory();
  81. while ($parentCategory ) {
  82. $parentCategories[] = $parentCategory;
  83. $parentCategory = $parentCategory->getParentCategory();
  84. }
  85. for ($i = count($parentCategories) - 1; $i >= 0; $i--) {
  86. $parentCategory = $parentCategories[$i];
  87. $breadcrumbsBlock->addCrumb('blog_parent_category_' . $parentCategory->getId(), [
  88. 'label' => $parentCategory->getTitle(),
  89. 'title' => $parentCategory->getTitle(),
  90. 'link' => $parentCategory->getCategoryUrl()
  91. ]);
  92. }
  93. $breadcrumbsBlock->addCrumb($key, [
  94. 'label' => $title ,
  95. 'title' => $title
  96. ]);
  97. }
  98. }
  99. }