Question

I'm using Magento EE 1.6

When I use a custom block class to display some tags inside my <head>, block won't display. But if I use the "page/html" type, everything is fine... I need my custom class to add some features to the block (activation, getting image url etc).

Please help me to understand what I missed. Here is what I've done so far :

app/etc/modules/Wbx_Social.xml :

<?xml version="1.0"?>
<config>
    <modules>
        <Wbx_Social>
            <active>true</active>
            <codePool>local</codePool>
        </Wbx_Social>
    </modules>
</config>

app/code/local/Wbx/Social/etc/config.xml :

<?xml version="1.0"?>
<config>
    <modules>
        <Wbx_Social>
            <version>0.1.0</version>
        </Wbx_Social>
    </modules>
    <global>
        [...]
        <blocks>
            <wbx_social>
                <class>Wbx_Social_Block</class>
            </wbx_social>
        </blocks>
    </global>
    <frontend>
        <layout>
            <updates>
                <wbx_social>
                    <file>wbx_social.xml</file>
                </wbx_social>
            </updates>
        </layout>
    </frontend>
</config>

app/code/local/Wbx/Social/Block/Opengraph.php :

class Wbx_Social_Block_Opengraph extends Mage_Core_Block_Template
{
    /**
     * @return int
     */
    public function isActive()
    {
        return true; // Will be replaced with store config value
    }

    /* And other useful functions */

}

app/design/frontend/responsive/default/layout/wbx_social.xml :

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="head">
            <block type="wbx_social/opengraph" name="opengraph" as="opengraph" template="social/opengraph.phtml"/>
        </reference>
    </default>
</layout>

And finally the template : app/design/frontend/responsive/default/template/social/opengraph.phtml :

<?php /* @var $this Wbx_Social_Block_Opengraph */ ?>
<?php if ($this->isActive()) : ?>
    <meta property="fb:app_id"       content="an id"/>
    <meta property="og:type"         content="website"/>
    <meta property="og:site_name"    content="a name"/>
<?php endif; ?>

Just in case, I didn't forget the Wbx_Social_Helper_Data empty helper.

In wbx_social.xml, when I try changing the block class for type="core/template" or type="page/html" (for example), the block is correctly displayed (without any access of custom functions of course)

I really need a hint, it's been several hours I'm tearing out...

Was it helpful?

Solution 2

Ok I found out the origin of the problem. The problem was indeed from my block Wbx_Social_Block_Opengraph that implemented a getUrl() method. Big mistake ! Mage_Core_Block_Abstract (an ancestor of my block) has already a getUrl() method but with a different signature. My real big question is : why the heck didn't I get any error / warning ?? It would have saved so much time...

Anyway, I changed my own method for getOgUrl() and VOILA ! My tags are displayed as I wanted....

Thanks a lot for your suggestions, folks !

OTHER TIPS

There are several block types in Magento. See app\code\core\Mage\Core\Block\

One of them is "list". It gets all children and just output them one by one. The content block works like this.

But most of blocks have type "template". It means they have some .phtml templates associated and render them. For example, your block has such type.

To add some block in the head ( type="template" ), you have to edit the head.phtml file and call the child block using $this->getChildHtml('opengraph')

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top