Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
11 
13 {
19  protected $_itemObjectClass = \Magento\Framework\Mview\ViewInterface::class;
20 
24  protected $config;
25 
29  protected $statesFactory;
30 
34  private $indexerConfig;
35 
42  public function __construct(
43  \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
44  \Magento\Framework\Mview\ConfigInterface $config,
45  \Magento\Framework\Mview\View\State\CollectionFactory $statesFactory,
46  ConfigInterface $indexerConfig = null
47  ) {
48  $this->config = $config;
49  $this->statesFactory = $statesFactory;
50  $this->indexerConfig = $indexerConfig ?: ObjectManager::getInstance()->get(ConfigInterface::class);
51  parent::__construct($entityFactory);
52  }
53 
63  public function loadData($printQuery = false, $logQuery = false)
64  {
65  if (!$this->isLoaded()) {
66  $states = $this->statesFactory->create();
67  foreach ($this->getOrderedViewIds() as $viewId) {
69  $view = $this->getNewEmptyItem();
70  $view = $view->load($viewId);
71  foreach ($states->getItems() as $state) {
73  if ($state->getViewId() == $viewId) {
74  $view->setState($state);
75  break;
76  }
77  }
78  $this->_addItem($view);
79  }
80  $this->_setIsLoaded(true);
81  }
82  return $this;
83  }
84 
90  private function getOrderedViewIds()
91  {
92  $orderedViewIds = [];
94  foreach (array_keys($this->indexerConfig->getIndexers()) as $indexerId) {
95  $indexer = $this->_entityFactory->create(IndexerInterface::class);
96  $orderedViewIds[] = $indexer->load($indexerId)->getViewId();
97  }
98  $orderedViewIds = array_filter($orderedViewIds);
99  $orderedViewIds += array_diff(array_keys($this->config->getViews()), $orderedViewIds);
100 
101  return $orderedViewIds;
102  }
103 
110  public function getViewsByStateMode($mode)
111  {
112  $this->load();
113 
114  $result = [];
115  foreach ($this as $view) {
117  if ($view->getState()->getMode() == $mode) {
118  $result[] = $view;
119  }
120  }
121  return $result;
122  }
123 }
loadData($printQuery=false, $logQuery=false)
Definition: Collection.php:675
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
load($printQuery=false, $logQuery=false)
Definition: Collection.php:687
__construct(\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Magento\Framework\Mview\ConfigInterface $config, \Magento\Framework\Mview\View\State\CollectionFactory $statesFactory, ConfigInterface $indexerConfig=null)
Definition: Collection.php:42