简体   繁体   中英

How to programmatically add more variables in Magento's WYSIWYG editor

I want to add custom variables to "Insert Variable" popup of Magento's WYSIWYG editor (TinyMCE)

Searched the internet but couldn't find any solution, is that possible? Any one?

插入变量

You can add a custom variable in your Magento backend:

System -> Custom Variable -> Add New Variable

Here you can add a HTML or plain text variable.

Variable code is identifier. Variable name what will be displayed.

For adding custom variables programmatically, see this blog:

http://inchoo.net/ecommerce/magento/injecting-variables-into-a-magento-cms-static-block/

Please follow below.

Add field in the custom module Form app\\code\\local\\Namespace\\Module\\Block\\Adminhtml\\Template\\Email\\Edit\\Tabs\\Form.php

$editor = Mage::getSingleton('namespace/wysiwyg_config')->getConfig(array('tab_id' => $this->getTabId()));

$fieldset->addField('body', 'editor', array(
    'name' => 'body',
    'label' => Mage::helper('module')->__('Body'),
    'title' => Mage::helper('module')->__('Body'),
    'style' => 'width:700px; height:500px;',
    'config' => $editor,
    'wysiwyg' => true,
    'required' => true,
));

app\\code\\local\\Namespace\\Module\\Model\\Wysiwyg\\Config.php

<?php
class Namespace_Module_Model_Wysiwyg_Config extends Mage_Cms_Model_Wysiwyg_Config
{
    /**
     *
     * @param Varien_Object
     * @return Varien_Object
     */
    public function getConfig($data = array())
    {
        $config = parent::getConfig($data);

        $newOptiones = Mage::getSingleton('namespace/variables_options')->getWysiwygPluginSettings($config);

        if (isset($newOptiones['plugins'][1]) && is_array($newOptiones['plugins'][1])) {
            $config->setData('plugins', array($newOptiones['plugins'][1]));
        }

        $config->setData('files_browser_window_url', Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg_images/index/'));
        $config->setData('directives_url', Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive'));
        $config->setData('directives_url_quoted', preg_quote($config->getData('directives_url')));
        $config->setData('widget_window_url', Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/widget/index'));
        $config->setData('add_variables', true);

        return $config;
    }

}

app\\code\\local\\Namespace\\Module\\Adminhtml\\VariableController.php

<?php
class Namespace_Module_Adminhtml_VariableController extends Mage_Adminhtml_Controller_Action
{
    /**
     * WYSIWYG Plugin Action
     *
     */
    public function wysiwygPluginAction()
    {
        $customVariables = Mage::getModel('namespace/variables_process')->getVariablesOptionArray(true);
        $this->getResponse()->setBody(Zend_Json::encode($customVariables));
    }    
}

app\\code\\local\\Namespace\\Module\\Model\\Variables\\Process.php

<?php
class Namespace_Module_Model_Variables_Process extends Mage_Core_Model_Variable
{

    public function getVariablesOptionArray($withGroup = false)
    {
        $collection = $this->getCollection();/*Custom Module Collection*/
        $variables = array();
        foreach ($collection->toOptionArray() as $variable) {
            $variables[] = array(
                'value' => '{{customVar code=' . $variable['value'] . '}}',
                'label' => Mage::helper('core')->__('%s', $variable['label'])
            );
        }
        if ($withGroup && $variables) {
            $variables = array(
                'label' => Mage::helper('core')->__('Custom Variables'),
                'value' => $variables
            );
        }


        $allVars = array(
            $variables
        );

        return $allVars;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM