1

I want to display images as radio button option in Magento 2 Admin panel Store Configuration.

For this i have added the following code in etc/adminhtml/system.xml file:

<field id="mymodsettings_imagepattern" translate="label" type="radios" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Select One of the Image</label>
    <source_model>Mynamespace\Mymodule\Model\Config\Source\Displayimageoptions</source_model>
</field>

Also I created a file Displayimageoptions.php in Mynamespace\Mymodule\Model\Config\Source\ directory with the below code:

<?php
namespace Mynamespace\Mymodule\Model\Config\Source;

class Displayimageoptions implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * @return array
     */
    public function toOptionArray()
    {
        $mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

        return [['value' => 1, 'label' => __("<img src='" . $mediaUrl . "mymod/images/pattern_1.png' />")]];
    }

}

I am unable to get the media URL here. When tried to print $this, it returns empty object. I also tried the below codes but did not get to work for me:

$this->getUrl($route, $params)
$this->_helper->getUrl($route, $params)

Any help would be much appreciated.

0

I was able to get the media url using the following code:

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $_objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currentStore = $storeManager->getStore();
$mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

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.