Magento: TinyMCE not displayed / Shows error

I was working with a custom Magento module. I had to display TinyMCE editor in an admin form field.

I was getting this error when I try to add/edit data from the custom module:-

Warning: Invalid argument supplied for foreach() in lib\Varien\Data\Form\Element\Editor.php on line 225

I tried some workaround and was able to see the form in admin but still tinymce editor was not displayed on the desired form field.

I was getting this error on firebug console:-

tinyMceWysiwygSetup is not defined

Finally this solution helped me:-

http://www.magentocommerce.com/boards/index.php/viewthread/176212/P30/#t402235

Here are the steps that I followed:-

1) Ensure that the editor is enabled in your shop
It is enabled by default. You can check it from
System -> Configuration -> [GENERAL] -> Content Management -> Enable WYSIWYG Editor -> Enabled by default

2) Preparing editor used in edit form
app/code/local/Mynamespace/Mymodule/Block/Adminhtml/Mymodule/Edit.php


protected function _prepareLayout() 
{ 
	// added this code	
	if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) { 
		$this->getLayout()->getBlock('head')->setCanLoadTinyMce(true); 
		$this->getLayout()->getBlock('head')->setCanLoadExtJs(true); 
	} 
	parent::_prepareLayout(); 
}

3) Transforming textarea to editor

app/code/local/Mynamespace/Mymodule/Block/Adminhtml/Mymodule/Edit/Tab/Form.php

Include the content within ‘_prepareForm()‘ function


$configSettings = Mage::getSingleton('cms/wysiwyg_config')->getConfig( 
array( 
'add_widgets' => false, 
'add_variables' => false, 
'add_images' => false, 
'files_browser_window_url'=> $this->getBaseUrl().'admin/cms_wysiwyg_images/index/', 
)); 

$fieldset->addField('mycontent', 'editor', array( 
'name' => 'mycontent', 
'label' => Mage::helper('mymodule')->__('My Content'), 
'title' => Mage::helper('mymodule')->__(’My Content'), 
'style’ => 'width:700px; height:320px;', 
'wysiwyg' => true, 
'required' => true, 
'config' => $configSettings, 
)); 

Note: One important thing to notice is that I also had problem when my editor field name and id was ‘content’. It contradicts with some other id in the page. Hence, TinyMCE editor is not displayed properly. You should change it to other name. Like, I have named it ‘mycontent’.

4) Clear Cache

Now, TinyMCE should work fine.