Custom Menu Items

Custom Menu Items

by Robert Tamblyn -
Number of replies: 51

I've been experimenting with custom menu items displayed by some themes.  I'm wondering if there is a way to differentiate items for certain courses.  For example, course 1 display certain items and course 2 display a completly different set.  A second question regarding custom menu items involves adding a small graphic/icon in addition to text.

Thanks.

Average of ratings: -
In reply to Robert Tamblyn

Re: Custom Menu Items

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Robert,

I believe from one of your other posts you are using Moodle2?

As far as my understanding goes, what you want would only be possible with the custom menus by over-riding the renderer - and even then, I'm not sure how you would go about differentiating which menus would be visible in which courses.

However, it should be possible to use a menu of the style present in many of the 1.9 themes (such as the aardvark series and those based on it) and using conditional statements based on the course id to show a particular menu item in a particular course. Using this method it would also be fairly straightforward to add a small icon along with the text for the menu item.

If you want any further help, I'd be happy to take a look at this for you sometime over the next few days.

Richard

In reply to Robert Tamblyn

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

You can set an id or a class in the custom-menu when you are writing it in the themes setting's page. I worked this out a few months ago when I was looking for another way of styling the menu. It actually worked, but it was a real pain to do. I think I added a style like this...

Moodle|class="topmenu" http://moodle.org/

At least I think that's how I did it.

Mary

In reply to Mary Evans

Re: Custom Menu Items - how to customize this? Help please

by Cindy Zhao -

Hi Mary and all,

I am using Moodle 2.0 and need some help on how to customize "custom menu".  I use "formal- white" theme. I tried to edit/add a few lines in Formalwhite.css to change the size and color of the menu links, but can only change the background color, not the links. It should be pretty simple. Am I at the wrong palce? Here are my codes:

#custommenu{
margin-right: 17px;
margin-left: 17px;
font-size: 2em;
background:#006;
color:#FF6;
}

#custommenu a.link, #custommenu a.visited{
color:#FFF;
font-size: 2em;
font-weight:bold;
}

#custommenu a.hover, #custommenu a.active{
color:#FF0;
font-size: 2em;
font-weight:bold;
}

Thank you very much!!!

Cindy

In reply to Cindy Zhao

Re: Custom Menu Items - how to customize this? Help please

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Cindy,

Added your code into menu.css and parts of it (such as the font size) work but the colours do not. This appears to be because the elements that work are not being set by yui, but the parts which are set by yui override what you are coding.

Your best bet is to use a tool such as firebug to identify the exact css rules which need to be set for how you want the menu to look.

Richard

In reply to Richard Oelmann

Re: Custom Menu Items - how to customize this? Help please

by Cindy Zhao -

Richard,

Thank you. I will try firebug.  I used Moodle over the years and knew my way around the older versions. I have to say I am quite lost in Moodle 2 as how to customize themes. I think Firebug will help. Just vew source code  in broswer last night gave me some clues.

Thanks again.

Cindy

In reply to Cindy Zhao

Re: Custom Menu Items - how to customize this? Help please

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Cindy,

I have found firebug invaluable looking at the themes and the css, since Mary Evans tipped me off to it a while ago. Although I have to say that the custom menu and the yui styling are possibly the most complicated bits of any theme to deal with!

Keep on going and it will come together smile Lots of help available through these forums - and that's been just as valuable as firebug (no - it's more valuable!)

Richard

In reply to Mary Evans

Re: Custom Menu Items

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Mary,

Knowing how busy you are, there's no rush with this request, but do you have any other info, or a reference I can check out for adding classes to the custom menu? I've been trying without success so far and searched through moodle.org finding anything. Was thinking that between this and the custom css in many themes, it would be another step towards site customisation from within the admin pages themselves.

Rich

In reply to Richard Oelmann

Re: Custom Menu Items

by Vijay N -

In terms of customising themes in Moodle (1.0 and 2.0) I found it easier to do this using Google chrome "inspect element" option. I personally like this tool over firebug (may be because I am not aware of any advanced tools used in firebug other than the normal inspect element).

The custom menu however is some thing I would like to learn more on because it runs on YUI and so understanding javascripts is a must before changing custom menu (I am not talking about adding css to it).

In reply to Vijay N

Re: Custom Menu Items

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Vijay,

The custom menu can be changed by using the override custom renderers technique using mainly php rather than javascript programming and there are a number of themes which do this to learn from.

I haven't personally used the Google version, although I gather a number of other people do and also find it useful - it doesn't really matter which one provided you are confident with what it gives you (I'm sure there must be similar add ons and tools for other browsers too). I personally like the firebug/firefox combination mainly because i use firefox as my browser - and I'm still learning about some of the more advanced options in it too!

Richard

In reply to Richard Oelmann

Re: Custom Menu Items

by Trem_r Trem_r -

Hi,

You say there are a number of themes to learn from, could you please name some ? I'm looking for custom renderer with dynamic data specifically !

Thanks

In reply to Trem_r Trem_r

Re: Custom Menu Items

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

I'd suggest starting with decaf (or krystle which uses most of the decaf custom menu code anyway) or flexi_ii. I know many/all of Mary's wonderful themes also have custom renderers to over-ride the main menu.

What sort of dynamic data are you looking to use in the menu?

In reply to Trem_r Trem_r

Re: Custom Menu Items

by Sam Hemelryk -

Hi Trem,

Perhaps of use depending upon your confidence with PHP: Development:Themes_2.0_extending_the_custom_menu

Cheers
Sam

In reply to Sam Hemelryk

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Trem,

Decaf was the theme I was thinking of. If you want to try out a menu I created/modified some time ago you can download this zip file which contains a theme which uses a dynamic menu, that put categories as topline menu titles and adds courses and sub categories to the dropdown menu. But this only works in Moodle 1.9.

Moodle 2. version of this theme is on a backburner.

HTH

Mary

In reply to Mary Evans

Re: Custom Menu Items

by Trem_r Trem_r -
Hi, Thank you but I'm bound to use Moodle 2 as I'm working on a sitewide migration and redesign to this version. Maybe I'll find some hints in the zip ! Thank you again
In reply to Sam Hemelryk

Re: Custom Menu Items

by Trem_r Trem_r -
Hi Sam, I used you tutorial and it works fine, but I have a lot of trouble adapting it to my user's demands, which are either to display categories or categories and courses in submenus. I'm new to Moodle dev like I said in an another post (http://moodle.org/mod/forum/discuss.php?d=168740#p766148) and I can't find the methods to get all courses, selected courses or courses categories as easily as you are doing with "mycourses", and I find it kind of frustrating !
In reply to Trem_r Trem_r

Re: Custom Menu Items

by Sam Hemelryk -
Hmm indeed Moodle is anything but simple it is a huge application and the sheer number of API's as well as the functions, classes, and methods within them is phenomenal.

Adding categories and courses to the custom menu seems to be a hot topic as of late so I wrote up a VERY quick tutorial of one way of achieving this:

Development:Themes 2.0 adding courses and categories to the custom menu

Perhaps that will help you along with it.

In regards to the courses and category related function check out moodle/course/lib.php.

Cheers
Sam
In reply to Richard Oelmann

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Richard,

Sorry for not getting back to you earlier. I started to answer then got distracted forgot later in the day.

I was looking at some of the themes in Moodle 1.9 and the YUI menu we started off with. That was, and is still the nicest looking menu I have used in Moodle.

I have come to loath this present YUI menu widgit. The custom menu is a great idea, but a real pain to style.

I was trying to recall how I created the one I mentioned earlier in this thread. What I did was add id="menu1" somewhere in the url of the custom menu section in the theme settings' page.

eg.,

Moodle|http://moodle.org" id="menu1

so what you do is interrupt the flow of the HTML tag with a "

the opening " is added automatically in the system php file wherever it's generated, so all you are doing is closing if after the URL and then opening a new set, to add your class selector in normal way, but leaving off the ending " ..for example...

<a href="http://moodle.org" id="menu1" >Moodle</a>

You style is as follows...

ul li a:link#menu1 { color: red; text-decoration: none;}

ul li a:hover#menu1 { color: blue; text-decoration: underline overline;}

It's a while since I tried this idea, but it did work, and very well at that.

HTH

Mary

In reply to Mary Evans

Re: Custom Menu Items

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Mary,

Many thanks for this, I'll give it a try.

I agree though, the custom menu is great but currently a major pain to style, while the yui menu that we used in 1.9 is a pain for some user/admins to code (although easier than overriding the renderer for the 2.0 menu), but is very flexible and easier to style.

I guess it's a step in the right direction though and future developments may make it easier to style and customise.

Rich

In reply to Richard Oelmann

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Further to this discussion Richard, I discovered that, like the My Courses link using the renderers.php file to add the actual code for the extra custom menu tab, you can use this method to add a login at the beginning of the custom menu. I'll email it to you later...if I remember.

I'm still working on the Flexilite theme, but got carried away styling a three column blog, which works wonderfully well.

Oh the joys of Moodle! LOL

Take care

M

In reply to Mary Evans

Re: Custom Menu Items

by Cindy Zhao -

Mary,

Thank you SO much for the detailed explaination. I really appreciate your color-coded codes smile

I carefully followed your example in the Theme setting, but it hasn't work for me yet. This is what I typed:

About Us|http://www.smart-farms.net" class="menu1

Then, Where should the style go? I added your lines in formal-white.css which is the theme I use, didn't work; then I inserte under Themes>Formal White>Custom CSS box.  Still didn't work.

Firebug windows shows the html codes as this:

<a href="http://129.2.76.28/moodle/mod/page/view.php?id=13%22%20class%3D%22menu1" title="Our Project" class="yui3-menu-label" id="yui_3_2_0_1_1302800847479112" role="menuitem" aria-haspopup="true" tabindex="-1">Our Project</a>

My site is at http://129.2.76.28/moodle/  I hope to increase the font size & bar height of the costum menu.

Thanks again,

Cindy

In reply to Cindy Zhao

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Cindy,

I made a slight error which I have now edited in my last post here.

It should have read ID and NOT CLASS.

However this is only an experiment for simple menu structures which Richard and I are currently working on, which can override the custom menu css. But your site looks great as it is, and besides the custom menu can only deal with a limited number of top level title tabs, so increasing the font size on your present site taking into account the number of top-level titles you have, stands to break the menu.

HTH

Mary

 

In reply to Mary Evans

Re: Custom Menu Items

by Cindy Zhao -

Hi Mary,

Thank you so much for your insights. I really appreciate you took the time to look at my site.  I don't have to change the font size. The professor I work for asks me to increase the bar height, so the menu bar looks a little bit bigger.  Is there an easy way to do that?

Right now, the background color of my custom menu bar is the same as the site background.  How can I simply change background color or image of custom menu?

Thank you so much again.

Cindy

In reply to Cindy Zhao

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Cindy,

You could try the following...

#custommenu {
margin-right: 17px;
margin-left: 17px;
font-size: 2em;
line-height: 2em;
background:#006;
color:#FF6;
}

Adding line-height should give you more height. If 2em is not enough you can increase the value.

Remember too that you need to have Theme Designer Mode enabled so that you see the changes. Site Administration > Appearance > Themes > Theme settings

HTH

Mary

In reply to Mary Evans

Re: Custom Menu Items

by Pieter van der Hijden -

Dear Mary,

In Moodle 2.0.2 with formal white theme, I would like to change the menu background color (now white on white) and to add a border to the menu (now no visible background).

Is this possible without touching the CSS files? E.g. by adding some extra's to the menu specification (as you showed earlier), or by adding some lines to the custom-css entry field of the theme.

Best Regards,
Pieter van der Hijden

In reply to Pieter van der Hijden

Re: Custom Menu Items

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Pieter,

Having been away for a few weeks I haven't had chance to carry on experimenting with Mary's suggestion using and ID in the custom menu, but that would certainly be one way of identifying sections to add css in the custom css section of many themes. I hope to return to this later this week if I get chance.

The other alternative is to use a tool such as firebug in firefox to identify the exact css which needs to be ammended in the custom css field to achieve the look you want.

Also (with a bit of shameless self-plugging) if you do not have access to the server files to ammend the css itself, you could try looking at my flexi_ii theme which can be configured from its own settings page wink

 

Richard

In reply to Pieter van der Hijden

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

You can certainly add custom css in the Admin settings page for the Formal White theme in Moodle 2. To find out which CSS selector you need to style use Firebug.

HTH

Mary

In reply to Mary Evans

Re: Custom Menu Items

by Pieter van der Hijden -

I would greatly appreciate if you could give an example of how to use the "custom css" field on the settings page of a specific theme.

I'm using formal-white and trying to invent how to change the custom menu background color and/or its border.

I browsed many documents on Moodle.org and on Yagoo (yui3), I installed Firebug which shows me pages full of CSS for the menu only, I experimented with various settings (both with cache clearing and with dev = on). However, my knowledge of CSS is limited and trial-and-error does not help.

Best Regards,
Pieter van der Hijden

In reply to Pieter van der Hijden

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

i Pieter,

If you use Firebug then you will know how to change css directly ointo your browser to see if the code works or not.  Here is a screenshot of the changes I made to #custommenu which is the div container around the custom menu.

Of course this is only a small part of the css for the custom menu. It's not easy, but to see the changes you are making you need Theme Designer Mode enabled in Site Administration > Appearance > Themes > Theme settings

This stops the the theme files being cached, and enables you t see the changes you make to the css or theme layout files.

The CSS for formal white is pretty basic compared with other themes.

If you deleted the 2 lines I have crossed out (which relate to a background image) - you will find this in formal_white/style/menu.css,  you will find that you can change the color of the background to whatever you like, just by adding the color code for your prefered color.

#custommenu .yui3-menu {
background-image:url([[pix:theme|custommenubg]]);
background-repeat:repeat-x;
background-color:#e1e1df;
}

CSS FAQ is a good place to get help with your Moodle theme customisation. Also Themes 2.0 FAQ is a good starting place, you will find a link to Moodle 2. 0 Theme development page where you will find lots of Tutorials which may help also.

Mary

In reply to Pieter van der Hijden

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Pieter,

Sorry I totally misread your question!

To add changes in the CSS using Custon CSS settings you can add the following which will do what I suggested in my previous comment above.

#custommenu {
background-color: #d7fa4c;
}

#custommenu .yui3-menu {
background-image: none;
background-repeat: none;
background-color: transparent;
}

.yui3-skin-sam .yui3-menu-horizontal .yui3-menu-label,
.yui3-skin-sam .yui3-menu-horizontal .yui3-menuitem-content {
line-height: 3em;
}

Which should give you a start.

Mary

In reply to Mary Evans

Re: Custom Menu Items

by Pieter van der Hijden -

Hi Mary,

Thanks again!

Indeed, your code example works when I work with Arialist and have entered it in the "Custom CSS" field of Arialist. However, when I work with Formal White and enter it there, nothing is happening (apparently).

Could it be that the Custom CSS field of Formal White does not work at all?

Best Regards,
Pieter van der Hijden

In reply to Pieter van der Hijden

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Pieter,

You are right, there is a BUG in the settings' page of Formal White.

I've been trying to trace the problem but can't pinpoint where it's gone wrong.

I need to report this as an Moodle Tracker issue if it has not been reported already. Someone must have noticed it?

You could try turning Debuggin on... Site Administration > Development > Debugging (set to Developer Mode and have messages directed to page unless you are a live site otherwise set to go to Server Log).

Mary

In reply to Mary Evans

Re: Custom Menu Items

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Mary,

Issue appears to be that there is not (at least I couldn't find mixed) a function in the lib file in formal white for the customcss setting. eg the one in the flexilite lib file:

function flexilite_set_customcss($css, $customcss) {
$tag = 'setting:customcss';
$replacement = $customcss;
if (is_null($replacement)) {
$replacement = '';
}
$css = str_replace($tag, $replacement, $css);
return $css;
}

This would mean that although there is a setting on the admin page and the csutomcss setting line in the css file, there is not the process in between to take the one and put it into the other.

I thought I remembered this being commented on before, and being relevant to a couple of other themes as well, but I have not been able to find the thread in the forum so I could be mistaken.

HTH

Richard

In reply to Richard Oelmann

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

You rock! Great find Richard...

I've opened MDL-27474 to track this. Could you add this same comment to that tracker?  That would be very helpful. This means I can get this fixed this week hopefully.

Cheers

In reply to Pieter van der Hijden

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Pieter,

I have opened a new Moodle Tracker issue MDl-27474 to track this BUG.

Thanks for bringing this up in the forum.

Cheers

Mary

In reply to Robert Tamblyn

Re: Custom Menu Items

by Miriam Laidlaw -
Picture of Plugin developers

End of a long day and my brain isn't working well. I'm sure the solution is obvious.

I am trying to customise my custom menu, and it seems that no matter what I do the items in the menu that have a sub-menu won't conform to the background colour of the rest of the menu.

Pictorial demonstration:

White background?

Between "Moodle free support" and "Moodle News" there is an item (in white text). The dark grey background is not applying to this item.

However, the hover-over (active) highlight does work:

Hovered

I have changed quite a few things here and there and they either have no effect, or they change the WHOLE CUSTOM MENU to that colour. As I have a gradient background in the main bar of the custom menu, some things I tried overrode that with the plain colour.

Not even sure if I'm making sense at this point. Hope someone understands what I'm trying to do.

(Thing is, I'm SURE I've done this successfully elsewhere. I must have, I have made dark custom menus before... dead.)

Edit: So many edits because of typos.

In reply to Miriam Laidlaw

Re: Custom Menu Items

by Larry Elchuck -

I was able to add two elements (My Units and Logout) to the end of the custom menu in the Afterburner theme, as it had a renderers.php file that could be modified.  The formal white theme has no such file.  How might these elements be added to Formal White?

 

I don't think adding them to the custom CSS for the Formal White  theme will will do the trick, but I might be wrong.

 

Suggestions?

In reply to Larry Elchuck

Re: Custom Menu Items

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

By adding a renderer.php file to the Formal White theme -> http://docs.moodle.org/dev/Overriding_a_renderer.

In reply to Gareth J Barnard

Re: Custom Menu Items

by Larry Elchuck -

Hi Gareth

 

As there was  not a renderers.php file in formal_white, I copied the one from afterburner that I modded by renaming the class to "class theme_formal_white_core_renderer extends core_renderer {"  but it did not do anything.  

I also added $THEME->rendererfactory = 'theme_formal_white_renderer_factory'; to the config.php file but then it only hung up

 

I must be missing something (and not surprising), as I'm a bit of a code hacker, and not a real student of php.

 

cheers

L

 

In reply to Larry Elchuck

Re: Custom Menu Items

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Larry,

With respect it looks like you did not follow the instructions (point 3 of http://docs.moodle.org/dev/Overriding_a_renderer#Getting_started).  Nowhere does it say to rename:

$THEME->rendererfactory = 'theme_overridden_renderer_factory';

to:

$THEME->rendererfactory = 'theme_formal_white_renderer_factory';

It is the core class 'theme_overridden_renderer_factory' that finds the 'renderer.php' file and applies your renderer.  So, change it to:

$THEME->rendererfactory = 'theme_overridden_renderer_factory';

And you should find that it works.

Gareth

In reply to Larry Elchuck

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi,

If you added Afterburner's renderers.php to Formal White you will also need to copy over the CSS for the menu too. As this menu is different than the norm and so uses a different set of CSS rules.

Cheers

Mary

In reply to Mary Evans

Re: Custom Menu Items

by Larry Elchuck -

I'm only interested in adding "My Courses" and "Log Out" (the latter only showing when a user is logged in) to the Custom Menu bar in Formal White.  Whatever is simplest (not being a CSS guy!)

This was the added code in afterburner .. but it had a renderers.php file.

        // Add a login or logout link
        if (isloggedin()) {
            $branchlabel = get_string('logout');
            $branchurl   = new moodle_url('/login/logout.php');
        } else {
            $branchlabel = get_string('login');
            $branchurl   = new moodle_url('/login/index.php');
        }
            $branchsort  = 10500;
 //       $branch = $menu->add($branchlabel, $branchurl, $branchlabel, -1);
            $branch = $menu->add($branchlabel, $branchurl, $branchtitle, $branchsort);
        
// Add the mycourses link
$mycourses = $this->page->navigation->get('mycourses');

if (isloggedin() && $mycourses && $mycourses->has_children()) {
    $branchlabel = get_string('mycourses');
    $branchurl   = new moodle_url('/my/index.php');
    $branchtitle = $branchlabel;
    $branchsort  = 10000;
 
    $branch = $menu->add($branchlabel, $branchurl, $branchtitle, $branchsort);

    foreach ($mycourses->children as $coursenode) {
                $branch->add($coursenode->get_content(), $coursenode->action, $coursenode->get_title());
    }
 }

In reply to Larry Elchuck

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

In that case where did you actually add that section of PHP code?

In reply to Mary Evans

Re: Custom Menu Items

by Larry Elchuck -

Hi Mary

Here was the entire renderers.php file from afterburner with the code inserted.

thanks

larry

---------------

 

<?php

class theme_afterburner_core_renderer extends core_renderer {

    /**
     * Renders a custom menu object (located in outputcomponents.php)
     *
     * The custom menu this method override the render_custom_menu function
     * in outputrenderers.php
     * @staticvar int $menucount
     * @param custom_menu $menu
     * @return string
     */
    protected function render_custom_menu(custom_menu $menu) {

        // If the menu has no children return an empty string
        if (!$menu->has_children()) {
            return '';
        }

        // Add a login or logout link
        if (isloggedin()) {
            $branchlabel = get_string('logout');
            $branchurl   = new moodle_url('/login/logout.php');
        } else {
            $branchlabel = get_string('login');
            $branchurl   = new moodle_url('/login/index.php');
        }
            $branchsort  = 10500;
 //       $branch = $menu->add($branchlabel, $branchurl, $branchlabel, -1);
            $branch = $menu->add($branchlabel, $branchurl, $branchtitle, $branchsort);
        
// Add the mycourses link
$mycourses = $this->page->navigation->get('mycourses');

if (isloggedin() && $mycourses && $mycourses->has_children()) {
    $branchlabel = get_string('mycourses');
    $branchurl   = new moodle_url('/my/index.php');
    $branchtitle = $branchlabel;
    $branchsort  = 10000;
 
    $branch = $menu->add($branchlabel, $branchurl, $branchtitle, $branchsort);

    foreach ($mycourses->children as $coursenode) {
                $branch->add($coursenode->get_content(), $coursenode->action, $coursenode->get_title());
    }
 }

        // Initialise this custom menu
        $content = html_writer::start_tag('ul', array('class'=>'dropdown dropdown-horizontal'));
        // Render each child
        foreach ($menu->get_children() as $item) {
            $content .= $this->render_custom_menu_item($item);
        }
        // Close the open tags
        $content .= html_writer::end_tag('ul');
        // Return the custom menu
        return $content;
    }

    /**
     * Renders a custom menu node as part of a submenu
     *
     * The custom menu this method override the render_custom_menu_item function
     * in outputrenderers.php
     *
     * @see render_custom_menu()
     *
     * @staticvar int $submenucount
     * @param custom_menu_item $menunode
     * @return string
     */
    protected function render_custom_menu_item(custom_menu_item $menunode) {
        // Required to ensure we get unique trackable id's
        static $submenucount = 0;
        $content = html_writer::start_tag('li');
        if ($menunode->has_children()) {
            // If the child has menus render it as a sub menu
            $submenucount++;
            if ($menunode->get_url() !== null) {
                $url = $menunode->get_url();
            } else {
                $url = '#cm_submenu_'.$submenucount;
            }
            $content .= html_writer::start_tag('span', array('class'=>'customitem'));
            $content .= html_writer::link($url, $menunode->get_text(), array('title'=>$menunode->get_title()));
            $content .= html_writer::end_tag('span');
            $content .= html_writer::start_tag('ul');
            foreach ($menunode->get_children() as $menunode) {
                $content .= $this->render_custom_menu_item($menunode);
            }
            $content .= html_writer::end_tag('ul');
        } else {
            // The node doesn't have children so produce a final menuitem

            if ($menunode->get_url() !== null) {
                $url = $menunode->get_url();
            } else {
                $url = '#';
            }
            $content .= html_writer::link($url, $menunode->get_text(), array('title'=>$menunode->get_title()));
        }
        $content .= html_writer::end_tag('li');
        // Return the sub menu
        return $content;
    }

    /**
     * Copied from core_renderer with one minor change - changed $this->output->render() call to $this->render()
     *
     * @param navigation_node $item
     * @return string
     */
    protected function render_navigation_node(navigation_node $item) {
        $content = $item->get_content();
        $title = $item->get_title();
        if ($item->icon instanceof renderable && !$item->hideicon) {
            $icon = $this->render($item->icon);
            $content = $icon.$content; // use CSS for spacing of icons
        }
        if ($item->helpbutton !== null) {
            $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton'));
        }
        if ($content === '') {
            return '';
        }
        if ($item->action instanceof action_link) {
            //adds class dimmed to hidden courses and categories
            $link = $item->action;
            if ($item->hidden) {
                $link->add_class('dimmed');
            }
            $content = $this->render($link);
        } else if ($item->action instanceof moodle_url) {
            $attributes = array();
            if ($title !== '') {
                $attributes['title'] = $title;
            }
            if ($item->hidden) {
                $attributes['class'] = 'dimmed_text';
            }
            $content = html_writer::link($item->action, $content, $attributes);

        } else if (is_string($item->action) || empty($item->action)) {
            $attributes = array();
            if ($title !== '') {
                $attributes['title'] = $title;
            }
            if ($item->hidden) {
                $attributes['class'] = 'dimmed_text';
            }
            $content = html_writer::tag('span', $content, $attributes);
        }
        return $content;
    }

}

In reply to Larry Elchuck

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Larry,

I know what the Afterburner renderers.php looks like as I built the Afterburner theme, I am just curious to see what you are doing with it in your theme. I presume you have changed the name from afterburner to whatever your theme is called. And also since you appear to be using the whole of the custom menu from Afterburner then you will also need the CSS, but is it is only for the login/logout then there is an easier way. But if it is working OK...carry one!

Cheers

Mary

In reply to Mary Evans

Re: Custom Menu Items

by Larry Elchuck -

Mary

Thanks for your help.

It is only for the Log in/Out and My Courses ... so if you have an easier way, please share.  ;)

L

In reply to Larry Elchuck

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

HI,

If you are only going to need the login/logount and the other links like My courses page and login/logout and not bother with the Custommenu proper...then you could add the following to the layout files instead of adding the renderer. This adds a link to the profile page too.

I'm not saying it is a better way...just an alternative.

<div id="custommenu">

<?php if (isloggedin()) {
    echo html_writer::start_tag(ul);

    echo html_writer::start_tag(li),
    echo html_writer::link(new moodle_url('/login/logout.php', array('sesskey'=>sesskey())), get_string('logout'));
    echo html_writer::end_tag('li');
    echo html_writer::start_tag('li', array());
    echo html_writer::link(new moodle_url('/user/profile.php', array('id'=>$USER->id)), get_string('myprofile'));
    echo html_writer::end_tag('li');
    echo html_writer::start_tag('li');
    echo html_writer::link(new moodle_url('/my', array('id'=>$USER->id)), get_string('mycourses'));
    echo html_writer::end_tag('li');
    echo html_writer::end_tag(ul),
} else {
    echo html_writer::start_tag(ul),
    echo html_writer::start_tag(li),
    echo html_writer::link(new moodle_url('/login/'), get_string('login'));
    echo html_writer::end_tag(li),
    echo html_writer::end_tag(ul),
}

</div>

Cheers

Mary

In reply to Mary Evans

Re: Custom Menu Items

by Larry Elchuck -

Should it be added to the 4 files inside of the layout folder, Mary?

(embedded, frontpage, general, report)

tnx.

L

 

In reply to Larry Elchuck

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Frontpage, General & Report but NOT Embedded.

In reply to Mary Evans

Re: Custom Menu Items

by Larry Elchuck -

Hi Mary

I tried inserting the code in the most logical place to me, but no luck on adding the items to the custom menu.

On this attempt, I only added it to general.php, as I am initially testing it within one course.

 

Do you think I added it properly?

 

thanks again,

larry

Attachment custommenu.jpg
In reply to Larry Elchuck

Re: Custom Menu Items

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

It's in the wrong place...and looking at it it isn't such a good idea so perhaps your first approach is right for you...so stick with the renderer.

Cheers

Mary