0

I grab category object from observer on event catalog_controller_category_init_after. Should I check it against store id? Or the fact that its loaded in controller first and then passed to event (and observer) makes it valid against the scope, as well as its data?

1 Answer 1

1

I've searched in module-catalog/Controller/Category/View.php and I saw that the method _initCategory() loads the category from the current store $category = $this->categoryRepository->get($categoryId, $this->_storeManager->getStore()->getId()); and after that is dispatching your catalog_controller_category_init_after event. So, no worries. You don't need to check it against store id.

protected function _initCategory()
    {
        $categoryId = (int)$this->getRequest()->getParam('id', false);
        if (!$categoryId) {
            return false;
        }

        try {
            $category = $this->categoryRepository->get($categoryId, $this->_storeManager->getStore()->getId());
        } catch (NoSuchEntityException $e) {
            return false;
        }
        if (!$this->_objectManager->get('Magento\Catalog\Helper\Category')->canShow($category)) {
            return false;
        }
        $this->_catalogSession->setLastVisitedCategoryId($category->getId());
        $this->_coreRegistry->register('current_category', $category);
        try {
            $this->_eventManager->dispatch(
                'catalog_controller_category_init_after',
                ['category' => $category, 'controller_action' => $this]
            );
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
            return false;
        }

        return $category;
    }
1
  • can you mark the answer accordingly ? thx Commented Mar 10, 2017 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.