I need to take some simple UL tag generated in PHP (Joomla 1.5) and wrap each anchor tag’s text content with a SPAN tag. The incoming HTML looks like this:
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Watch UNC-TV</a>
</li>
<li>
<a href="#" >Contact</a>
</li>
</ul>
The output needs to look like this:
<ul id="top-nav" class="flatList">
<li class="selected">
<a href="#"><span class="embed embed-top-nav">Home</span>
<p >news, highlights</p></a>
</li>
<li>
<a href="#"><span class="embed embed-top-nav">Watch UNC-TV</span>
<p>schedule, local programs</p></a>
</li>
<li id="nav-last">
<a href="#"><span class="embed embed-top-nav">Contact</span>
<p>feedback, connect, share</p></a>
</li>
</ul>
Also note the class added to the active LI tag (“selected”) and the class added to the last one in the list (“nav-last”). This is in Joomla 1.5, where I’m overriding the mod_mainmenu module used by the Main Menu. The code uses the SimpleXML library to read and write the HTML in the modMainMenuXMLCallback(&$node, $args):
<?php
defined('_JEXEC') or die('Restricted access');
if ( ! defined('fancyMenuPatch') )
{
function fancyMenuPatch($result,$tag){
// Replace UL tag with ours.
// Replace LI tag with ours.
// Add to the start of the UL tag.
$begin_ul = "<ul id=\"top-nav\" class=\"flatList\">";
$begin_li = "<li>"; //not sure what to do with this.
// do the replacement
$result = str_replace("<ul>",$begin_ul, $result);
$result = str_replace("<li>", $begin_li, $result);
return $result;
}
define('fancyMenuPatch', true);
}
if ( ! defined('modMainMenuXMLCallbackDefined') )
{
function modMainMenuXMLCallback(&$node, $args)
{
$user = &JFactory::getUser();
$menu = &JSite::getMenu();
$active = $menu->getActive();
$path = isset($active) ? array_reverse($active->tree) : null;
if (($args['end']) && ($node->attributes('level') >= $args['end']))
{
$children = $node->children();
foreach ($node->children() as $child)
{
if ($child->name() == 'ul') {
$node->removeChild($child);
}
}
}
if ($node->name() == 'ul') {
foreach ($node->children() as $child)
{
if ($child->attributes('access') > $user->get('aid', 0)) {
$node->removeChild($child);
}
}
}
if (($node->name() == 'li') && isset($node->ul)) {
$node->addAttribute('class', 'parent');
}
if (isset($path) && (in_array($node->attributes('id'), $path) || in_array($node->attributes('rel'), $path)))
{
if ($node->attributes('class')) {
$node->addAttribute('class', $node->attributes('class').' active');
} else {
$node->addAttribute('class', 'active');
}
}
else
{
if (isset($args['children']) && !$args['children'])
{
$children = $node->children();
foreach ($node->children() as $child)
{
if ($child->name() == 'ul') {
$node->removeChild($child);
}
}
}
}
if (($node->name() == 'li') && ($id = $node->attributes('id'))) {
if ($node->attributes('class')) {
$node->addAttribute('class', $node->attributes('class').' item'.$id);
} else {
$node->addAttribute('class', 'item'.$id);
}
}
if (isset($path) && $node->attributes('id') == $path[0]) {
$node->addAttribute('id', 'current');
} else {
$node->removeAttribute('id');
}
$node->removeAttribute('rel');
$node->removeAttribute('level');
$node->removeAttribute('access');
}
define('modMainMenuXMLCallbackDefined', true);
}
ob_start();
modMainMenuHelper::render($params, 'modMyMainMenuXMLCallback');
$menu_html = ob_get_contents();
ob_end_clean();
if($params->get('menutype')=="primarynav"){
$tag = $params->get('tag_id');
}
//output the menu!
echo fancyMenuPatch($menu_html,$tag);
?>
Thank you.
Ok so here is how you want to do this. Jump out of the default.php script you posted above. As Joomla! uses the core mainmenu module for all menus and trees down from that you will need to edit the helper script. This should stick regardless of any 3rd party menu extension you may be using.
Now jump down to the switch statement at line 358. Specifically we want to edit line 363. It looks like this:
Now edit it to be this:
there you go, now menus utilized by the Joomla! getMenu class will add this span tag around the links.
Note that you may also want to do this to line 375 for links that will open in a new window et cetra. Also note Louis’ funny comment ” // hrm…this is a bit dickey”.
cheers