3

I need to replace a few values in a few blocks, starting with category/view or more specifically product/list. I found an excellent writeup by Ben Marks, but as far as I can tell, I can only observe the abstract event and not specify this for specific blocks. This would be my ideal situation. Alternatively, I'd like the method to be a no-op as soon as possible, if it's not the block I want. Is $observer->getBlock()->getName() or something to that effect, my only option or are the cheaper ways to filter my block?

5

I haven't found a "less expensive" way than core_block_to_html_after, unless the block itself has some custom event it dispatches. You could rewrite the block and have it dispatch an event that you'd want but then you may as well just stick all the logic there. That approach has its own drawbacks as well.

You should set the event only in the area that you need it if possible:

<frontend>
    <events>
         <core_block_abstract_to_html_after>
            <observers>
                <my_module_block_to_html_after>
                    <type>singleton</type>
                    <class>my_module/observer</class>
                    <method>blockToHtmlAfter</method>
                </my_module_block_to_html_after>
            </observers>
        </core_block_abstract_to_html_after>
    </events>
</frontend>

This would prevent the method from being called in the admin area where the block type/name you're looking for isn't going to be rendered.

I usually check for class types if appropriate:

if ($observer->getBlock() instanceof Mage_Catalog_Block_Category_View)

I wonder if (and hope) that would perform better than $observer->getBlock()->getName() or similar.

1
  • Aha, thanks. I was wondering if I needed a <reference /> of some kind but that should work fine. I don't want to rewrite the block if possible, cause this isn't an extension one would expect a block rewrite from (template, new functionality or similar) so in any real world store, I'd probably generate a conflict if I did. Thank you very much. – Melvyn Jan 22 '14 at 23:06

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.