Magento:将小部件工具添加到产品和类别WYSIWYG

4
在Magento的CMS页面的所见即所得编辑器上,有一个工具可以将Magento小部件添加到编辑器中。我希望这个功能也可以在产品和类别描述的所见即所得编辑器中使用。
我现在无法找到编辑器加载的位置。请问有人能告诉我该怎么做或至少指点一下方向吗?
提前感谢。 enter image description here
4个回答

7
在按照 @David Manner 的回答启用 Mage_Adminhtml_Block_Catalog_Helper_Form_Wysiwyg_Content 类中的 add_widgetsadd_variables 后,您可能会发现虽然这确实会在所见即所得编辑器中启用它们并正常工作,但它在前端只会呈现原始的小部件/变量代码(而不是适当的标记)。
您可以通过以下方式修复:
导航至 /app/design/frontend/package/theme/template/catalog/category/view.phtml 查找 <?php if($_description=$this->getCurrentCategory()->getDescription()): ?> 在此行下面添加以下内容:
<?php 
    $helper = Mage::helper('cms'); 
    $processor = $helper->getPageTemplateProcessor(); 
    $_description = $processor->filter($_description); 
?>

这样前端就能正确呈现了。

我的上面的例子是针对类别页面的,但同样可以在产品视图模板中实现在产品页面上。 - zigojacko
1
这似乎在Magento ver. 1.9.1.0上不再起作用。 - zigojacko

4
在类 Mage_Adminhtml_Block_Catalog_Helper_Form_Wysiwyg_Content 下的配置数组中有两个标志 'add_widgets' 和 'add_variables',它们默认都设置为 false。
将它们设置为 true 将在事件 cms_wysiwyg_config_prepare 上被 Mage_Widget_Model_Observer 类函数 prepareWidgetsPluginConfig 捕获。
我建议您重新编写 Mage_Adminhtml_Block_Catalog_Helper_Form_Wysiwyg_Content 以适应您的需求,但将 add_widgets 和 add_variables 设置为 true 应该适用于分类和产品。

1
刚试了一下,虽然它在所见即所得编辑器中完美地添加了按钮,并且似乎按预期运行,但在前端渲染时没有正确显示(只显示小部件代码而不是转换为链接)。 - zigojacko

1
阅读所有答案后,我发现了一种优雅的解决方案。这个解决方案只重写了一个Block类,并且没有改变任何模板文件。
  1. Rewrite Mage_Adminhtml_Block_Catalog_Helper_Form_Wysiwyg_Content

    <config> ... <global> <blocks> ... <adminhtml> <rewrite> <catalog_helper_form_wysiwyg_content>Agere_Wysiwyg_Block_Widget_Anywhere</catalog_helper_form_wysiwyg_content> </rewrite> </adminhtml> </blocks> ... </global> </config>

  2. Change only two flags in the config array 'add_widgets' and 'add_variables' to true

    class Agere_Wysiwyg_Block_Widget_Anywhere extends Mage_Adminhtml_Block_Catalog_Helper_Form_Wysiwyg_Content {
        protected function _prepareForm() {
            //return parent::_prepareForm();
            $form = new Varien_Data_Form(array('id' => 'wysiwyg_edit_form', 'action' => $this->getData('action'), 'method' => 'post'));
    
            $config['document_base_url']     = $this->getData('store_media_url');
            $config['store_id']              = $this->getData('store_id');
            $config['add_variables']         = true;
            $config['add_widgets']           = true;
            $config['add_directives']        = true;
            $config['use_container']         = true;
            $config['container_class']       = 'hor-scroll';
    
            $form->addField($this->getData('editor_element_id'), 'editor', array(
                'name'      => 'content',
                'style'     => 'width:725px;height:460px',
                'required'  => true,
                'force_load' => true,
                'config'    => Mage::getSingleton('cms/wysiwyg_config')->getConfig($config)
            ));
            $this->setForm($form);
    
            return $this;
        }
    }
    
  3. Create handler that will process content from category or product

    class Agere_Wysiwyg_Helper_Filter extends Mage_Core_Helper_Abstract {
        public function categoryAttribute($mainHelper, $result, $params) {
            return $this->process($result);
        }
    
        public function productAttribute($mainHelper, $result, $params) {
            return $this->process($result);
        }
    
        public function process($result) {
            /** @var Mage_Cms_Helper_Data $helperCms */
            $helperCms = Mage::helper('cms');
            $processor = $helperCms->getPageTemplateProcessor();
    
            return $processor->filter($result);
        }
    }
    
  4. Finally create Observer wich add handlers for out wysiwyg

    class Agere_Wysiwyg_Model_Observer extends Varien_Event_Observer {
        public function addWysiwygHandler(Varien_Event_Observer $observer) {
            /** @var Mage_Catalog_Helper_Output $_helperOutput */
            /** @var Agere_Wysiwyg_Helper_Filter $_helperFilter */
            $_helperOutput = Mage::helper('catalog/output');
            $_helperFilter = Mage::helper('agere_wysiwyg/filter');
            $_helperOutput->addHandler('categoryAttribute', $_helperFilter);
            $_helperOutput->addHandler('productAttribute', $_helperFilter);
        }
    }
    

完整的代码快照请查看链接https://github.com/popovsergiy/magento-wysiwyg


0

我认为更好的方法是创建一个新的观察者来监听相同的事件,并使模块依赖于Mage_Widget。然后我们的观察者将在Mage_Widget之后运行


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接