How to override community block module with php fi

2019-09-17 17:28发布

问题:

I would like to override a "community" Block within "Script.php" file.

Override third party module as http://connect20.magentocommerce.com/community/Anaraky_GDRT_1

Magento version : 1.7.0

Here's community Module file tree:

-app
 - code 
  - community
   - Anaraky
    - Gdrt
     - Block
      - Script.php 

     - etc
      - Config.xml
     - Helper
      - Data.php
     - Module
      - Adminhtml
      - Observer.php

I want to override Script.php file to My Custom module

Here's my custom module file tree :

- app 
  - code 
    - local 
      - Anarakyoverride 
       - Gdrtoverride 
        - Block 
          - Script.php 
        - etc
         - config.xml

Also, I have add code config.xml file of My Custom (Anarakyoverride_Gdrtoverride) module as following code

<?xml version="1.0"?>
<config>
    <modules>
        <Anarakyoverride_Gdrtoverride>
            <version>1.0.8</version>
        </Anarakyoverride_Gdrtoverride>
    </modules>
    <global>        
        <blocks>
            <gdrt>
                <class>Anarakyoverride_Gdrtoverride_Block</class>
            </gdrt>
        </blocks>
    </global>

</config>

Can any one help me and suggest to override third party module?

More detail of module is http://connect20.magentocommerce.com/community/Anaraky_GDRT_1 Installed module and override script.php file in your custom module i.e app/code/local/Anarakyoverride/Gdrtoverride/Block/Script.php

Thanks and Regards, Vishal

回答1:

Try below code in your XML

<global>
    <modules>
        <Anarakyoverride_Gdrtoverride>
            <version>1.0.8</version>
        </Anarakyoverride_Gdrtoverride>
    </modules>
    <blocks>
        <gdrtoverride><class>Anarakyoverride_Gdrtoverride_Block</class></gdrtoverride>
        <gdrt>
            <rewrite>
                    <script>Anarakyoverride_Gdrtoverride_Block_Script</script>
            </rewrite>
        </gdrt>
    </blocks>
   /* Only if you want */
    <models>
        <gdrt>
            <rewrite>
                <state>Anarakyoverride_Gdrtoverride_Model_State</state>
            </rewrite>
        </gdrt>
    </models>
   /* Only if you want */
</global>   
<frontend>
  <layout>   
        <updates>   
            <gdrtoverride>   
                <file>gdrtoverride.xml</file>   
            </gdrtoverride>   
        </updates>   
    </layout>
</frontend>   
</config>

OR only block node for your XML

<blocks>
        <gdrtoverride>
            <class>Anarakyoverride_Gdrtoverride_Block</class>
        </gdrtoverride>
    <gdrt>
        <rewrite>
            <script>Anarakyoverride_Gdrtoverride_Block_Script</script>
        </rewrite>
    </gdrt>
</blocks>

then in your script.php

<?php
   class Anarakyoverride_Gdrtoverride_Block_Script extends Anaraky_Gdrt_Block_Script
   {
        // create your method and write your code
   }

Also you need to copy this exact configuration to your module config - specifying your block classname - and (important!) make sure that the following occur:

  1. Your configuration is loaded after the community module's configuration
  2. Your class extends from the community module's class.

To load your module configuration after the community module, you can make use of <depends> tag in your app/etc/modules/

<depends>
    <Community_Module /> <!-- e.g. -->
</depends>