In my observer for core_layout_block_create_after

I am checking

public function addButtonBlock($observer)
    {
        $block = $observer->getEvent()->getBlock();

        if (($block->getAction()->getFullActionName() == 'catalog_product_view') && ($block->getNameInLayout() == 'product.info')) {
            }}

It's working fine on Product View page , while on review list page with the form its giving an error

PHP Fatal error: Call to a member function getFullActionName() on string in /var/www/html/meemee/app/code/community/Tebet/SocialShare/Model/Observer.php on line 15, referer: http://localhost/meemee/asd.html

I am able to get an echo for review_product_list for $block->getAction()->getFullActionName() but while using it in if condition it throws error

up vote 6 down vote accepted

This is an interesting problem here.

Basically the problem is that the getAction method declared in Mage_Core_Block_Abstract is definitely what you need to use here and your code is definitely right.

It is supposed to return a Mage_Core_Controller_Varien_Action that you can use to get the full action name.

However, the Mage_Review_Block_Form block entirely rewrites the getAction method with the following code:

public function getAction()
{
    $productId = Mage::app()->getRequest()->getParam('id', false);
    return Mage::getUrl('review/product/post', array('id' => $productId, '_secure' => $this->_isSecure()));
}

That code returns a string so you cannot call the getFullActionName method on it, that's the problem.

I'm not sure why the Magento team did use the same name as such an important function of the parent block, sounds like a bug to me.

What you can do avoid that is:

Either use the following code instead:

Mage::app()->getFrontController()->getAction()->getFullActionName()

Or if you want to do things for a specific block and not every block created, add a condition to test the block class:

if ($block instanceof The_Block_You_Want)
{
    if (($block->getAction()->getFullActionName() == 'catalog_product_view') && ($block->getNameInLayout() == 'product.info'))
        {
              //
        }
}

Of course that won't work if the block you want to test is the Mage_Review_Block_Form ^^

On a side note, please note that the following block also rewrite the getAction method: Mage_Customer_Block_Newsletter (and it also returns a string)

  • 1
    Good find! And a textbook argument for strict return types :) – Fabian Schmengler Apr 8 '16 at 11:40
  • 1
    true. really looking forward for return types beeing implemented in a far future :x – David Verholen Apr 8 '16 at 17:44
  • Yeah can't wait :) – Raphael at Digital Pianism Apr 8 '16 at 18:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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