[2.2] Correct way to add new items in the menus

[2.2] Correct way to add new items in the menus

by tim st.clair -
Number of replies: 3
Picture of Plugin developers

Hi. I want my reports and plugins to be able to correctly register new menu items in arbitary spots.

I realise that for "site administration" I can follow this regular pattern:

$ADMIN->add('reports', new admin_externalpage('reportlog', get_string('log', 'admin'), "$CFG->wwwroot/report/log/index.php?id=".SITEID, 'report/log:view'));

However, I would like to add links to other locations, such as a direct child of My profile, or My Profile > Activity reports, or as a child of My home.

I have been nosing around in the code to see where these items are coded, but as mentioned I want my plugin to perform this, not modify core code (if at all possible).

Average of ratings: -
In reply to tim st.clair

Re: [2.2] Correct way to add new items in the menus

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Have a look in the lib.php files of other plugins. There are function you can define called things like ..._extend_navigation and ..._extend_settings_navigation. (These only exist for some plugin types.)

In reply to Tim Hunt

Re: [2.2] Correct way to add new items in the menus

by Hubert Chathi -

Not only do they only exist for certain plugin types, some (all?) of them will only be called on certain pages.

In reply to Tim Hunt

Re: [2.2] Correct way to add new items in the menus

by tim st.clair -
Picture of Plugin developers

ok, I had to move my plugin to /local/ because it's the only folder whose lib.php is checked for the plugin_extends_navigation() function which is called in the navigation block code (/lib/navigationlib.php). This in itself was an ordeal, however looks like it's ok for now. (reference here: http://moodle.org/mod/forum/discuss.php?d=170325)

Strange thing happens though. In my lib.php there's the extends function which makes a new moodle url as such:

function cpd_extends_navigation(global_navigation $navigation) {
    if (!isloggedin()) return;
    $container2 = $navigation->add(get_string('cpdactivities', 'local_cpd'));
    $userview2 = $container2->add(get_string('cpd', 'local_cpd'), new moodle_url('local/cpd/metadata.php'));
}

however the link that is generated when I look at the page is

http://localhost:8888/moodle22/admin/local/cpd/metadata.php

How has it slipped that "/admin/" into the mix? Should I not use moodle_url at all and just slip in a "$CFG->wwwroot/local/cpd/metadata.php" ? Everywhere I read reccomends moodle_url over any other method.