Magento – Widget dependency doesn’t work for chooser fields

dependencywidgetwidgets

I have one yes/no select field in a widget.xml

If "yes" was selected then category select field should be visible.

But category select field always visible instead. That's wrong.

Here is my widget.xml:

<select_category_type translate="label description">
    <label>Select categories</label>
    <visible>1</visible>
    <type>select</type>
    <values>
        <item0 translate="label">
            <value>0</value>
            <label>No</label>
        </item0>
        <item1 translate="label">
            <value>1</value>
            <label>Yes</label>
        </item1>
    </values>
    <sort_order>25</sort_order>
</select_category_type>

<selected_category translate="label">
    <visible>1</visible>
    <required>1</required>
    <label>Select categories</label>
    <type>label</type>
    <helper_block>
        <type>av_widget_product_featured/catalog_chooser</type>
        <data>
            <button translate="open">
                 <open>Select Categories...</open>
            </button>
            <use_massaction>1</use_massaction>
        </data>
    </helper_block>
    <depends>
        <select_category_type>
            <value>1</value>
        </select_category_type>
    </depends>
    <sort_order>30</sort_order>
</selected_category>

Best Answer

The problem is that you cannot use depends on widget items that are of type chooser :(

In the file js/mage/adminhtml/form.js it processes the changes on widgets in the function trackChange. Here is loads the hidden field that is used to store the value of the item as a reference and the using up it works up the dom to find the td that is should show or hide.

The problem with the chooser items is that there is no hidden input field that matches the id format that magento is looking for.

You will see the JS error Uncaught TypeError: Cannot read property 'up' of null.

To get part of the depends working you can look into how the html of the chooser is built in the file Mage_Widget_Block_Adminhtml_Widget_Chooser. By simply adding a hidden field with the id $element->getId() to the returned string of the function _toHtml the field itself with hide/show but the button for choosing will not as this is part of a different row in the table. Here you would have to update the javascript to deal with the structure. Luckly the $chooserId that is used to build the button appears to be derived from the element id and so this should be fairly simply to find via javascript.

Related Topic