Skip to content

Instantly share code, notes, and snippets.

@vbuck
Created August 3, 2015 19:14
Show Gist options
  • Save vbuck/ba89b97ac2b54bbab435 to your computer and use it in GitHub Desktop.
Save vbuck/ba89b97ac2b54bbab435 to your computer and use it in GitHub Desktop.
The Missing Magento Customer Account Navigation Block
<?php
/**
* Customer account navigation block extensions.
*
* PHP Version 5
*
* @category Class
* @package Vbuck_Common
* @author Rick Buczynski <me@rickbuczynski.com>
* @copyright 2015 Rick Buczynski. All Rights Reserved.
*/
/**
* Class declaration
*
* @category Class_Type_Block
* @package Vbuck_Common
* @author Rick Buczynski <me@rickbuczynski.com>
*/
class Vbuck_Common_Block_Customer_Account_Navigation
extends Mage_Customer_Block_Account_Navigation
{
/**
* Sort links just before rendering.
*
* @return Mage_Core_Block_Abstract
*/
protected function _beforeToHtml()
{
uasort($this->_links, array($this, 'sortLinks'));
return parent::_beforeToHtml();
}
/**
* Link registration override to support positioning.
*
* @param string $name The link identifier.
* @param string $path The route.
* @param string $label The visible link label.
* @param array $urlParams Optional route parameters.
* @param int $position An optional position of the link.
*
* @return Vbuck_Common_Block_Customer_Account_Navigation
*/
public function addLink($name, $path, $label, $urlParams = array(), $position = 250)
{
parent::addLink($name, $path, $label, $urlParams);
if (isset($this->_links[$name])) {
$this->_links[$name]->setPosition($position);
}
return $this;
}
/**
* Clear all registered links.
*
* @return Vbuck_Common_Block_Customer_Account_Navigation
*/
public function clearLinks()
{
$this->_links = array();
$this->_activeLink = false;
return $this;
}
/**
* Edit an existing link.
*
* @param string $name The link identifier.
* @param string $path The route.
* @param string $label The visible link label.
* @param array $urlParams Optional route parameters.
* @param int $position An optional position of the link.
*
* @return Vbuck_Common_Block_Customer_Account_Navigation
*/
public function editLink($name = '', $path = '', $label = '', $urlParams = array(), $position = 250)
{
if (isset($this->_links[$name])) {
$this->_links[$name] = new Varien_Object(
array_merge(
$this->_links[$name]->getData(),
array_filter(
array(
'name' => $name,
'path' => $path,
'label' => $label,
'url' => $path ? $this->getUrl($path, $urlParams) : '',
'position' => $position,
)
)
)
);
}
return $this;
}
/**
* Remove an existing link.
*
* @param $name The link name to remove.
*
* @return Vbuck_Common_Block_Customer_Account_Navigation
*/
public function removeLink($name)
{
if (isset($this->_links[$name])) {
unset($this->_links[$name]);
}
return $this;
}
/**
* Internal link comparator.
*
* @param Varien_Object $a A link object.
* @param Varien_Object $b A link object.
*
* @return int
*/
public function sortLinks($a, $b)
{
if ($a->getPosition() === $b->getPosition()) {
return 0;
}
return $a->getPosition() < $b->getPosition() ? -1 : 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment