Navigation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Catalog layered navigation view block
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\LayeredNavigation\Block;
  12. use Magento\Framework\View\Element\Template;
  13. /**
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Navigation extends \Magento\Framework\View\Element\Template
  18. {
  19. /**
  20. * Catalog layer
  21. *
  22. * @var \Magento\Catalog\Model\Layer
  23. */
  24. protected $_catalogLayer;
  25. /**
  26. * @var \Magento\Catalog\Model\Layer\FilterList
  27. */
  28. protected $filterList;
  29. /**
  30. * @var \Magento\Catalog\Model\Layer\AvailabilityFlagInterface
  31. */
  32. protected $visibilityFlag;
  33. /**
  34. * @param Template\Context $context
  35. * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
  36. * @param \Magento\Catalog\Model\Layer\FilterList $filterList
  37. * @param \Magento\Catalog\Model\Layer\AvailabilityFlagInterface $visibilityFlag
  38. * @param array $data
  39. */
  40. public function __construct(
  41. \Magento\Framework\View\Element\Template\Context $context,
  42. \Magento\Catalog\Model\Layer\Resolver $layerResolver,
  43. \Magento\Catalog\Model\Layer\FilterList $filterList,
  44. \Magento\Catalog\Model\Layer\AvailabilityFlagInterface $visibilityFlag,
  45. array $data = []
  46. ) {
  47. $this->_catalogLayer = $layerResolver->get();
  48. $this->filterList = $filterList;
  49. $this->visibilityFlag = $visibilityFlag;
  50. parent::__construct($context, $data);
  51. }
  52. /**
  53. * Apply layer
  54. *
  55. * @return $this
  56. */
  57. protected function _prepareLayout()
  58. {
  59. foreach ($this->filterList->getFilters($this->_catalogLayer) as $filter) {
  60. $filter->apply($this->getRequest());
  61. }
  62. $this->getLayer()->apply();
  63. return parent::_prepareLayout();
  64. }
  65. /**
  66. * Get layer object
  67. *
  68. * @return \Magento\Catalog\Model\Layer
  69. */
  70. public function getLayer()
  71. {
  72. return $this->_catalogLayer;
  73. }
  74. /**
  75. * Get layered navigation state html
  76. *
  77. * @return string
  78. */
  79. public function getStateHtml()
  80. {
  81. return $this->getChildHtml('state');
  82. }
  83. /**
  84. * Get all layer filters
  85. *
  86. * @return array
  87. */
  88. public function getFilters()
  89. {
  90. return $this->filterList->getFilters($this->_catalogLayer);
  91. }
  92. /**
  93. * Check availability display layer block
  94. *
  95. * @return bool
  96. */
  97. public function canShowBlock()
  98. {
  99. return $this->visibilityFlag->isEnabled($this->getLayer(), $this->getFilters());
  100. }
  101. /**
  102. * Get url for 'Clear All' link
  103. *
  104. * @return string
  105. */
  106. public function getClearUrl()
  107. {
  108. return $this->getChildBlock('state')->getClearUrl();
  109. }
  110. }