【问题标题】:Magento: Add Widget tool onto product and category WYSIWYGMagento:将小部件工具添加到产品和类别所见即所得
【发布时间】:2012-12-29 10:26:39
【问题描述】:

在 Magento CMS 页面的 WYSIWYG 编辑器上,有一个工具可以将 Magento 小部件添加到编辑器中。我希望这也可用于产品和类别描述的 WYSIWYG。

我现在正在努力寻找编辑器的加载位置。谁能让我知道我可能需要做什么或至少为我指明正确的方向?

提前致谢。

【问题讨论】:

    标签: magento content-management-system widget wysiwyg


    【解决方案1】:

    阅读所有答案后,我找到了优雅的解决方案。此方案仅重写一个 Block 类,不更改任何模板文件。

    1. 重写 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. 仅将配置数组“add_widgets”和“add_variables”中的两个标志更改为 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. 创建将处理来自类别或产品的内容的处理程序

      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. 最后创建观察者,为所见即所得添加处理程序

      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

    【讨论】:

      【解决方案2】:

      根据@David Manner 的回答在Mage_Adminhtml_Block_Catalog_Helper_Form_Wysiwyg_Content 类中启用add_widgetsadd_variables 后,您可能会发现虽然这肯定会在所见即所得编辑器中启用这些功能并正常运行,但它只会呈现原始小部件前端的 /variable 代码(而不是适当的标记)。

      您可以使用以下方法进行修复:-

      导航到/app/design/frontend/package/theme/template/catalog/category/view.phtml

      查找&lt;?php if($_description=$this-&gt;getCurrentCategory()-&gt;getDescription()): ?&gt;

      在此行下方添加以下内容:-

      <?php 
          $helper = Mage::helper('cms'); 
          $processor = $helper->getPageTemplateProcessor(); 
          $_description = $processor->filter($_description); 
      ?>
      

      这将在前端正确呈现。

      【讨论】:

      • 我上面的示例是针对类别页面的,但您同样可以在产品视图模板中的产品页面上实现。
      • 这似乎不再适用于 Magento 版本。 1.9.1.0.
      【解决方案3】:

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

      【讨论】:

        【解决方案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 应该适用于类别和产品。

        【讨论】:

        • 刚试过这个,虽然它完美地添加了所见即所得编辑器中的按钮并且似乎按预期运行,但它没有在前端正确呈现(只显示小部件代码而不是转换为链接)。
        猜你喜欢
        • 2012-12-02
        • 2013-03-23
        • 2013-03-11
        • 2012-07-11
        • 1970-01-01
        • 2014-04-08
        • 2013-02-23
        • 1970-01-01
        • 2022-01-07
        相关资源
        最近更新 更多