Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
View.php
Go to the documentation of this file.
1 <?php
8 
16 
23 {
29  protected $_coreRegistry = null;
30 
36  protected $_catalogSession;
37 
43  protected $_catalogDesign;
44 
48  protected $_storeManager;
49 
54 
58  protected $resultPageFactory;
59 
64 
70  private $layerResolver;
71 
76 
92  public function __construct(
93  \Magento\Framework\App\Action\Context $context,
94  \Magento\Catalog\Model\Design $catalogDesign,
95  \Magento\Catalog\Model\Session $catalogSession,
96  \Magento\Framework\Registry $coreRegistry,
97  \Magento\Store\Model\StoreManagerInterface $storeManager,
98  \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
100  \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
101  Resolver $layerResolver,
103  ) {
104  parent::__construct($context);
105  $this->_storeManager = $storeManager;
106  $this->_catalogDesign = $catalogDesign;
107  $this->_catalogSession = $catalogSession;
108  $this->_coreRegistry = $coreRegistry;
109  $this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
110  $this->resultPageFactory = $resultPageFactory;
111  $this->resultForwardFactory = $resultForwardFactory;
112  $this->layerResolver = $layerResolver;
113  $this->categoryRepository = $categoryRepository;
114  }
115 
121  protected function _initCategory()
122  {
123  $categoryId = (int)$this->getRequest()->getParam('id', false);
124  if (!$categoryId) {
125  return false;
126  }
127 
128  try {
129  $category = $this->categoryRepository->get($categoryId, $this->_storeManager->getStore()->getId());
130  } catch (NoSuchEntityException $e) {
131  return false;
132  }
133  if (!$this->_objectManager->get(\Magento\Catalog\Helper\Category::class)->canShow($category)) {
134  return false;
135  }
136  $this->_catalogSession->setLastVisitedCategoryId($category->getId());
137  $this->_coreRegistry->register('current_category', $category);
138  try {
139  $this->_eventManager->dispatch(
140  'catalog_controller_category_init_after',
141  ['category' => $category, 'controller_action' => $this]
142  );
143  } catch (\Magento\Framework\Exception\LocalizedException $e) {
144  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
145  return false;
146  }
147 
148  return $category;
149  }
150 
158  public function execute()
159  {
160  if ($this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED)) {
161  return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
162  }
163  $category = $this->_initCategory();
164  if ($category) {
165  $this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
166  $settings = $this->_catalogDesign->getDesignSettings($category);
167 
168  // apply custom design
169  if ($settings->getCustomDesign()) {
170  $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
171  }
172 
173  $this->_catalogSession->setLastViewedCategoryId($category->getId());
174 
175  $page = $this->resultPageFactory->create();
176  // apply custom layout (page) template once the blocks are generated
177  if ($settings->getPageLayout()) {
178  $page->getConfig()->setPageLayout($settings->getPageLayout());
179  }
180 
181  $hasChildren = $category->hasChildren();
182  if ($category->getIsAnchor()) {
183  $type = $hasChildren ? 'layered' : 'layered_without_children';
184  } else {
185  $type = $hasChildren ? 'default' : 'default_without_children';
186  }
187 
188  if (!$hasChildren) {
189  // Two levels removed from parent. Need to add default page type.
190  $parentType = strtok($type, '_');
191  $page->addPageLayoutHandles(['type' => $parentType], null, false);
192  }
193  $page->addPageLayoutHandles(['type' => $type], null, false);
194  $page->addPageLayoutHandles(['id' => $category->getId()]);
195 
196  // apply custom layout update once layout is loaded
197  $layoutUpdates = $settings->getLayoutUpdates();
198  if ($layoutUpdates && is_array($layoutUpdates)) {
199  foreach ($layoutUpdates as $layoutUpdate) {
200  $page->addUpdate($layoutUpdate);
201  $page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)], null, false);
202  }
203  }
204 
205  $page->getConfig()->addBodyClass('page-products')
206  ->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
207  ->addBodyClass('category-' . $category->getUrlKey());
208 
209  return $page;
210  } elseif (!$this->getResponse()->isRedirect()) {
211  return $this->resultForwardFactory->create()->forward('noroute');
212  }
213  }
214 }
_redirect($path, $arguments=[])
Definition: Action.php:167
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$storeManager
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Catalog\Model\Design $catalogDesign, \Magento\Catalog\Model\Session $catalogSession, \Magento\Framework\Registry $coreRegistry, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator, PageFactory $resultPageFactory, \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory, Resolver $layerResolver, CategoryRepositoryInterface $categoryRepository)
Definition: View.php:92
$type
Definition: item.phtml:13
$page
Definition: pages.php:8
$settings
Definition: bootstrap.php:29