1

I am building a simple Joomla Sidebar module and I want to display fontawesome icons into each menu item using the Link CSS Style.

enter image description here

Which param is responsible for this? How can I get this param from Joomla and display icons into my menu item like this:

<?php $getfontawesomeiconhere = htmlspecialchars($params->get('class_sfx')); ?>
<a class='$getfontawesomeiconhere' href='#_'>$tlink[0]</a>

This is how my menu is build:

// Render the menu item.

echo '<li' . $class . '>';
$tlink = array();
switch ($item->type):
case 'separator':
case 'url':
case 'component':
case 'heading':
    $tlink = modTriggerMenuHelper::getUrlLink($item);
    break;

default:
    $tlink = modTriggerMenuHelper::getUrlLink($item);
    break;
endswitch;

if (($item->type) == 'heading')
    {
    echo "<a href='#_'>$tlink[0]</a>";
    }
  else
if (($item->type) == 'separator')
    {
    echo "<a href='#_'>$tlink[0]</a>";
    }
  else
if (count($tlink) == 3)
    {
    echo "<a href='$tlink[1]'>$tlink[0]</a>";
    }

if ($item->deeper)
    {
    echo '<ul>';
    }
elseif ($item->shallower)
    {

    // The next item is shallower.

    echo '</li>';
    echo str_repeat('</ul></li></li>', $item->level_diff);
    }
  else
    {

    // The next item is on the same level.

    echo '</li></li>';
    }
  • Are you trying to create a template override for the mod_menu, or what? The code you presented here says nothing by itself. It would only make sense if that is a modification of the default.php template of mod_menu. – FFrewin Sep 4 '15 at 21:51
1

Assuming you are modifying the mod_menu's default.php template, then to access the "Link CSS Style" param, all you need is $item->anchor_css.

But the above answer would be valid only for the mod_menu and only in the context of:

$list = ModMenuHelper::getList($params);
foreach ($list as $i => &$item)
{
$class = $item->anchor_css;
}

Update following the dialogue in the comments section:

Jakub Skowroński:: I building separate menu module. I tried your method but I cant get the class.

FFrewin:: Of course you can't get the class if you are in a module of yours and you haven't created such class.

FFrewin:: The module you are creating, what will do that will be different from the mod_menu ?

Jakub Skowroński:: Different function and styling. Is it really so hard to get this one param to other module?

FFrewin:: It's no hard at all. But php variables get their meaning when used inside a specific context. In your code you are using an $item which seems to be an object from the way you use it. However, what object is this, is not possible to know from the code you have presented. Therefore we can't know what properties or methods this object may have.

I asked for what your module will do differently than the mod_menu, because the code seems to be similar to the mod_menu, and I am not sure if you really need to create a new module, or simply create a template override for the mod_menu, where you can easily use the $list array of $item objects (generated by the ModMenuHelper::getList(), to access each $item object's properties.

Whatever the case, you can study the ModMenuHelper::getList($params) method, to see how it returns the $list array that is populated with the menu items and their properties as objects, of a specific menu that is passed as an argument and has been previously selected in the backend of the module. Then you can modify it to suit your needs inside your helper.php file.

Below as a short snippet, similar to the one inside the mod_menu helper file. which is addressing how to access the css anchor property of menu items, this snippet example may help:

$app   = JFactory::getApplication();
$menu  = $app->getMenu();
$items = $menu->getItems('menutype', 'mainmenu');

if ($items)
{
    foreach ($items as $i => $item)
    {
        $item->anchor_css   = htmlspecialchars($item->params->get('menu-anchor_css', ''), ENT_COMPAT, 'UTF-8', false); 
        //Above we are assigning the anchor_css protected property, to another object property 'anchor_css' for easy access.
    }
}

echo "<pre>" . print_r($items, true)."</pre>";
//or 
echo $items[0]->anchor_css;

Documentation: JMENU SITE API

  • I building separate menu module. I tried your method but I cant get the class. An a looks like this: <a class="" href="#_">Menu item</a> – Frostbourn Sep 5 '15 at 8:52
  • Of course you can't get the class if you are in a module of yours and you haven't created such class. – FFrewin Sep 5 '15 at 8:53
  • Is it possible that you can tell me how to create this class? Because I tried everything and cant get this work. – Frostbourn Sep 5 '15 at 9:23
  • The module you are creating, what will do that will be different from the mod_menu ? – FFrewin Sep 5 '15 at 11:47
  • Different function and styling. Is it really so hard to get this one param to other module? – Frostbourn Sep 6 '15 at 9:22
0

You need add a class in your item Menu like "fa" <- Its ok; Now, you need create CSS class:

.fa:before {
    content: "\f054"; /* Your content icon http://astronautweb.co/snippet/font-awesome/ */
    font-family: FontAwesome;
    float: left;
    padding-left: 0px 10px;
}

You can create one class for each menu item;

.fa2:before {
    content: "\f080"; /* Your content icon http://astronautweb.co/snippet/font-awesome/ */
    font-family: FontAwesome;
    float: left;
    padding-left: 0px 10px;
}
.fa3:before {
    content: "\f150"; /* Your content icon http://astronautweb.co/snippet/font-awesome/ */
    font-family: FontAwesome;
    float: left;
    padding-left: 0px 10px;
}
  • I know how to display this icons via css. But i want to display then through link css style param. And then get the style param from the backend to my menu items. – Frostbourn Sep 4 '15 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.