Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Grid.php
Go to the documentation of this file.
1 <?php
8 
19 {
23  const XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER = 'checkout/cart/number_items_to_display_pager';
24 
28  private $itemsCollection;
29 
34  private $itemCollectionFactory;
35 
39  private $joinAttributeProcessor;
40 
46  private $isPagerDisplayed;
47 
60  public function __construct(
61  \Magento\Framework\View\Element\Template\Context $context,
62  \Magento\Customer\Model\Session $customerSession,
63  \Magento\Checkout\Model\Session $checkoutSession,
64  \Magento\Catalog\Model\ResourceModel\Url $catalogUrlBuilder,
65  \Magento\Checkout\Helper\Cart $cartHelper,
66  \Magento\Framework\App\Http\Context $httpContext,
67  \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory $itemCollectionFactory,
68  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
69  array $data = []
70  ) {
71  $this->itemCollectionFactory = $itemCollectionFactory;
72  $this->joinAttributeProcessor = $joinProcessor;
73  parent::__construct(
74  $context,
75  $customerSession,
76  $checkoutSession,
77  $catalogUrlBuilder,
78  $cartHelper,
80  $data
81  );
82  }
83 
93  protected function _construct()
94  {
95  if (!$this->isPagerDisplayedOnPage()) {
96  parent::_construct();
97  }
98  if ($this->hasData('template')) {
99  $this->setTemplate($this->getData('template'));
100  }
101  }
102 
107  protected function _prepareLayout()
108  {
109  parent::_prepareLayout();
110  if ($this->isPagerDisplayedOnPage()) {
111  $availableLimit = (int)$this->_scopeConfig->getValue(
112  self::XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER,
113  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
114  );
115  $itemsCollection = $this->getItemsForGrid();
117  $pager = $this->getLayout()->createBlock(\Magento\Theme\Block\Html\Pager::class);
118  $pager->setAvailableLimit([$availableLimit => $availableLimit])->setCollection($itemsCollection);
119  $this->setChild('pager', $pager);
120  $itemsCollection->load();
121  $this->prepareItemUrls();
122  }
123  return $this;
124  }
125 
132  public function getItemsForGrid()
133  {
134  if (!$this->itemsCollection) {
136  $itemCollection = $this->itemCollectionFactory->create();
137 
138  $itemCollection->setQuote($this->getQuote());
139  $itemCollection->addFieldToFilter('parent_item_id', ['null' => true]);
140  $this->joinAttributeProcessor->process($itemCollection);
141 
142  $this->itemsCollection = $itemCollection;
143  }
144  return $this->itemsCollection;
145  }
146 
151  public function getItems()
152  {
153  if (!$this->isPagerDisplayedOnPage()) {
154  return parent::getItems();
155  }
156  return $this->getItemsForGrid()->getItems();
157  }
158 
165  private function isPagerDisplayedOnPage()
166  {
167  if (!$this->isPagerDisplayed) {
168  $availableLimit = (int)$this->_scopeConfig->getValue(
169  self::XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER,
170  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
171  );
172  $this->isPagerDisplayed = !$this->getCustomItems() && $availableLimit < $this->getItemsCount();
173  }
174  return $this->isPagerDisplayed;
175  }
176 }
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Catalog\Model\ResourceModel\Url $catalogUrlBuilder, \Magento\Checkout\Helper\Cart $cartHelper, \Magento\Framework\App\Http\Context $httpContext, \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory $itemCollectionFactory, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor, array $data=[])
Definition: Grid.php:60
getData($key='', $index=null)
Definition: DataObject.php:119
const XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER
Definition: Grid.php:23