Argument replacement not working for type object
-
13-03-2021 - |
Pregunta
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>
Solución
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.
Otros consejos
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>