Magento: Resolving Image Links Issues in Custom Modules with WYSIWYG Editor
When developing custom Magento modules that incorporate the WYSIWYG editor, you might encounter issues with incorrect image links being generated for uploaded media files. This can lead to images not displaying properly across different browsers.
The Problem: Incorrect Image Links Generated by WYSIWYG Editor
In your custom module’s field definition, you’ve correctly configured the editor type and specified the necessary wysiwyg option:
$fieldset->addField('submenu_html', 'editor', array(
// Field configuration...
'wysiwyg' => true,
'config' => Mage::getSingleton('cms/wysiwyg_config')->getConfig(),
));
You’ve also ensured that the required JavaScript and CSS files are included in your XML layout file. However, when you upload an image using the WYSIWYG editor, Magento generates links like this:
<img src="http://linktoproject/index.php/admin/cms_wysiwyg/directive/___directive/e3ttZWRpYSB1cmw9Ind5c2l3eWcvVW50aXRsZWQtMS5wbmcifX0,/key/13e9709bba938ba67ff4ab68cb24c7a969aa89da4039bd850d65204116d6e76a/" alt="" />
This type of link only works correctly in Mozilla, while other browsers fail to load the image.
Understanding the Issue: Base URL and Image Path
The problem arises from how Magento’s WYSIWYG editor handles image paths. When you upload an image, it generates a relative path that is not correctly resolved against the base URL during output.
To fix this issue, we need to ensure that the correct base URL is applied when rendering the content containing the uploaded images.
The Solution: Setting the Correct Base URL for WYSIWYG Content
- Extend the WYSIWYG Configuration Object
Create a new module configuration model that extends Mage_Cms_Model_Wysiwyg_Config and override the _getUploadDir() method:
class My_Module_Model_Wysiwyg_Config extends Mage_Cms_Model_Wysiwyg_Config
{
protected function _getUploadDir()
{
return Mage::getBaseDir('media') . DS . 'wysiwyg' . DS;
}
}
- Register the Custom Configuration
In your module’s configuration XML, register the custom WYSIWYG configuration:
<config>
<global>
<cms_wysiwyg_config>
<my_module>
<files>images,flash</files>
<directories>
<media>media/wysiwyg</media>
</directories>
<models>
<my_module>
<class>My_Module_Model_Wysiwyg_Config</class>
<fields>
<editor>submenu_html</editor>
</fields>
</my_module>
</models>
</my_module>
</cms_wysiwyg_config>
</global>
</config>
- Update the Field Configuration
Modify your field definition to use the custom WYSIWYG configuration:
$fieldset->addField('submenu_html', 'editor', array(
// Field configuration...
'config' => Mage::getSingleton('my_module/cms_wysiwyg_config')->getConfig(),
));
Conclusion
By extending Magento’s default WYSIWYG configuration and setting the correct upload directory, you can ensure that image links generated by the editor are resolved against the proper base URL. This resolves the issue of images not displaying correctly across different browsers.
Remember to clear your cache after making these changes for them to take effect in your custom module’s admin interface.
For more advanced Magento development techniques and best practices, explore our comprehensive resources at Laravel Company.