1

I tried so many ways to override the following file: vendor/magento/module-catalog/Model/Product/Gallery/CreateHandler.php but this file always runs from the core file.

I also added this file in the di.xml file. But it still doesn't work.

di.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<preference for="Magento\Catalog\Model\Product\Gallery\CreateHandler" type="Company\ModuleName\Model\Product\Gallery\CreateHandler" />
</config>

I want to override processNewAndExistingImages() function from this file.

How can I do that?

Please help me.

Thanks.

10
  • have you run di compile command?
    – kunj
    Jun 11, 2018 at 10:23
  • did you extend \Magento\Catalog\Model\Product\Gallery\CreateHandler for your CreateHandler class? If yes then run di compile. Jun 11, 2018 at 10:27
  • Yes. I extend that class @SukumarGorai Jun 11, 2018 at 10:27
  • Why di compile? Other model override without di compile. Jun 11, 2018 at 10:28
  • 2
    Did you find any solution?
    – ND17
    Oct 10, 2018 at 7:58

8 Answers 8

3

Your di.xml is wrong if you want to override a class.

In Magento there are two types of overriding:

  1. One is Plugin
  2. Another is preference

PLUGIN:

Is used for changing the behavior of a method.

Syntax:

<type name="{OriginalClass}">
    <plugin name="{Name}" type="{PluginClasss}" sortOrder="{sortorder}"/>
</type>

Class preference:

The old-fashioned way of overriding classes is what we got used to, but this is slightly different... See the details at the [Inchoo blog][2].

Syntax:

<preference for="{OriginalClass}" type="{OverriderClassClasss}" />

As per as, you want to override a function/method: Magento\Customer\Model\Address\AbstractAddress

Bellow Example for preference

Create di.xml File

Path : app/code/{vendorName}/{ModuleName}/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Model\Address\AbstractAddres" type="{vendorName}\{ModuleName}\Model\Address\AbstractAddres" />
</config> 

Create Model file

Path : app/code/{vendorName}/{ModuleName}/Model/Value.php

namespace {vendorName}\{ModuleName}\Model\Address\AbstractAddres;

    class Value extends \Magento\Customer\Model\Address\AbstractAddres
    {
        public function __construct()
        {
            echo "Model Rewrite Working"; die();

        }

    }
1

The "processNewAndExistingImages" method is located in Magento\Catalog\Model\Product\Gallery\CreateHandler (that called on product creating), but it called on editind from Magento\Catalog\Model\Product\Gallery\UpdateHandler. So... To override "processNewAndExistingImages" in "CreateHandler" need to override it twice:

  • make preference for Magento\CatalogStaging\Model\Product\Gallery\CreateHandler with method processNewAndExistingImages
  • make preference for Magento\Catalog\Model\Product\Gallery\UpdateHandler with method processNewAndExistingImages
0

If you want to achieve this then you also need add dependency in you

module.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright © 2016 Rocket Bazaar. All rights reserved.
See COPYING.txt for license details
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Moddule/etc/module.xsd">
    <module name="Your_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>
7
0

In your file Company\ModuleName\Model\Product\Gallery\CreateHandler add this code:

<?php
class Company\ModuleName\Model\Product\Gallery\CreateHandler extends \Magento\Catalog\Model\Product\Gallery\CreateHandler
{
  public function processNewAndExistingImages()
  {
     //write your code here
  }
}

But, this is not the good way to override. If you have time try overriding using plugin. Follow below link for more information about plugin.

https://devdocs.magento.com/guides/v2.1/extension-dev-guide/plugins.html

1
  • This method (processNewAndExistingImages) is protected. So plugin will not work.
    – orthener
    Nov 12, 2020 at 20:30
0

There is a plugin at play here that's tricky. Product save calls the UpdateHandler method of Magento\Catalog\Model\Product\Gallery.

The file @ Magento\Catalog\Model\Product\Gallery\UpdateHandler.php says: class UpdateHandler extends \Magento\Catalog\Model\Product\Gallery\CreateHandler{...}

To achieve your goal, override UpdateHandler.php in addition to CreateHandler.php.

di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\Catalog\Model\Product\Gallery\CreateHandler"
               type="SithLord\Media\Model\Product\Gallery\CreateHandler" />

    <preference for="Magento\Catalog\Model\Product\Gallery\UpdateHandler"
               type="SithLord\Media\Model\Product\Gallery\UpdateHandler" />
    ...
</config>

CreateHandler.php:

<?php
namespace SithLord\Media\Model\Product\Gallery;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\MediaStorage\Model\File\Uploader as FileUploader;

class CreateHandler extends \Magento\Catalog\Model\Product\Gallery\CreateHandler
{
    //-=- rip all code for example simplicity -=-
    ...
}

UpdateHandler.php:

<?php
namespace SithLord\Media\Model\Product\Gallery;

use Magento\Framework\EntityManager\Operation\ExtensionInterface;

class UpdateHandler extends \SithLord\Media\Model\Product\Gallery\CreateHandler
{
    //-=- rip all code for example simplicity -=-
    ...
}
0

This is one of those classes that you cannot override due to a bug in Magento. Actually @Emipro was doing it correct but sadly the class UpdateHandler calls CreateHandler without the correct path thus instantiating it before the rewrite can ever be used. This is the current UpdateHandler:

namespace Magento\Catalog\Model\Product\Gallery;
use ...
class UpdateHandler extends CreateHandler
{...

In the "use" list the CreateHandler is not named and since it is in the same directory as de UpdateHandler the UpdateHandler instantiates this CreateHandler and uses it before Magento gets to your rewrite. What should have been there is:

namespace Magento\Catalog\Model\Product\Gallery;
use ...
class UpdateHandler extends Magento\Catalog\Model\Product\GalleryCreateHandler
{...

Sadly that is not the case. So what can you do? Simply rewrite both of them. Here is my rewrite and it works (but I still think Magento should fix this).

di.xml:

<preference for="Magento\Catalog\Model\Product\Gallery\CreateHandler" type="Vendor\ProductImporter\Model\Rewrite\Catalog\Model\Product\Gallery\CreateHandler" />
<preference for="Magento\Catalog\Model\Product\Gallery\UpdateHandler" type="Vendor\ProductImporter\Model\Rewrite\Catalog\Model\Product\Gallery\UpdateHandler" />

then because you have no choice but copy the whole file because of the "extend" part, in my directory Vendor\ProductImporter\Model\Rewrite\Catalog\Model\Product\Gallery I have the following:

namespace Vendor\ProductImporter\Model\Rewrite\Catalog\Model\Product\Gallery;
use ..[all the uses from the original file]
class UpdateHandler extends \Vendor\ProductImporter  
\Model\Rewrite\Catalog\Model\Product\Gallery\CreateHandler
{
..[the rest of the original file]

So you can see the UpdateHandler now extends the correct way. And now I can use my rewrite of the CreateHandler and I want to make sure Magento does not make copies of my images with an underscore followed by an index (like image_1.png) so I want to rewrite the function moveImageFromTmp($file):

namespace Vendor\ProductImporter\Model\Rewrite\Catalog\Model\Product\Gallery;
class CreateHandler extends \Magento\Catalog\Model\Product\Gallery\CreateHandler
{
protected function moveImageFromTmp($file) 
{ ... } 

Note that I don't need the whole file here, I can extend it here as it should be extended. It is just the UpdateHandler that isn't correctly extending things...

0

Since this issue still exists (https://github.com/magento/magento2/issues/34814) , you can use this patch to fix the file

diff --git a/Catalog/Model/Product/Gallery/UpdateHandler.php b/Catalog/Model/Product/Gallery/UpdateHandler.php
index edee9ae..5b8689b 100644
--- a/Catalog/Model/Product/Gallery/UpdateHandler.php
+++ b/Catalog/Model/Product/Gallery/UpdateHandler.php
@@ -26,7 +26,7 @@ use Magento\Store\Model\StoreManagerInterface;
  * @since 101.0.0
  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  */
-class UpdateHandler extends CreateHandler
+class UpdateHandler extends \Magento\Catalog\Model\Product\Gallery\CreateHandler
 {
     /**
      * @var AttributeValue
0

i already get some issue like this and in my case need update some code in processNewAndExistingImages, you just need extends

Magento\Catalog\Model\Product\Gallery\UpdateHandler

and update function u need to update. its works to me.

i hope this helps.

New contributor
Bramantyo is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.