1

I'm trying to understand virtual types and how we can use them in practice. Referred some code from the vendor, still, it is giving me an error when I'm trying to access the helper class.

namespace Ravi\Learn\Helper;

use Magento\Framework\App\Helper\Context;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;
    public function __construct(Context $context, $customerSession)
    {
        parent::__construct($context);
        $this->customerSession = $customerSession;
    }

    public function getSess(){
       return $this->customerSession->getId();
    }
}

Below is the code for di.xml file

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <virtualType name="Ravi\Learn\Helper\Data\Session" type="Ravi\Learn\Helper\Data">
        <arguments>
            <argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session</argument>
        </arguments>
    </virtualType>
</config>

2 Answers 2

1

You didn't assign Session class inside constructor Change your code with below code

<?php
namespace Ravi\Learn\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Customer\Model\Session;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var Session
     */
    protected $customerSession;

    public function __construct(
        Context $context, 
        Session $customerSession
    )
    {
        parent::__construct($context);
        $this->customerSession = $customerSession;
    }

    public function getSess(){
       return $this->customerSession->getId();
    }
}
0

So; I'm not sure to know much more than you, though, from what I can see quickly in all my websites codes; the virtual types seems to be used as argument in di.xml and never directly called. They also seems to be mostly used for ui component, grid and form.

Some example

Deifintion of virtual type

<virtualType name="Cpy\Cms\PlpImageUploader" type="Magento\Catalog\Model\ImageUploader">
    <arguments>
        <argument name="baseTmpPath" xsi:type="string">banners/plp/tmp</argument>
        <argument name="basePath" xsi:type="string">banners/plp</argument>
        <argument name="allowedExtensions" xsi:type="array">
            <item name="jpg" xsi:type="string">jpg</item>
            <item name="jpeg" xsi:type="string">jpeg</item>
            <item name="gif" xsi:type="string">gif</item>
            <item name="png" xsi:type="string">png</item>
        </argument>
        <argument name="allowedMimeTypes" xsi:type="array">
            <item name="jpg" xsi:type="string">image/jpg</item>
            <item name="jpeg" xsi:type="string">image/jpeg</item>
            <item name="gif" xsi:type="string">image/gif</item>
            <item name="png" xsi:type="string">image/png</item>
        </argument>
    </arguments>
</virtualType>

Use of virtual type

<type name="Cpy\Cms\Controller\Adminhtml\Plp\Imageupload">
    <arguments>
        <argument name="imageUploader" xsi:type="object">Cpy\Cms\PlpImageUploader</argument>
    </arguments>
</type>

Then in the controller class, the called class isn't the virtual type, but the type used to define the virtual class. I guess magento links it using the di.

class Imageupload extends Action implements HttpPostActionInterface{
/**
 * Image uploader
 *
 * @var \Magento\Catalog\Model\ImageUploader
 */
protected $imageUploader;

That was an exemple with a controller but I guess it's the same with an helper.

2
  • claims you are wrong virtual type can be used in model helper and other. And its not used only in forms. Please see this. <virtualType name="Magento\Catalog\Pricing\Price\Pool" type="Magento\Framework\Pricing\Price\Pool"> <arguments> <argument name="prices" xsi:type="array"> <item name="catalog_rule_price" xsi:type="string">Magento\CatalogRule\Pricing\Price\CatalogRulePrice</item> </argument> </arguments> </virtualType> Commented Nov 15, 2021 at 11:06
  • Did not said they were only used for that, I said mostly. Never said neither we can't use them in helper. My point is that if you want to use it in a helper, you probably need to make that declaration of helper like in my Use of virtual type code block. Hope you will manage to do what you want :)
    – Claims
    Commented Nov 15, 2021 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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