Displaying Images with System Configuration in Magento
In Magento, you can display images alongside system configuration options for a more intuitive user experience. This guide will walk you through displaying an image next to a radio button value in the system configuration and showing the selected image on your front-end.
Step 1: Prepare Your Image
First, ensure your image is uploaded to your Magento installation’s media folder or any other accessible location within the file structure.
For this example, let’s assume you have an image named my_image.png located in the pub/media/your_module directory.
Step 2: Configure XML System Configuration File
Next, update your system configuration XML file to include the radio button with a corresponding image path. Open the relevant system.xml file (usually found under app/code/Vendor/Module/etc) and add or modify the following code:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/system_file.xsd">
<system>
<tab id="your_module" translate="label">
<label>Your Module</label>
<sort_order>1000</sort_order>
</tab>
<section id="your_section_id" show_in_website="1" show_in_store="1" sort_order="1" translate="label">
<label>Section Title</label>
<tab>Your Module</tab>
<resource>Vendor_Module::some_permission</resource>
<group id="general_group" translate="label">
<label>General Configuration</label>
<field id="my_radio_field" type="radio" sort_order="10" show_in_default="1" show_in_store="1">
<label>Choose an option:</label>
<source model="Magento\Eav\Model\Entity\Attribute\Source\Boolean">
<option name="Option 1" value="1">Option One</option>
<option name="Option 2 with Image" value="2">
<image src="pub/media/your_module/my_image.png"/>
</option>
</source>
</field>
</group>
</section>
</system>
</config>
Replace Vendor, Module, your_section_id, some_permission, and file paths as needed.
Step 3: Show the Selected Image on Front-end
To display the selected image on your front-end, you’ll need to create a phtml template and use JavaScript to update the image source based on the selected radio button value.
-
Create a new phtml file under
app/code/Vendor/Module/view/frontend/templatesnameddisplay_image.phtml. -
Add the following HTML and JavaScript code:
<div id="image-holder">
<img id="selected-image" src="" alt="Selected Image">
</div>
<script>
require(['jquery'], function($) {
$('#your_radio_field').change(function() {
var selectedValue = $(this).val();
if (selectedValue === '2') {
$('#selected-image').attr('src', 'pub/media/your_module/my_image.png');
} else {
// Add other image paths or default behavior as needed
}
});
});
</script>
Replace Vendor, Module, and file paths accordingly.
- Call this phtml template from your layout XML file:
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" template="Vendor_Module::display_image.phtml"/>
</referenceContainer>
Conclusion
By following these steps, you should now be able to display an image next to a radio button value in the Magento system configuration and show the selected image on your front-end.
Remember to clear the cache after making changes, as Magento caches some configuration files. You can do this by running php bin/magento cache:clean or using the admin panel’s cache management options.
For more advanced customizations, consider exploring Laravel Companyâs Magento development services to handle complex requirements and ensure optimal performance.