2

I want to know some detail in config.xml about rewriting block for exemple, usually when we create a block we need to declare it in a xml like this:

<global>
    <blocks>
        <moduleName> 
            <class>moduleNameSpace_moduleName_Block</class>
        </moduleName>
    </blocks>
</global>

But when we rewrite some module we dont declare it :

We take product_view block rewrite for exemple

    <global>
        <blocks>
            <!--we haven't this-->
        <!--<moduleName>
                <class>moduleNameSpace_moduleName_Block</class>
            </moduleName>-->
            <catalog>
                <rewrite>
                    <product_view>moduleNameSpace_moduleName_Block_Product_View</product_view>
                </rewrite>
            </catalog>
        </blocks>
    </global>

I know that it works but i want to know the explanation.

  • For your "we havent this" comment ... of course we have, its used for every custom block type?!? Or did i got it wrong? – sv3n Jun 24 '17 at 14:38
  • Look sv3n, For now I have just rewrite product_view block of catalog module by product_view block of my custom module catalog, So in my module I have well created my block like this: Block/Product/View.php But what I want to know is that since I have created my block In my custom module, logically I have to declare it in config.xml to tell to Magento: i have a block and then I tell him that this block Product/View.php it will rewrites the natif block of catalog module like my exemple. for information my rewrites works fine, i have just to know this detail :) – PЯINCƏ Jun 24 '17 at 15:09
2

In your first declaration

<global>
    <blocks>
        <moduleName> 
            <class>moduleNameSpace_moduleName_Block</class>
        </moduleName>
    </blocks>
</global>

You define this for your main module where blocks of your module are defined.

Now if you want to override/rewrite a block or model or helper to notify magento you need to use <rewrite> tag.

<global>
    <blocks>
        <catalog>
            <rewrite>
                <product_view>moduleNameSpace_moduleName_Block_Product_View</product_view>
            </rewrite>
        </catalog>
    </blocks>
</global>

Breaking this up is easy. In the string, catalog, tag you have defined is the module name magento looks for which in this case is Catalog. The node goes inside of this. Then, inside <rewrite/> you define the block you need to override which is product_view so Magento looks for Catalog/Block/Product/View.php file.

And, moduleNameSpace_moduleName_Block_Product_View is the name of the class that is being loaded instead of catalog/product_view.

  • Thank you for your time but i know all this, i know how the rewrite works. My question is just why we dont declare our exsisting block. Logically we tel to magento : i have block then i want to rewrite some block of some module like my exemple – PЯINCƏ Jun 24 '17 at 10:00
2

Still not sure if i got it right ... but let's try :)

For example look at your code and catalog.xml where you can find

<block type="catalog/product_view"

Rewrite:

<blocks>
    <catalog>
        <rewrite>
            <product_view>Namespace_Name_Block_Product_View</product_view>
        </rewrite>
    </catalog>
</blocks>

In this case all blocks of tpye catalog/product_view would use your logic from your block class.

Own class:

<blocks>
    <!--We have this -->
    <moduleName>
        <class>Namespace_Name_Block</class>
    </moduleName>
</blocks>

This had no effect to catalog.xml <block type="catalog/product_view", but you can use tthis for your own (rewritten) blocks:

<block type="moduleName/product_view"
  • So if I understood correctly Magento could detect in the rewrite <product_view>Namespace_Name_Block_Product_View</product_view> my block without having to declare it before, the <!--We have this --> block ! the explanation is (we have entred the full class block Namespace_Name_Block_Product_View) so he dont need to check it in the xml declaration block. And if I ever have to use another simple block (not rewrite), at this moment I have to declare it as in my example <!--We have this --> block. right ? – PЯINCƏ Jun 25 '17 at 15:28
  • Right. Hope this answered yor question. :) – sv3n Jun 25 '17 at 17:11
  • Thank you for your time, the first answer is correct so I accept it but +1 – PЯINCƏ Jun 25 '17 at 23:48

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.