Example #1
0
 /**
  * Category view action
  *
  * @return void
  */
 public function execute()
 {
     if ($this->_request->getParam(\Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED)) {
         $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
         return;
     }
     $category = $this->_initCategory();
     if ($category) {
         $settings = $this->_catalogDesign->getDesignSettings($category);
         // apply custom design
         if ($settings->getCustomDesign()) {
             $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
         }
         $this->_catalogSession->setLastViewedCategoryId($category->getId());
         $update = $this->_view->getLayout()->getUpdate();
         $update->addHandle('default');
         if ($category->getIsAnchor()) {
             $type = $category->hasChildren() ? 'layered' : 'layered_without_children';
         } else {
             $type = $category->hasChildren() ? 'default' : 'default_without_children';
         }
         if (!$category->hasChildren()) {
             // Two levels removed from parent.  Need to add default page type.
             $parentType = strtok($type, '_');
             $this->_view->addPageLayoutHandles(array('type' => $parentType));
         }
         $this->_view->addPageLayoutHandles(array('type' => $type, 'id' => $category->getId()));
         // apply custom layout (page) template once the blocks are generated
         if ($settings->getPageLayout()) {
             $this->_objectManager->get('Magento\\Theme\\Helper\\Layout')->applyHandle($settings->getPageLayout());
         }
         $this->_view->loadLayoutUpdates();
         // apply custom layout update once layout is loaded
         $layoutUpdates = $settings->getLayoutUpdates();
         if ($layoutUpdates && is_array($layoutUpdates)) {
             foreach ($layoutUpdates as $layoutUpdate) {
                 $update->addUpdate($layoutUpdate);
             }
         }
         $this->_view->generateLayoutXml();
         $this->_view->generateLayoutBlocks();
         $root = $this->_view->getLayout()->getBlock('root');
         if ($root) {
             $root->addBodyClass('categorypath-' . $category->getUrlPath())->addBodyClass('category-' . $category->getUrlKey());
         }
         $this->_view->getLayout()->initMessages();
         $this->_view->renderLayout();
     } elseif (!$this->getResponse()->isRedirect()) {
         $this->_forward('noroute');
     }
 }
Example #2
0
 /**
  * Category view action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     if ($this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED)) {
         return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
     }
     $category = $this->_initCategory();
     if ($category) {
         $this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
         $settings = $this->_catalogDesign->getDesignSettings($category);
         // apply custom design
         if ($settings->getCustomDesign()) {
             $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
         }
         $this->_catalogSession->setLastViewedCategoryId($category->getId());
         $page = $this->resultPageFactory->create();
         // apply custom layout (page) template once the blocks are generated
         if ($settings->getPageLayout()) {
             $page->getConfig()->setPageLayout($settings->getPageLayout());
         }
         $hasChildren = $category->hasChildren();
         if ($category->getIsAnchor()) {
             $type = $hasChildren ? 'layered' : 'layered_without_children';
         } else {
             $type = $hasChildren ? 'default' : 'default_without_children';
         }
         if (!$hasChildren) {
             // Two levels removed from parent.  Need to add default page type.
             $parentType = strtok($type, '_');
             $page->addPageLayoutHandles(['type' => $parentType]);
         }
         $page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]);
         // apply custom layout update once layout is loaded
         $layoutUpdates = $settings->getLayoutUpdates();
         if ($layoutUpdates && is_array($layoutUpdates)) {
             foreach ($layoutUpdates as $layoutUpdate) {
                 $page->addUpdate($layoutUpdate);
                 $page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)]);
             }
         }
         $page->getConfig()->addBodyClass('page-products')->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))->addBodyClass('category-' . $category->getUrlKey());
         return $page;
     } elseif (!$this->getResponse()->isRedirect()) {
         return $this->resultForwardFactory->create()->forward('noroute');
     }
 }