3
votes

I want to allow adding the Catalog Product Link widget in the description field of a product (so I can easily link to other products in the description). I've already extended Mage_Catalog_Model_Product by creating a file like:

class mymodule_Catalog_Model_Product extends Mage_Catalog_Model_Product
{

    /**
      * Add getDescription function that interprets widget or static blocks
      * within product descriptions
      */
    public function getDescription() {
        $processor = Mage::getModel('widget/template_filter');
        $html = $processor->filter($this->getData('description'));
        return $html;
    }

}

Now it works fine if I type something like

{{widget type="catalog/product_widget_link" anchor_text="my text" template="catalog/product/widget/link/link_inline.phtml" id_path="product/1234"}}

into the description field -- it creates a link to the product Id 1234.

But I want to add the actual Catalog Product Link widget button in the WYSIWYG editor for editing a product. The button is already in the CMS editor, but I am missing what I need to modify in order to add this widget to the Admin interface for editing a product. Can someone help me out?

2
This is very bad practice, you should better place this into the blocks or event better the templates.witrin
@witrin Why is this bad practice? The product description is dynamic; how would you put those links into a template?Yumecosmos
@Yumecosmos Because you override the catalog core classes and you're only able to do this once, so you're chance is good to get conflicts with other extensions, or in other words it's bad for maintenance. Just create a helper with those three lines above and use it in your template for the view where the description is displayed. In general I would try not to override classes like this directly. Better are observers or if there is no event use the blocks or templates as long as you have not to change 10 files. Overriding core classes is the very last option if no alternatives available.witrin
@witrin Oh! Sorry, I misunderstood. I thought you meant using a widget to make a link is bad practice. Agree that overriding should be a last resort, but I'll play devil's advocate... as tightly coupled as the views and blocks tend to be, is there really any way to avoid the maintenance headaches? Sooner or later you end up manually merging changes. (We've definitely had vendors tell us, "to install our extension copy these lines into the phtml..." Granted, that's probably terrible practice in and of itself.) Or maybe I'm just doing it wrong. Need to learn how/when to use helpers.Yumecosmos

2 Answers

10
votes

For anybody stumbling across this later on like myself, you can use the cms_wysiwyg_config_prepare event to set this to true.

E.G: in config.xml

<events>
            <cms_wysiwyg_config_prepare>
                <observers>
                    <webtise_widgets>
                        <class>webtise_widgets/observer</class>
                        <method>cmsWysiwygConfigPrepare</method>
                    </webtise_widgets>
                </observers>
            </cms_wysiwyg_config_prepare>
        </events>

In your observer

<?php class Webtise_Widgets_Model_Observer{

    public function cmsWysiwygConfigPrepare(Varien_Event_Observer $observer){
        $observer->getEvent()->getConfig()->setAddWidgets(true);
    }
}
2
votes

I was able to do this by overriding the core file

app/code/local/Mage/Adminhtml/Block/Catalog/Helper/Form/Wysiwyg/Content.php

and setting

$config['add_widgets']           = true;

Now the widget button shows up in all WYSIWIG editors in the admin interface.