Categories.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © 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\Sidebar;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Blog sidebar categories block
  12. */
  13. class Categories extends \Magento\Framework\View\Element\Template
  14. {
  15. use Widget;
  16. /**
  17. * @var string
  18. */
  19. protected $_widgetKey = 'categories';
  20. /**
  21. * @var \Magefan\Blog\Model\ResourceModel\Category\Collection
  22. */
  23. protected $_categoryCollection;
  24. /**
  25. * Construct
  26. *
  27. * @param \Magento\Framework\View\Element\Context $context
  28. * @param \Magefan\Blog\Model\ResourceModel\Category\Collection $categoryCollection
  29. * @param array $data
  30. */
  31. public function __construct(
  32. \Magento\Framework\View\Element\Template\Context $context,
  33. \Magefan\Blog\Model\ResourceModel\Category\Collection $categoryCollection,
  34. array $data = []
  35. ) {
  36. parent::__construct($context, $data);
  37. $this->_categoryCollection = $categoryCollection;
  38. }
  39. /**
  40. * Get grouped categories
  41. * @return \Magefan\Blog\Model\ResourceModel\Category\Collection
  42. */
  43. public function getGroupedChilds()
  44. {
  45. $k = 'grouped_childs';
  46. if (!$this->hasData($k)) {
  47. $array = $this->_categoryCollection
  48. ->addActiveFilter()
  49. ->addStoreFilter($this->_storeManager->getStore()->getId())
  50. ->setOrder('position')
  51. ->getTreeOrderedArray();
  52. $this->setData($k, $array);
  53. }
  54. return $this->getData($k);
  55. }
  56. /**
  57. * Retrieve true if need to show posts count
  58. * @return int
  59. */
  60. public function showPostsCount()
  61. {
  62. $key = 'show_posts_count';
  63. if (!$this->hasData($key)) {
  64. $this->setData($key, (bool)$this->_scopeConfig->getValue(
  65. 'mfblog/sidebar/'.$this->_widgetKey.'/show_posts_count', ScopeInterface::SCOPE_STORE
  66. ));
  67. }
  68. return $this->getData($key);
  69. }
  70. /**
  71. * Retrieve block identities
  72. * @return array
  73. */
  74. public function getIdentities()
  75. {
  76. return [\Magento\Cms\Model\Block::CACHE_TAG . '_blog_categories_widget' ];
  77. }
  78. }