0

I'm using Magento 1.9.1.1 with the latest patches on PHP 5.6.

I am attempting to add a dynamic link to the top.links nav on the frontend. I have managed to get to the point where my block seems to be loaded, but I do not see anything from my template. Here are the relevant files:

app/design/frontend/callcenter/default/layout/local.xml

<?xml version="1.0" ?>
<layout>
    <default>
        <reference name="top.links">
            <block type="callcenter_frontadmin/adminaccount" name="adminaccount" template="frontadmin/adminaccount.phtml" />
        </reference>
        <remove name="left.permanent.callout"/>
        <remove name="right.permanent.callout"/>
    </default>
</layout>

app/design/frontend/callcenter/default/template/frontadmin/adminaccount.phtml

Hello, world!

My block is located in app/code/local/Callcenter/Frontadmin/Block/Adminaccount.php and is in fact being loaded (if I die() inside it, the site dies). If I substitute the <block> tag for something else, like an <action method="addLink" whatever I add seems to appear in the correct place.

Caching is turned off. Logging does not reveal anything suspicious in the logfiles.

EDIT: Here is my block

app/code/local/Callcenter/Frontadmin/Block/Adminaccount.php

<?php

class Callcenter_Frontadmin_Block_Adminaccount extends Mage_Core_Block_Template {

}

3 Answers 3

3

the top.links block is an instance of a template block. As such it doesn't automatically render child blocks. To render your block as a child of top.links you will have to edit the page/template/links.phtmltemplate within your theme and add an explicit render like this:

echo $this->getChildHtml('adminaccount');

...

gonna correct myself slightly, you should also be able to do this:

    <reference name="top.links">
        <action method="addLinkBlock"><blockName>adminaccount</blockName></action>
    </reference>

within your existing default node

1
  • You were almost correct - I had to put both the action AND the block tag under the reference tag, and now I can see a new tag in my header. Thank you!
    – AlexMax
    Mar 3, 2016 at 14:32
0

UPDATE 1

Try this way from xml:

<?xml version="1.0"?>
<layout>
<default>
<reference name="top.links">
  <action method="addLink" translate="label title">
       <label>Blog</label>
       <url>/blog</url>
       <title>Blog</title>
       <prepare/>
       <urlParams/>
       <position>1</position>
       <liParams />
       <aParams><![CDATA[target="_blank"]]></aParams>
  </action>
  </reference>
  <remove name="left.permanent.callout"/>
  <remove name="right.permanent.callout"/>
</default>

We are utilising a method called addLinks from Mage_Page_Block_Template_Links.php

/**
 * Add link to the list
 *
 * @param string $label
 * @param string $url
 * @param string $title
 * @param boolean $prepare
 * @param array $urlParams
 * @param int $position
 * @param string|array $liParams
 * @param string|array $aParams
 * @param string $beforeText
 * @param string $afterText
 * @return Mage_Page_Block_Template_Links
 */
public function addLink($label, $url='', $title='', $prepare=false, $urlParams=array(),
    $position=null, $liParams=null, $aParams=null, $beforeText='', $afterText='')
{
    if (is_null($label) || false===$label) {
        return $this;
    }
    $link = new Varien_Object(array(
        'label'         => $label,
        'url'           => ($prepare ? $this->getUrl($url, (is_array($urlParams) ? $urlParams : array())) : $url),
        'title'         => $title,
        'li_params'     => $this->_prepareParams($liParams),
        'a_params'      => $this->_prepareParams($aParams),
        'before_text'   => $beforeText,
        'after_text'    => $afterText,
    ));

    $this->_addIntoPosition($link, $position);

    return $this;
}

I am ignoring your app/code/local/Callcenter/Frontadmin/Block/Adminaccount.php class, if you have any function inside there, then you will need to create a simple extension for better approach.

OR like @Dave suggested

You will have to call <?php echo $this->getChildHtml('adminaccount');?> from your page/template/links.phtml file.

I would still call it from xml, easy to maintain in future.

4
  • Tried that. Still nothing being rendered. I added the block to the original post.
    – AlexMax
    Mar 2, 2016 at 22:23
  • ok, can you remove <default> and </default> completely. check my updated answer, also remove space from <?xml version="1.0" ?> Mar 2, 2016 at 22:24
  • Removing the default tags did not result in the template being rendered, and additionally resulted in the two callout blocks that I had removed returning.
    – AlexMax
    Mar 2, 2016 at 22:26
  • Sorry about that, you definitely need <default> Mar 2, 2016 at 22:34
0

If you are using modman to deploy your module, make sure that your Admin -> Configuration -> Advanced -> Developer -> Template settings -> Allow symlinks is "Yes".

This can also be done with an SQL query:

update core_config_data \
    set value = '1' \
    where path = 'dev/template/allow_symlink';

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.