Magento – Issue in Admin Panel after SUPEE Patch 8788 installation

adminconflictmagento-1.9patchessupee-8788

I have Magento CE 1.9.2.4 installed along with patches (5377,1533,4788 etc. almost all patches).

This question also reveals issues that may/surely occur in any custom module involving Image uploads in their custom sections, rather than just core magento issues.

  1. Now after I installed the latest patch 8788 through command-line, I am not able to open "Add/Edit" page of my custom module, which was working fine prior to 8788 installation.

I am getting below error when I try to open the "Add New Banner" page of my module:

Fatal error: Call to a member function setUrl() on a non-object in /home/site_user/public_html/app/code/community/My/Module/Block/Adminhtml/Banner/Add/Tab/Image.php on line 57

The culprit line is as below:

$this->getUploader()->getConfig()->setUrl(Mage::getModel('adminhtml/url')->addSessionParam()->getUrl('*/*/image'))
  1. Also I cannot see already uploaded product images in admin Catalog > Manage Products > Any product > Images section.

Below is the core Mage_Adminhtml_Block_Media_Uploader class being called.

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @copyright  Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Adminhtml media library uploader
 *
 * @category   Mage
 * @package    Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */

/**
 * @deprecated
 * Class Mage_Adminhtml_Block_Media_Uploader
 */
class Mage_Adminhtml_Block_Media_Uploader extends Mage_Uploader_Block_Multiple
{
    /**
     * Constructor for uploader block
     */
    public function __construct()
    {
        parent::__construct();
        $this->getUploaderConfig()->setTarget(Mage::getModel('adminhtml/url')->addSessionParam()->getUrl('*/*/upload'));
        $this->getUploaderConfig()->setFileParameterName('file');
    }
}

Anyone let me know how to fix this with least code changes as possible.

Best Answer

Mage_Adminhtml_Block_Media_Uploader is deprecated on after SUPEE-8788 (and 1.9.3). Thus there are several backward incompatibility changes which breaks module using the uploader.

I first thought a small change would fix it but there's actually way more to do.

Make a module using the gallery compatible with 1.9.2 and 1.9.3

So if you're a module provider, you don't want to have two different versions of your module for 1.9.2 and 1.9.3. Here's how to make your code compatible with both:

In your block _prepareLayout method you need to do the following:

Replace:

 $this->getUploader()->getConfig()
            ->setUrl($url)
            ->setFileField('image')
            ->setFilters(array(
                'images' => array(
                    'label' => Mage::helper('adminhtml')->__('Images (.gif, .jpg, .png)'),
                    'files' => array('*.gif', '*.jpg','*.jpeg', '*.png')
                )
            ));

With:

    if (class_exists("Mage_Uploader_Block_Abstract")) {
        // PATCH SUPEE-8788 or Magento 1.9.3
        $this->getUploader()->getUploaderConfig()
            ->setFileParameterName('image')
            ->setTarget($url);

        $browseConfig = $this->getUploader()->getButtonConfig();
        $browseConfig
            ->setAttributes(
                array("accept"  =>  $browseConfig->getMimeTypesByExtensions('gif, png, jpeg, jpg'))
            );
    } else {
        $this->getUploader()->getConfig()
            ->setUrl($url)
            ->setFileField('image')
            ->setFilters(array(
                'images' => array(
                    'label' => Mage::helper('adminhtml')->__('Images (.gif, .jpg, .png)'),
                    'files' => array('*.gif', '*.jpg','*.jpeg', '*.png')
                )
            ));
    }

As you can see I'm using class_exists to check whether SUPEE-8788 or Magento 1.9.3 is applied.

Then in your gallery.phtml you need to replace:

var <?php echo $_block->getJsObjectName(); ?> = new Product.Gallery('<?php echo $_block->getHtmlId() ?>', <?php if ($_block->getElement()->getReadonly()):?>null<?php else:?><?php echo $_block->getUploader()->getJsObjectName() ?><?php endif;?>, <?php echo $_block->getImageTypesJson() ?>);

With:

<?php if (class_exists("Mage_Uploader_Block_Abstract")): ?>
    var <?php echo $_block->getJsObjectName(); ?> = new Product.Gallery('<?php echo $_block->getHtmlId() ?>', <?php echo $_block->getImageTypesJson() ?>);
<?php else: ?>
    var <?php echo $_block->getJsObjectName(); ?> = new Product.Gallery('<?php echo $_block->getHtmlId() ?>', <?php if ($_block->getElement()->getReadonly()):?>null<?php else:?><?php echo $_block->getUploader()->getJsObjectName() ?><?php endif;?>, <?php echo $_block->getImageTypesJson() ?>);
<?php endif; ?>

Then for the layout file, you can do it like this:

<reference name="head">
    <action method="addJs"><file helper="module/getFlowMin" /></action>
    <action method="addJs"><file helper="module/getFustyFlow" /></action>
    <action method="addJs"><file helper="module/getFustyFlowFactory" /></action>
    <action method="addJs"><file helper="module/getAdminhtmlUploaderInstance" /></action>
</reference>

Replace module with your helper class identifier and in your module Data.php helper add the following:

protected function _isNoFlashUploader()
{
    return class_exists("Mage_Uploader_Block_Abstract");
}

public function getFlowMin()
{
    return $this->_isNoFlashUploader() ? "lib/uploader/flow.min.js" : null;
}

public function getFustyFlow()
{
    return $this->_isNoFlashUploader() ? "lib/uploader/fusty-flow.js" : null;
}

public function getFustyFlowFactory()
{
    return $this->_isNoFlashUploader() ? "lib/uploader/fusty-flow-factory.js" : null;
}

public function getAdminhtmlUploaderInstance()
{
    return $this->_isNoFlashUploader() ? "mage/adminhtml/uploader/instance.js" : null;
}
Related Topic