0

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...

6
  • anything in var/log folder?
    – Marius
    Oct 17, 2014 at 12:46
  • Logs enabled, nothing in there. I hoped there would be some "class not found" warnings, but no :( Oct 17, 2014 at 12:52
  • By any chance, do you have a _toHtml() method in your block? If yes, post it in the question. If no, then try to add something in the template outside the if statement and see if it prints.
    – Marius
    Oct 17, 2014 at 12:54
  • No _toHtml() method. I already tried printing something outside the if statement, and it's only working when this is not type="wbx_social/opengraph". So I think the problem is whether in config (blocks declaration) or layout config (bad type ?) Oct 17, 2014 at 13:06
  • Change the module alias to "wbxsocial" everywhere instead of "wbx_social" as a last resort?
    – Amasty
    Oct 17, 2014 at 14:51

2 Answers 2

0

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')

1
  • The head.phtml already has this : <?php echo $this->getChildHtml() ?>, meaning that all sorted children are displayed. I checked all head's children, and my opengraph block isn't part of them. If I change the type for a native one (core/template, page/html...), my block appears in head's sorted children. Long story short : echo $this->getChildHtml('opengraph') has no effect as long as the block declaration is type="wbx_social/opengraph" Oct 17, 2014 at 13:57
0

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 !

1
  • 1
    May be you need to enable E_STRICT error level to see such type of errors.
    – Amasty
    Oct 18, 2014 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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