2

di.xml

<preference for="Magento\Catalog\Controller\Adminhtml\Category\Save" type="Modulename\Complexgrd\Controller\Adminhtml\Category\Save" /> 

Save.php

<?php 
namespace Modulename\Complexgrd\Controller\Adminhtml\Category;

class Save extends \Magento\Catalog\Controller\Adminhtml\Category\Save
{

protected $storeManager;

public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
    $this->storeManager = $storeManager;
}

    public function execute()
    {


        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */


        $resultRedirect = $this->resultRedirectFactory->create();
        /*$resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);*/

        $category = $this->_initCategory();

        if (!$category) {
            return $resultRedirect->setPath('catalog/*/', ['_current' => true, 'id' => null]);
        }

        $data['general'] = $this->getRequest()->getPostValue();
        $categoryPostData = $data['general'];

        $isNewCategory = !isset($categoryPostData['entity_id']);
        $categoryPostData = $this->stringToBoolConverting($categoryPostData);
        $categoryPostData = $this->imagePreprocessing($categoryPostData);
        $categoryPostData = $this->dateTimePreprocessing($category, $categoryPostData);
        $storeId = isset($categoryPostData['store_id']) ? $categoryPostData['store_id'] : null;

        $store = $this->storeManager->getStore($storeId);
        $this->storeManager->setCurrentStore($store->getCode());

        $store->setCurrentStore($store->getCode());
        $parentId = isset($categoryPostData['parent']) ? $categoryPostData['parent'] : null;
        if ($categoryPostData) {
            $category->addData($this->_filterCategoryPostData($categoryPostData));
            if ($isNewCategory) {
                $parentCategory = $this->getParentCategory($parentId, $storeId);
                $category->setPath($parentCategory->getPath());
                $category->setParentId($parentCategory->getId());
            }

            /**
             * Process "Use Config Settings" checkboxes
             */

            $useConfig = [];
            if (isset($categoryPostData['use_config']) && !empty($categoryPostData['use_config'])) {
                foreach ($categoryPostData['use_config'] as $attributeCode => $attributeValue) {
                    if ($attributeValue) {
                        $useConfig[] = $attributeCode;
                        $category->setData($attributeCode, null);
                    }
                }
            }

            $category->setAttributeSetId($category->getDefaultAttributeSetId());

            if (isset($categoryPostData['category_products'])
                && is_string($categoryPostData['category_products'])
                && !$category->getProductsReadonly()
            ) {
                $products = json_decode($categoryPostData['category_products'], true);
                $category->setPostedProducts($products);
            }
            $this->_eventManager->dispatch(
                'catalog_category_prepare_save',
                ['category' => $category, 'request' => $this->getRequest()]
            );

            /**
             * Check "Use Default Value" checkboxes values
             */
            if (isset($categoryPostData['use_default']) && !empty($categoryPostData['use_default'])) {
                foreach ($categoryPostData['use_default'] as $attributeCode => $attributeValue) {
                    if ($attributeValue) {
                        $category->setData($attributeCode, null);
                    }
                }
            }

            /**
             * Proceed with $_POST['use_config']
             * set into category model for processing through validation
             */
            $category->setData('use_post_data_config', $useConfig);

            try {
                $categoryResource = $category->getResource();
                if ($category->hasCustomDesignTo()) {
                    $categoryResource->getAttribute('custom_design_from')->setMaxValue($category->getCustomDesignTo());
                }

                $validate = $category->validate();
                if ($validate !== true) {
                    foreach ($validate as $code => $error) {
                        if ($error === true) {
                            $attribute = $categoryResource->getAttribute($code)->getFrontend()->getLabel();
                            throw new \Magento\Framework\Exception\LocalizedException(
                                __('Attribute "%1" is required.', $attribute)
                            );
                        } else {
                            throw new \Exception($error);
                        }
                    }
                }

                $category->unsetData('use_post_data_config');

                $category->save();
                $this->messageManager->addSuccess(__('You saved the category.'));
            } catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
                $this->messageManager->addError($e->getMessage());
                $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
                $this->_getSession()->setCategoryData($categoryPostData);
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
                $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
                $this->_getSession()->setCategoryData($categoryPostData);
            } catch (\Exception $e) {
                $this->messageManager->addError(__('Something went wrong while saving the category.'.$e->getMessage()));
                $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
                $this->_getSession()->setCategoryData($categoryPostData);
            }
        }
    }

}

Getting this error:

Fatal error: Uncaught Error: Call to a member function isLoggedIn() on null in 
/var/www/html/M2/vendor/magento/module-backend/App/AbstractAction.php:252 Stack trace: #0 
/var/www/html/M2/vendor/magento/module-backend/App/AbstractAction.php(205): Magento\Backend\App\AbstractAction->_processUrlKeys() #1 
/var/www/html/M2/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Backend\App\AbstractAction->dispatch(Object(Magento\Framework\App\Request\Http)) #2 
/var/www/html/M2/vendor/magento/framework/Interception/Chain/Chain.php(70): Modulename\Complexgrd\Controller\Adminhtml\Category\Save\Interceptor->___callParent('dispatch', Array) #3 
/var/www/html/M2/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Modulename\\Comple...', 'dispatch', Object(Modulename\Complexgrd\Controller\Adminhtml\Category\Save\Interceptor), Array, 'adminAuthentica...') #4 
/var/www/html/M2/vendor/magento/module-backend/ in 
/var/www/html/M2/vendor/magento/module-backend/App/AbstractAction.php on line 252

If any one idea please share and help me as soon as

  • did you tried to call that isLoggedIn function? – kunj Oct 17 '18 at 12:21
  • no i haven't call – Jimit Bhavsar Oct 17 '18 at 12:32
  • Maybe any other module trying to call that method or by mistake someone has created di.xml under etc/ folder. – kunj Oct 17 '18 at 12:58
  • di.xml is in adminhtml folder – Jimit Bhavsar Oct 17 '18 at 13:26
  • is there any other module? – kunj Oct 17 '18 at 13:35
2

Change your constructor with below code:

public function __construct(\Magento\Backend\App\Action\Context $context, 
    \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, 
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, 
    \Magento\Framework\View\LayoutFactory $layoutFactory, 
    \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter, StoreManagerInterface $storeManager, 
    \Magento\Store\Model\StoreManagerInterface $storeManager
    \Magento\Eav\Model\Config $eavConfig = null)
   {
       $this->storeManager = $storeManager;
       parent::__construct($context, $resultRawFactory, $resultJsonFactory, $layoutFactory, $dateFilter, $storeManager, $eavConfig);
   }
  • @JimitBhavsar please accept answer if it works. – Ashish Viradiya Mar 13 at 7:11
1

If you need to declare __construct() method in your Admin controller then you should call

public function __construct(
    \Magento\Backend\App\Action\Context $context,
    ...
) {

    parent::__construct($context);
    ...
}

In your case, you'd look at parent class \Magento\Catalog\Controller\Adminhtml\Category\Save __construct() method to see which parameters you need to pass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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