cancel
Showing results for 
Search instead for 
Did you mean: 

Am I using the correct way to override a method?

SOLVED

Am I using the correct way to override a method?

I needed to slightly change the behavior of Magento_Elasticsearch. I achieved this by adding just one line of code to app/code/Magento/Elasticsearch/SearchAdapter/Query/Builder/MatchQuery.php (see https://github.com/Pontorez/magento/commit/3704d3024612602a998b3ac1e91863850c878fa7 ). This is METHOD #1.

 

But modifying Magento files is not a good idea so I decided to revert it and to create my own version of MatchQuery.php and mention it in di.xml:

<preference for="Magento\Elasticsearch\SearchAdapter\Query\Builder\MatchQuery" type="Pontorez\Elasticsearch\SearchAdapter\Query\Builder\MatchQuery" />

But that wasn't enough because I got errors like:

main.CRITICAL: Type Error occurred when creating object: Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Mapper, Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Mapper::__construct(): Argument #2 ($matchQueryBuilder) must be of type Magento\Elasticsearch\SearchAdapter\Query\Builder\MatchQuery, Pontorez\Elasticsearch\SearchAdapter\Query\Builder\MatchQuery given
main.CRITICAL: Type Error occurred when creating object: Magento\Elasticsearch7\SearchAdapter\Mapper, Magento\Elasticsearch7\SearchAdapter\Mapper::__construct(): Argument #1 ($mapper) must be of type Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Mapper, Pontorez\Elasticsearch\Elasticsearch5\SearchAdapter\Mapper given

I ended up having to override as many as 4 php files and mention them all in di.xml (see https://github.com/Pontorez/magento/commit/6a30e2db0c40152bd442e7c1eb7a0742bf471b00 ). This is METHOD #2.

 

Yes, I got things working without modifying Magento files, but the diff looks so long...

 

As a result, method #1 seems to be preferable because in this case, when it's time to update Magento to a new version, I will have to remember to add just one line to new version of app/code/Magento/Elasticsearch/SearchAdapter/Query/Builder/MatchQuery.php

 

If I use method #2, Magento update process gets harder because there are more files to pay attention to.

 

Am I doing something wrong? Is there a way to override a method without making so many changes?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Am I using the correct way to override a method?

Hello @Pontorez 

 

You need to use below constructor and pass values to parent by using below code:

 public function __construct(
        FieldMapperInterface $fieldMapper,
        AttributeProvider $attributeProvider,
        TypeResolver $fieldTypeResolver,
        ValueTransformerPool $valueTransformerPool,
        Config $config
    ) {
        $this->fieldMapper = $fieldMapper;
        $this->attributeProvider = $attributeProvider;
        $this->fieldTypeResolver = $fieldTypeResolver;
        $this->valueTransformerPool = $valueTransformerPool;
        $this->config = $config;
       parent::__construct($fieldMapper, $attributeProvider, $fieldTypeResolver, $valueTransformerPool,$config);
    }

Then run bin/magento setup:di:compile.

It will resolve issue.
Thank you

Problem solved? Click Accept as Solution!

View solution in original post

2 REPLIES 2

Re: Am I using the correct way to override a method?

Hi @Pontorez ,

 

I think you can use Plugin for this file.

By using Plugin there is no need to modify other files.

 

I dont know which function you are trying to modify but below is just an example.

https://magento.stackexchange.com/questions/307324/magento-2-elastic-search-6-fulltext-search-with-l...

 

Problem Solved? Accept as Solution!

 

Hope it helps!

Thanks

Ankit Jasani

Re: Am I using the correct way to override a method?

Hello @Pontorez 

 

You need to use below constructor and pass values to parent by using below code:

 public function __construct(
        FieldMapperInterface $fieldMapper,
        AttributeProvider $attributeProvider,
        TypeResolver $fieldTypeResolver,
        ValueTransformerPool $valueTransformerPool,
        Config $config
    ) {
        $this->fieldMapper = $fieldMapper;
        $this->attributeProvider = $attributeProvider;
        $this->fieldTypeResolver = $fieldTypeResolver;
        $this->valueTransformerPool = $valueTransformerPool;
        $this->config = $config;
       parent::__construct($fieldMapper, $attributeProvider, $fieldTypeResolver, $valueTransformerPool,$config);
    }

Then run bin/magento setup:di:compile.

It will resolve issue.
Thank you

Problem solved? Click Accept as Solution!