Magento2 – How to Set Default Value for Configuration Field with Type Image

magento2

I have system.xml with some fields:

<field id="image_width" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Image width</label>
</field>
<field id="image_height" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Image height</label>
</field>
<field id="placeholder_image" translate="label comment" type="image" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Image placeholder</label>
    <comment>Allowed file types:PNG, GIF, JPG, JPEG, SVG.</comment>
    <backend_model>MyNamespace\MyModule\Model\Config\Backend\Image\Placeholder</backend_model>
    <base_url type="media" scope_info="1">module_folder</base_url>
</field>

And i set default values for them in config.xml:

<image_width>100</image_width>
<image_height>100</image_height>
<placeholder_image>placeholder.gif</placeholder_image>

The problem is in placeholder_image field. My placeholder image lies in app/code/MyNamespace/MyModule/view/frontend/web/images/placeholder.gif and Magento 2 looks for it in pub/media/module_folder/placeholder.gif. How can i tell magento to copy it from my folder to pub/media?

Best Answer

I assume you have this code in one of your custom extensions.
You can use the Setup/InstallData.php or Setup/UpgradeData.php files that run when you install or upgrade the module and place an image inside the folder you need.

Related Topic