Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductList.php
Go to the documentation of this file.
1 <?php
8 
16 {
20  const XML_PATH_LIST_MODE = 'catalog/frontend/list_mode';
21 
22  const VIEW_MODE_LIST = 'list';
23  const VIEW_MODE_GRID = 'grid';
24 
25  const DEFAULT_SORT_DIRECTION = 'asc';
29  protected $scopeConfig;
30 
34  private $coreRegistry;
35 
41  protected $_defaultAvailableLimit = [10 => 10,20 => 20,50 => 50];
42 
46  public function __construct(
48  \Magento\Framework\Registry $coreRegistry = null
49  ) {
50  $this->scopeConfig = $scopeConfig;
51  $this->coreRegistry = $coreRegistry ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
52  \Magento\Framework\Registry::class
53  );
54  }
55 
61  public function getAvailableViewMode()
62  {
63  $value = $this->scopeConfig->getValue(
64  self::XML_PATH_LIST_MODE,
66  );
67  switch ($value) {
68  case 'grid':
69  $availableMode = ['grid' => __('Grid')];
70  break;
71 
72  case 'list':
73  $availableMode = ['list' => __('List')];
74  break;
75 
76  case 'grid-list':
77  $availableMode = ['grid' => __('Grid'), 'list' => __('List')];
78  break;
79 
80  case 'list-grid':
81  $availableMode = ['list' => __('List'), 'grid' => __('Grid')];
82  break;
83  default:
84  $availableMode = null;
85  break;
86  }
87  return $availableMode;
88  }
89 
96  public function getDefaultViewMode($options = [])
97  {
98  if (empty($options)) {
99  $options = $this->getAvailableViewMode();
100  }
101  return current(array_keys($options));
102  }
103 
109  public function getDefaultSortField()
110  {
111  $currentCategory = $this->coreRegistry->registry('current_category');
112  if ($currentCategory) {
113  return $currentCategory->getDefaultSortBy();
114  }
115 
116  return $this->scopeConfig->getValue(
117  \Magento\Catalog\Model\Config::XML_PATH_LIST_DEFAULT_SORT_BY,
119  );
120  }
121 
128  public function getAvailableLimit($mode)
129  {
130  if (!in_array($mode, [self::VIEW_MODE_GRID, self::VIEW_MODE_LIST])) {
132  }
133  $perPageConfigKey = 'catalog/frontend/' . $mode . '_per_page_values';
134  $perPageValues = (string)$this->scopeConfig->getValue(
135  $perPageConfigKey,
137  );
138  $perPageValues = explode(',', $perPageValues);
139  $perPageValues = array_combine($perPageValues, $perPageValues);
140  if ($this->scopeConfig->isSetFlag(
141  'catalog/frontend/list_allow_all',
143  )) {
144  return ($perPageValues + ['all' => __('All')]);
145  } else {
146  return $perPageValues;
147  }
148  }
149 
156  public function getDefaultLimitPerPageValue($viewMode)
157  {
158  if ($viewMode == self::VIEW_MODE_LIST) {
159  return $this->scopeConfig->getValue(
160  'catalog/frontend/list_per_page',
162  );
163  } elseif ($viewMode == self::VIEW_MODE_GRID) {
164  return $this->scopeConfig->getValue(
165  'catalog/frontend/grid_per_page',
167  );
168  }
169  return 0;
170  }
171 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Registry $coreRegistry=null)
Definition: ProductList.php:46
$value
Definition: gender.phtml:16
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15