5

I am trying to replace an object using Argument Replacement but unfortunately I am unable to do so for type:object.

Every time I am getting error:

1 exception(s):
Exception #0 (Magento\Framework\Exception\RuntimeException): Type 
Error occurred when creating object: Narendra\VirtualType\Controller\Index\Index\Interceptor

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Narendra\VirtualType\Controller\Index\Index\Interceptor
#1 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Narendra\Virtual...', array()) called at [vendor/magento/framework/ObjectManager/ObjectManager.php:56]
#2 Magento\Framework\ObjectManager\ObjectManager->create('Narendra\Virtual...') called at [vendor/magento/framework/App/ActionFactory.php:44]
#3 Magento\Framework\App\ActionFactory->create('Narendra\Virtual...') called at [vendor/magento/framework/App/Router/Base.php:306]
#4 Magento\Framework\App\Router\Base->matchAction(&Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#, array('moduleFrontName' => 'vt', 'actionPath' => 'index', 'actionName' => 'index')) called at [vendor/magento/framework/App/Router/Base.php:167]
#5 Magento\Framework\App\Router\Base->match(&Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#) called at [vendor/magento/framework/App/FrontController.php:95]
#6 Magento\Framework\App\FrontController->dispatch(&Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#) called at [vendor/magento/framework/Interception/Interceptor.php:58]
#7 Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', array(&Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#)) called at [vendor/magento/framework/Interception/Interceptor.php:138]
#8 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#) called at [vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php:94]
#9 Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(&Magento\Framework\App\FrontController\Interceptor#000000004565ee820000000078c8f6e1#, &Closure#000000004565ecd20000000078c8f6e1#, &Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#) called at [vendor/magento/framework/Interception/Interceptor.php:135]
#10 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#) called at [vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php:73]
#11 Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(&Magento\Framework\App\FrontController\Interceptor#000000004565ee820000000078c8f6e1#, &Closure#000000004565ecd20000000078c8f6e1#, &Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#) called at [vendor/magento/framework/Interception/Interceptor.php:135]
#12 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#) called at [vendor/magento/framework/Interception/Interceptor.php:153]
#13 Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', array(&Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#), NULL) called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:26]
#14 Magento\Framework\App\FrontController\Interceptor->dispatch(&Magento\Framework\App\Request\Http#000000004565e8fa0000000078c8f6e1#) called at [vendor/magento/framework/App/Http.php:137]
#15 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:24]
#16 Magento\Framework\App\Http\Interceptor->launch() called at [vendor/magento/framework/App/Bootstrap.php:261]
#17 Magento\Framework\App\Bootstrap->run(&Magento\Framework\App\Http\Interceptor#000000004565ee0a0000000078c8f6e1#) called at [index.php:47]

I tried replacing the argument for type string and it worked perfectly for me.

What I have done is:

Created a controller Index:

public $argument1;

public $scaler1;

public function __construct(Context $context, BlockRepositoryInterface $argument1, $scaler1='narendra')
{
    $this->argument1 = $argument1;
    $this->scaler1 = $scaler1;
    parent::__construct($context);
}

public function execute()
{
    echo $this->scaler1;exit;
}

app/code/Narendra/VirtualType/etc/di.xml

This does not works

<type name="Narendra\VirtualType\Controller\Index\Index">
    <arguments>
        <argument name="argument1" xsi:type="object">Magento\Catalog\Api\ProductRepositoryInterface</argument>
    </arguments>
</type>

This works for me

<type name="Narendra\VirtualType\Controller\Index\Index">
    <arguments>
        <argument name="scaler1" xsi:type="string">from di xml file</argument>
    </arguments>
</type>
  • Can you edit the question add the whole exception @Narendra – Prachi Saxena Sep 17 at 7:09
  • @PrachiSaxena: I have updated my question and have added whole error, please have a look – Narendra Vyas Sep 17 at 7:20
  • Did you tried removing content of generated folder and check if the error is still there? – Prachi Saxena Sep 17 at 7:25
  • yes i removed it and tried but not sure why error keeps on populating. rm -rf generated/* && bin/magento c:f – Narendra Vyas Sep 17 at 7:27
6

When you trying to replace an argument type of a function, then you must remember that the replaced type class should also able to create the old argument type object.

Lets me give an example:

On your question, your argument1's type is Magento\Cms\Api\BlockRepositoryInterface.

So, you must have to use those interfaces Or Classes which extend Magento\Cms\Api\BlockRepositoryInterface as replace the type of argument1.

like: Magento\Cms\Model\BlockRepository which implement Magento\Cms\Api\BlockRepositoryInterface

But you have to try to use Magento\Catalog\Api\ProductRepositoryInterface Argument Replacement of argument1.That's why you are getting the error.

So, replace types is important during the implementing of type argument.

At Second case, scaler1 is the type of string and replace type also string. So it is working.

  • Seems as if you are correct. But I am still wondering what are the classes in argument that we can replace and what are that we cant. I changed di.xml like this and it worked for me without any error: <type name="Narendra\VirtualType\Controller\Index\Index"> <arguments> <argument name="argument1" xsi:type="object">Magento\Cms\Model\BlockRepository</argument> </arguments> </type> – Narendra Vyas Sep 18 at 0:23
  • I have created this module only to make understanding on Argument Replacement. I am thinking that any argument in the constructor class can be replaced with any different class. If you can provide me any reference link from where i may get better insight i would really apericiate. tnks – Narendra Vyas Sep 18 at 0:25
2

Can you please try in the following format this example is similar to yours and working for me:

<type name="Amasty\ShopbyBase\Model\UrlBuilder">
    <arguments>
        <argument name="urlAdapters" xsi:type="array">
            <item name="base" xsi:type="array">
                <item name="adapter" xsi:type="object">\Amasty\ShopbyBase\Model\UrlBuilder\Adapter</item>
                <item name="sort_order" xsi:type="string">100</item>
            </item>
        </argument>
    </arguments>
</type>
  • hmmm, not sure if that is going to work as the argument that I want to replace is not an array. But will give a try – Narendra Vyas Sep 17 at 7:21
  • you have to modify it as per your implementation I am not saying copy paste will work. But structure will be like above. – Hassan Ali Shahzad Sep 17 at 7:24

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.