Mage_Page_Block_Template_Links
Sonassi here で概説されているメソッドを使用して、Magento クラスを拡張しました。
関数を編集し_construct
てカスタム テンプレートを含める:-
protected function _construct()
{
$this->setTemplate('page/template/links_nested.phtml');
}
関数を編集して変数addLink
を含め$childMenu
、これを XML レイアウトで使用できるようにします。
public function addLink($label, $url = '', $title = '', $prepare = false, $urlParams = array(), $position = null, $liParams = null, $aParams = null, $beforeText = '', $afterText = '', $childMenu = false)
{
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,
'child_menu' => ($childMenu ? $this->getLayout()->getBlock($childMenu) : '')
));
$this->_links[$this->_getNewPosition($position)] = $link;
if (intval($position) > 0) {
ksort($this->_links);
}
return $this;
}
次に<childMenu>
、topLinks のアイテムの local.xml にパラメーターを含めます。
<reference name="top.links">
<action method="addLink" translate="label title before_text">
<label>Account</label>
<url />
<title>Account</title>
<prepare />
<urlParams />
<position>10</position>
<liParams>id="account-dropdown"</liParams>
<aParams />
<before_text />
<after_text />
<childMenu>account-submenu</childMenu>
</action>
</reference>
そして、childMenuを次のように構築しますaccount-submenu
:-
<reference name="top.links">
<block type="page/template_links" name="submenu" as="submenu">
<action method="setName">
<name>account-submenu</name>
</action>
<action method="addLink" translate="label title before_text">
<label>Contact Us</label>
<url />
<title>Contact Us</title>
<prepare />
<urlParams />
<position>110</position>
<liParams />
<before_text />
<after_text />
</action>
</block>
</reference>
childMenu
次に、宣言されている場合にレンダリングするテンプレート ファイルにいくつかの変更を加えました。
<?php $_links = $this->getLinks(); ?>
<?php if(count($_links)>0): ?>
<ul class="links nav"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php foreach($_links as $count=>$_link): ?>
<?php if ($_link instanceof Mage_Core_Block_Abstract):?>
<?php echo $_link->toHtml() ?>
<?php else: ?>
<li<?php if($_link->getIsFirst()||$_link->getIsLast()||$count): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?> link-<?php echo $count ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>>
<?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?>
<?php var_dump($_link->getChildMenu()); ?>
<?php echo ($_link->getChildMenu()) ? $_link->getChildMenu()->toHtml() : ''; ?>
</li>
<?php endif;?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
childMenu
フロントエンドで何もレンダリングされないという事実を除けば、すべてが期待どおりに機能しているため、「マイアカウント」のトップリンクにはサブメニューが含まれていません。
childMenu
テンプレート ファイルの呼び出しに何か問題がありますか?
<?php echo ($_link->getChildMenu()) ? $_link->getChildMenu()->toHtml() : ''; ?>