How to override a link's href attribute in mod_menu?

by Xander   Last Updated October 14, 2019 17:10 PM

Better title might be 'How to set the menuItem object 'link' property?'

I've used menu overrides for a variety of purposes such as changing layout and styling etc but would now like to change the href of a menu item with a certain alias.

By default, the attributes are built like so:

$attributes = array();

if ($item->anchor_title)
{
    $attributes['title'] = $item->anchor_title;
}

if ($item->anchor_css)
{
    $attributes['class'] = $item->anchor_css;
}

if ($item->anchor_rel)
{
    $attributes['rel'] = $item->anchor_rel;
}

Ideally I'd like something like:

switch ($item->alias) {
  case 'specificAlias':
    $attributes['href'] = "www.desired_link.com";
    $linktype = $item->title;
    break;
  default:
    $linktype = $item->title;
}

I've also tried this:

$item['link'] = "www.theDesiredLink.com";

I will be working out the URL dynamically which is why I can't set it manually for the menu item.



Answers 1


Need to use the 'flink' property in the $item object.

Full solution was as follows:

switch ($item->alias) {
  case 'a specific alias':
    $item->flink = "the desired URL";
    $linktype = $item->title;
    break;
  default:
    $linktype = $item->title;
}
Xander
Xander
October 14, 2019 14:40 PM

Related Questions


Updated May 26, 2015 11:04 AM

Updated March 27, 2015 05:03 AM

Updated July 07, 2015 12:43 PM

Updated June 05, 2015 22:04 PM

Updated March 28, 2016 08:04 AM