1

I overridden the \Magento\Customer\Controller\Account\CreatePost controller using preference in di.xml and want to add a collection using factory method but always I get

Type Error occurred when creating object

Even I removed the generated folder and done di compile still facing same issue.

My di.xml

<preference for="Magento\Customer\Controller\Account\CreatePost" type="Vendor\Modulename\Override\Account\CreatePost"/>

My Controller.

    <?php

        namespace Vendor\Modulename\Override\Account;

        use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
        use Magento\Customer\Model\Account\Redirect as AccountRedirect;
        use Magento\Customer\Api\Data\AddressInterface;
        use Magento\Framework\Api\DataObjectHelper;
        use Magento\Framework\App\Action\Context;
        use Magento\Customer\Model\Session;
        use Magento\Framework\App\Config\ScopeConfigInterface;
        use Magento\Framework\App\CsrfAwareActionInterface;
        use Magento\Framework\App\ObjectManager;
        use Magento\Framework\App\Request\InvalidRequestException;
        use Magento\Framework\App\RequestInterface;
        use Magento\Framework\Controller\Result\Redirect;
        use Magento\Framework\Exception\LocalizedException;
        use Magento\Framework\Phrase;
        use Magento\Store\Model\StoreManagerInterface;
        use Magento\Customer\Api\AccountManagementInterface;
        use Magento\Customer\Helper\Address;
        use Magento\Framework\UrlFactory;
        use Magento\Customer\Model\Metadata\FormFactory;
        use Magento\Newsletter\Model\SubscriberFactory;
        use Magento\Customer\Api\Data\RegionInterfaceFactory;
        use Magento\Customer\Api\Data\AddressInterfaceFactory;
        use Magento\Customer\Api\Data\CustomerInterfaceFactory;
        use Magento\Customer\Model\Url as CustomerUrl;
        use Magento\Customer\Model\Registration;
        use Magento\Framework\Escaper;
        use Magento\Customer\Model\CustomerExtractor;
        use Magento\Framework\Exception\StateException;
        use Magento\Framework\Exception\InputException;
        use Magento\Framework\Data\Form\FormKey\Validator;
        use Magento\Customer\Controller\AbstractAccount;

        class CreatePost extends \Magento\Customer\Controller\Account\CreatePost
{
    /**
     * @var \Magento\Customer\Api\AccountManagementInterface
     */
    protected $accountManagement;

    /**
     * @var \Magento\Customer\Helper\Address
     */
    protected $addressHelper;

    /**
     * @var \Magento\Customer\Model\Metadata\FormFactory
     */
    protected $formFactory;

    /**
     * @var \Magento\Newsletter\Model\SubscriberFactory
     */
    protected $subscriberFactory;

    /**
     * @var \Magento\Customer\Api\Data\RegionInterfaceFactory
     */
    protected $regionDataFactory;

    /**
     * @var \Magento\Customer\Api\Data\AddressInterfaceFactory
     */
    protected $addressDataFactory;

    /**
     * @var \Magento\Customer\Model\Registration
     */
    protected $registration;

    /**
     * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
     */
    protected $customerDataFactory;

    /**
     * @var \Magento\Customer\Model\Url
     */
    protected $customerUrl;

    /**
     * @var \Magento\Framework\Escaper
     */
    protected $escaper;

    /**
     * @var \Magento\Customer\Model\CustomerExtractor
     */
    protected $customerExtractor;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlModel;

    /**
     * @var \Magento\Framework\Api\DataObjectHelper
     */
    protected $dataObjectHelper;

    /**
     * @var Session
     */
    protected $session;

    /**
     * @var AccountRedirect
     */
    private $accountRedirect;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
     */
    private $cookieMetadataFactory;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager
     */
    private $cookieMetadataManager;

    /**
     * @var Validator
     */
    private $formKeyValidator;

    /**
     * @param Context $context
     * @param Session $customerSession
     * @param ScopeConfigInterface $scopeConfig
     * @param StoreManagerInterface $storeManager
     * @param AccountManagementInterface $accountManagement
     * @param Address $addressHelper
     * @param UrlFactory $urlFactory
     * @param FormFactory $formFactory
     * @param SubscriberFactory $subscriberFactory
     * @param RegionInterfaceFactory $regionDataFactory
     * @param AddressInterfaceFactory $addressDataFactory
     * @param CustomerInterfaceFactory $customerDataFactory
     * @param CustomerUrl $customerUrl
     * @param Registration $registration
     * @param Escaper $escaper
     * @param CustomerExtractor $customerExtractor
     * @param DataObjectHelper $dataObjectHelper
     * @param AccountRedirect $accountRedirect
     * @param Validator $formKeyValidator
     *
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        Context $context,
        Session $customerSession,
        ScopeConfigInterface $scopeConfig,
        StoreManagerInterface $storeManager,
        AccountManagementInterface $accountManagement,
        Address $addressHelper,
        FormFactory $formFactory,
        SubscriberFactory $subscriberFactory,
        RegionInterfaceFactory $regionDataFactory,
        AddressInterfaceFactory $addressDataFactory,
        CustomerInterfaceFactory $customerDataFactory,
        CustomerUrl $customerUrl,
        Registration $registration,
        Escaper $escaper,
        CustomerExtractor $customerExtractor,
        DataObjectHelper $dataObjectHelper,
        AccountRedirect $accountRedirect,
        Validator $formKeyValidator = null,
        UrlFactory $urlFactory
    ) {
        $this->session = $customerSession;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->accountManagement = $accountManagement;
        $this->addressHelper = $addressHelper;
        $this->formFactory = $formFactory;
        $this->subscriberFactory = $subscriberFactory;
        $this->regionDataFactory = $regionDataFactory;
        $this->addressDataFactory = $addressDataFactory;
        $this->customerDataFactory = $customerDataFactory;
        $this->customerUrl = $customerUrl;
        $this->registration = $registration;
        $this->escaper = $escaper;
        $this->customerExtractor = $customerExtractor;
        $this->urlModel = $urlFactory->create();
        $this->dataObjectHelper = $dataObjectHelper;
        $this->accountRedirect = $accountRedirect;
        $this->formKeyValidator = $formKeyValidator ?: ObjectManager::getInstance()->get(Validator::class);
        parent::__construct($context, $customerSession, $scopeConfig, $storeManager, $accountManagement, $addressHelper, $formFactory, $subscriberFactory, $regionDataFactory, $addressDataFactory, $customerDataFactory, $customerUrl, $registration, $escaper, $customerExtractor, $dataObjectHelper, $accountRedirect, $formKeyValidator=null, $urlFactory->create());
    }

            /**
             * Retrieve cookie manager
             *
             * @deprecated 100.1.0
             * @return \Magento\Framework\Stdlib\Cookie\PhpCookieManager
             */
            private function getCookieManager()
            {
                if (!$this->cookieMetadataManager) {
                    $this->cookieMetadataManager = ObjectManager::getInstance()->get(
                        \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class
                    );
                }
                return $this->cookieMetadataManager;
            }

            /**
             * Retrieve cookie metadata factory
             *
             * @deprecated 100.1.0
             * @return \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
             */
            private function getCookieMetadataFactory()
            {
                if (!$this->cookieMetadataFactory) {
                    $this->cookieMetadataFactory = ObjectManager::getInstance()->get(
                        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
                    );
                }
                return $this->cookieMetadataFactory;
            }

            /**
             * Add address to customer during create account
             *
             * @return AddressInterface|null
             */
            protected function extractAddress()
            {
                if (!$this->getRequest()->getPost('create_address')) {
                    return null;
                }

                $addressForm = $this->formFactory->create('customer_address', 'customer_register_address');
                $allowedAttributes = $addressForm->getAllowedAttributes();

                $addressData = [];

                $regionDataObject = $this->regionDataFactory->create();
                foreach ($allowedAttributes as $attribute) {
                    $attributeCode = $attribute->getAttributeCode();
                    $value = $this->getRequest()->getParam($attributeCode);
                    if ($value === null) {
                        continue;
                    }
                    switch ($attributeCode) {
                        case 'region_id':
                            $regionDataObject->setRegionId($value);
                            break;
                        case 'region':
                            $regionDataObject->setRegion($value);
                            break;
                        default:
                            $addressData[$attributeCode] = $value;
                    }
                }
                $addressDataObject = $this->addressDataFactory->create();
                $this->dataObjectHelper->populateWithArray(
                    $addressDataObject,
                    $addressData,
                    \Magento\Customer\Api\Data\AddressInterface::class
                );
                $addressDataObject->setRegion($regionDataObject);

                $addressDataObject->setIsDefaultBilling(
                    $this->getRequest()->getParam('default_billing', false)
                )->setIsDefaultShipping(
                    $this->getRequest()->getParam('default_shipping', false)
                );
                return $addressDataObject;
            }

            /**
             * @inheritDoc
             */
            public function createCsrfValidationException(
                RequestInterface $request
            ): ?InvalidRequestException {
                /** @var Redirect $resultRedirect */
                $resultRedirect = $this->resultRedirectFactory->create();
                $url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
                $resultRedirect->setUrl($this->_redirect->error($url));

                return new InvalidRequestException(
                    $resultRedirect,
                    [new Phrase('Invalid Form Key. Please refresh the page.')]
                );
            }

            /**
             * @inheritDoc
             */
            public function validateForCsrf(RequestInterface $request): ?bool
            {
                return null;
            }

            /**
             * Create customer account action
             *
             * @return void
             * @SuppressWarnings(PHPMD.CyclomaticComplexity)
             * @SuppressWarnings(PHPMD.NPathComplexity)
             */
            public function execute()
            {
    }
    }
  • How did you added collection? Can you please share your code? – Ranganathan Dec 27 '19 at 11:32
  • Atleast a helper is fine for me. I added that collection in helper I just need to call that helper in constructor. – Rising Dec 27 '19 at 11:34
  • Can you add your di.xml and controller code with file path? – Sukumar Gorai Dec 27 '19 at 11:36
  • If you want to add your collection in constructor. You should call parent constructor with required params. – Ranganathan Dec 27 '19 at 11:37
  • I will check the code which you sent and get back to you. – Rising Dec 27 '19 at 11:45
2

Can you please update __construct code and use below code format

public function __construct(
    Context $context,
    Session $customerSession,
    ScopeConfigInterface $scopeConfig,
    StoreManagerInterface $storeManager,
    AccountManagementInterface $accountManagement,
    Address $addressHelper,
    UrlFactory $urlFactory,
    FormFactory $formFactory,
    SubscriberFactory $subscriberFactory,
    RegionInterfaceFactory $regionDataFactory,
    AddressInterfaceFactory $addressDataFactory,
    CustomerInterfaceFactory $customerDataFactory,
    CustomerUrl $customerUrl,
    Registration $registration,
    Escaper $escaper,
    CustomerExtractor $customerExtractor,
    DataObjectHelper $dataObjectHelper,
    AccountRedirect $accountRedirect,
    Validator $formKeyValidator = null
) {
    $this->formKeyValidator = $formKeyValidator ?: ObjectManager::getInstance()->get(Validator::class);
    $this->accountRedirect = $accountRedirect;
    parent::__construct(
        $context,
        $customerSession,
        $scopeConfig,
        $storeManager,
        $accountManagement,
        $addressHelper,
        $urlFactory,
        $formFactory,
        $subscriberFactory,
        $regionDataFactory,
        $addressDataFactory,
        $customerDataFactory,
        $customerUrl,
        $registration,
        $escaper,
        $customerExtractor,
        $dataObjectHelper,
        $accountRedirect
    );
}
  • Great thanks yar. What's the mistake I done? – Rising Dec 27 '19 at 13:08
  • @Rising sound good! You have added formKeyValidator and accountRedirect in the parent construct. – Vithal Bariya Dec 27 '19 at 13:32
0

You can add your helper object by below method.

<?php
namespace Vendor\Module\Controller;

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Customer\Model\Account\Redirect as AccountRedirect;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Helper\Address;
use Magento\Framework\UrlFactory;
use Magento\Customer\Model\Metadata\FormFactory;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\Customer\Api\Data\RegionInterfaceFactory;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Customer\Model\Registration;
use Magento\Framework\Escaper;
use Magento\Customer\Model\CustomerExtractor;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Customer\Controller\AbstractAccount;

class Yourclass extends \Magento\Customer\Controller\Account\CreatePost
{
    /**
     * @var \Magento\Customer\Api\AccountManagementInterface
     */
    protected $accountManagement;

    /**
     * @var \Magento\Customer\Helper\Address
     */
    protected $addressHelper;

    /**
     * @var \Magento\Customer\Model\Metadata\FormFactory
     */
    protected $formFactory;

    /**
     * @var \Magento\Newsletter\Model\SubscriberFactory
     */
    protected $subscriberFactory;

    /**
     * @var \Magento\Customer\Api\Data\RegionInterfaceFactory
     */
    protected $regionDataFactory;

    /**
     * @var \Magento\Customer\Api\Data\AddressInterfaceFactory
     */
    protected $addressDataFactory;

    /**
     * @var \Magento\Customer\Model\Registration
     */
    protected $registration;

    /**
     * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
     */
    protected $customerDataFactory;

    /**
     * @var \Magento\Customer\Model\Url
     */
    protected $customerUrl;

    /**
     * @var \Magento\Framework\Escaper
     */
    protected $escaper;

    /**
     * @var \Magento\Customer\Model\CustomerExtractor
     */
    protected $customerExtractor;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlModel;

    /**
     * @var \Magento\Framework\Api\DataObjectHelper
     */
    protected $dataObjectHelper;

    /**
     * @var Session
     */
    protected $session;

    /**
     * @var AccountRedirect
     */
    private $accountRedirect;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
     */
    private $cookieMetadataFactory;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager
     */
    private $cookieMetadataManager;

    /**
     * @var Validator
     */
    private $formKeyValidator;

    protected $_customHelper;

    public function __construct(
        Context $context,
        Session $customerSession,
        ScopeConfigInterface $scopeConfig,
        StoreManagerInterface $storeManager,
        AccountManagementInterface $accountManagement,
        Address $addressHelper,
        UrlFactory $urlFactory,
        FormFactory $formFactory,
        SubscriberFactory $subscriberFactory,
        RegionInterfaceFactory $regionDataFactory,
        AddressInterfaceFactory $addressDataFactory,
        CustomerInterfaceFactory $customerDataFactory,
        CustomerUrl $customerUrl,
        Registration $registration,
        Escaper $escaper,
        CustomerExtractor $customerExtractor,
        DataObjectHelper $dataObjectHelper,
        AccountRedirect $accountRedirect,
        Validator $formKeyValidator = null,
        \Vendor\Module\etc\Helper $customHelper
    ) {
        parent::__construct(
            $context, 
            $customerSession, 
            $scopeConfig, 
            $storeManager, 
            $accountManagement, 
            $addressHelper, 
            $formFactory, 
            $subscriberFactory, 
            $regionDataFactory, 
            $addressDataFactory, 
            $customerDataFactory, 
            $customerUrl, 
            $registration, 
            $escaper, 
            $customerExtractor, 
            $urlFactory->create(), 
            $dataObjectHelper, 
            $accountRedirect, 
            $formKeyValidator ?: ObjectManager::getInstance()->get(Validator::class)
        );

        $this->_customHelper = $customHelper;
    }

    public function execute()
    {

    }
}

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.