extendix / Extendix_AdminFormChooserButton

Magento extension for creating Image Chooser, Product Chooser, Category Chooser, Static Block Chooser and Cms Page Chooser
7 stars 3 forks source link

Infinite loop of JS errors #1

Open clemblanco opened 9 years ago

clemblanco commented 9 years ago

Hey! Trying to add an Image chooser to a CMS Page but I get an infinite loop of JS errors: Screenshot1 Magento v1.9.1. I believe the reason could be that on a CMS Page edit page in the admin, Magento seems to add "page_" before the ID of the HTML element, see: Screenshot2

ceckoslab commented 9 years ago

Could you paste the entire form class?

clemblanco commented 9 years ago

This is how I call your Helper from a Model Observer in app/code/local/ACME/MyModule/Model/Observer.php:

class ACME_MyModule_Model_Observer
{
    public function cmsField($observer)
    {
        // get CMS model with data
        $model = Mage::registry('cms_page');
        // get form instance
        $form = $observer->getForm();
        // create new custom fieldset 'acme_mymodule_content_fieldset'
        $fieldset = $form->addFieldset('acme_mymodule_featured_fieldset', array(
            'legend' => Mage::helper('cms')->__('Featured'),
            'class'  => 'fieldset-wide'
        ));

        // other custom fields declared here like:
        $fieldset->addField('featured_title', 'text', array(
            'name'      => 'featured_title',
            'label'     => Mage::helper('cms')->__('Featured Title'),
            'title'     => Mage::helper('cms')->__('Featured Title'),
            'disabled'  => false,
            'value'     => $model->getFeaturedTitle()
        ));

        // ...

        /** @var Extendix_AdminFormChooserButton_Helper_Chooser $chooserButtonHelper */
        $chooserButtonHelper = Mage::helper('extendix_admin_form_chooser_button/chooser');
        $imageConfig = array(
            'input_name'        => 'featured_tile_background_image',
            'input_label'       => Mage::helper('cms')->__('Tile background image'),
            'button_text'       => Mage::helper('cms')->__('Insert Image'),
            'required'          => false,
        );
        $chooserButtonHelper->createImageChooser($model, $fieldset, $imageConfig);
    }
}

Defined in my app/code/local/ACME/MyModule/etc/config.xml as:

<?xml version="1.0"?>
<config>
    <modules>
        <ACME_MyModule>
            <version>0.0.10</version>
        </ACME_MyModule>
    </modules>
    <global>
    <events>
        <adminhtml_cms_page_edit_tab_content_prepare_form>
                <observers>
                    <acme_mymodule_page_edit_tab_content>
                        <type>singleton</type>
                        <class>ACME_MyModule_Model_Observer</class>
                        <method>cmsField</method>
                    </acme_mymodule_page_edit_tab_content>
                </observers>
        </adminhtml_cms_page_edit_tab_content_prepare_form>
    </events>
    </global>
</config>

Is that enough?

clemblanco commented 9 years ago

I think this is what you missed:

$prefix    = $element->getForm()->getHtmlIdPrefix();
$elementId = $prefix . $element->getId();

And then you should have 'target_element_id' => $elementId somewhere...

I'll make a pull request soon with the ability to have a preview of the image once saved, this problem fixed and a delete button as well.

clemblanco commented 9 years ago

Let me know what you think about this one. Inspired by https://github.com/aijko/aijko-widgetimagechooser.