1

Magento 2.2.2

Following is the error I am getting:

I am getting following error on compilation with MGS Store Locator extension:

Errors during compilation:
    MGS\StoreLocator\Controller\Adminhtml\Locator\Save
            Incompatible argument type: Required type: \Magento\Backend\App\Action\Context. Actual type: \Magento\Framework\App\Action\Context; File:
  /home/nginx/domains/mydomain.com/public/app/code/MGS/StoreLocator/Controller/Adminhtml/Locator/Save.php

    MGS\StoreLocator\Controller\Adminhtml\Locator\Edit
            Incompatible argument type: Required type: \Magento\Backend\App\Action\Context. Actual type: \Magento\Framework\App\Action\Context; File:/home/nginx/domains/mydomain.com/public/app/code/MGS/StoreLocator/Controller/Adminhtml/Locator/Edit.php

/home/nginx/domains/mydomain.com/public/app/code/MGS/StoreLocator/Controller/Adminhtml/Locator/Save.php

            namespace MGS\StoreLocator\Controller\Adminhtml\Locator;

            use MGS\StoreLocator\Model\StoreFactory;
            use Magento\Framework\App\Action\Context;
            use Magento\Framework\App\Filesystem\DirectoryList;

            class Save extends \Magento\Backend\App\Action {

                protected $_storeFactory;
                protected $_coreRegistry;
                protected $_storeManager;
                protected $_filesystem;
                protected $_fileUploaderFactory;

                public function __construct(Context $context, 
                        StoreFactory $storeFactory, 
                        \Magento\Framework\Registry $coreRegistry, 
                        \Magento\Store\Model\StoreManagerInterface $storeManager,
                        \Magento\Framework\Filesystem $filesystem,
                        \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory) {
                    parent::__construct($context);
                    $this->_storeFactory = $storeFactory;
                    $this->_coreRegistry = $coreRegistry;
                    $this->_storeManager = $storeManager;
                    $this->_filesystem = $filesystem;
                    $this->_fileUploaderFactory = $fileUploaderFactory;
                }

                /**
                 * @return void
                 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
                 * @SuppressWarnings(PHPMD.NPathComplexity)
                 */
                public function execute() {
                    $data = $this->getRequest()->getPostValue();
                    if (!$data) {
                        $this->_redirect('locator/*/');
                        return;
                    }

                    $model = $this->_objectManager->create('MGS\StoreLocator\Model\Store');
                    if (!empty($data['id'])) {
                        $model->load($data['id']);
                    }
                    if (!empty($data['id']) && $model->isObjectNew()) {
                        $this->messageManager->addError(__('This Locator no longer exists.'));
                        $this->_redirect('locator/*/');
                        return;
                    }

                    // Store logo Image upload
                    if(isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') {
                        /** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
                        $uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);
                        $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
                        $uploader->setAllowRenameFiles(true);
                        $uploader->setAllowCreateFolders(true);
                        $uploader->setFilesDispersion(true);
                        $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
                        if($uploader->checkAllowedExtension($ext)) {
                            $path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)
                                    ->getAbsolutePath('mgs_storelocator/');
                            $uploader->save($path);

                            $fileName = $uploader->getUploadedFileName();
                            if ($fileName) {
                                $data['image'] = 'mgs_storelocator'.$fileName;
                            }
                        } else {
                            $this->messageManager->addError(__('Disallowed file type.'));
                            return $this->redirectToEdit($model, $data);
                        }
                    } else {
                        if(isset($data['image']['delete']) && $data['image']['delete'] == 1) {
                            $data['image'] = '';
                        } else {
                            unset($data['image']);
                        }
                    }

                    $model->setData($data);
                    try {
                        $model->setStoreIds($this->getRequest()->getParam('stores', []));
                        $model->save();
                        $this->messageManager->addSuccess(__('You saved the store locator.'));
                        $this->_getSession()->setLocator(false);
                        $back = $this->getRequest()->getParam('back', false);
                        if($back == 'edit') {
                            return $this->_redirect('locator/*/edit', ['id' => $model->getId(), '_current' => true, 'active_tab' => '']);
                        }
                        $this->_redirect('locator/*/');
                    } catch (\Exception $e) {
                        $messages = $e->getMessages();
                        $this->messageManager->addMessages($messages);
                        $this->redirectToEdit($model, $data);
                    }
                }

                /**
                 * @param MGS\StoreLocator\Model\StoreFactory $model
                 * @param array $data
                 * @return void
                 */
                protected function redirectToEdit($model, array $data) {
                    $this->_getSession()->setLocator($data);
                    $arguments = $model->getId() ? ['id' => $model->getId()] : [];
                    $arguments = array_merge($arguments, ['_current' => true, 'active_tab' => '']);
                    $this->_redirect('locator/*/edit', $arguments);
                }

            }

/home/nginx/domains/mydomain.com/public/app/code/MGS/StoreLocator/Controller/Adminhtml/Locator/Edit.php

          /**
             *
             * Copyright © 2015 Magento. All rights reserved.
             * See COPYING.txt for license details.
             */

            namespace MGS\StoreLocator\Controller\Adminhtml\Locator;

            use MGS\StoreLocator\Model\StoreFactory;
            use Magento\Framework\App\Action\Context;

            class Edit extends \Magento\Backend\App\Action {

                protected $_storeFactory;
                protected $_coreRegistry;

                public function __construct(Context $context, StoreFactory $storeFactory, \Magento\Framework\Registry $coreRegistry) {
                    parent::__construct($context);
                    $this->_storeFactory = $storeFactory;
                    $this->_coreRegistry = $coreRegistry;
                }

                /**
                 * @return $this
                 */
                protected function _initAction()
                {
                    $this->_view->loadLayout();
                    $this->_addBreadcrumb(
                        __('Store Locator'),
                        __('Store Locator')
                    )->_addBreadcrumb(
                        __('Edit'),
                        __('Edit')
                    );
                    return $this;
                }

                /**
                 * @return void
                 */
                public function execute() {
                    $storeId = $this->getRequest()->getParam('id');
                    /** @var \Magento\User\Model\User $model */
                    $model = $this->_storeFactory->create();

                    if ($storeId) {
                        $model->load($storeId);
                        if (!$model->getId()) {
                            $this->messageManager->addError(__('This Locator no longer exists.'));
                            $this->_redirect('adminhtml/*/');
                            return;
                        }
                    }
                    // Restore previously entered form data from session
                    $data = $this->_session->getLocator(true);
                    if (!empty($data)) {
                        $model->setData($data);
                    }
                    $this->_coreRegistry->register('locator', $model);
                    if (isset($storeId)) {
                        $breadcrumb = __('Edit Store');
                    } else {
                        $breadcrumb = __('New Store');
                    }
                    $this->_initAction()->_addBreadcrumb($breadcrumb, $breadcrumb);
                    $this->_view->getPage()->getConfig()->getTitle()->prepend(__('Stores'));
                    $this->_view->getPage()->getConfig()->getTitle()->prepend($model->getId() ? __("Edit Store '%1'", $model->getName()) : __('New Store'));
                    $this->_view->renderLayout();
                }

            }

Can anyone please tell where is the issue?

  • Check the answer, Afaq – Raul Sanchez Apr 25 '18 at 16:02
1

Error says you are injecting the wrong classes in constructor

Incompatible argument type: Required type: \Magento\Backend\App\Action\Context. Actual type: \Magento\Framework\App\Action\Context; File:

Replace

use Magento\Framework\App\Action\Context;

With

use Magento\Backend\App\Action\Context;

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.