local_xxx_extends_settings_navigation's $nav->find fails whenever Site Administration menu isn't loaded

local_xxx_extends_settings_navigation's $nav->find fails whenever Site Administration menu isn't loaded

by Michael Milette -
Number of replies: 1
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

I have the following in my local_mydevtool plugin's lib.php file which works fine in Moodle 2.5:


<?php
// Filename: /local/mydevtool/lib.php
function local_mydevtool_extends_settings_navigation($nav, $context) {
    global $PAGE;
    // Append My Dev Tool to the end of the Home > Administration > Site Administration > Development.
    $url = new moodle_url('/local/mydevtool/');
    $settingsnavnode = $nav->find('development', navigation_node::TYPE_SETTING);
    $settingsnavnode->add('My Dev Tool', $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/settings', ''));
}
?>


As of Moodle 2.6 and 2.7, I get the following error when I try to access any Moodle page that is not listed under the Site Administration menu (when the Site Administration dynamic menu is not loaded by default):

Fatal Error: Call to member function add() on a non-object in /local/mydevtool/lib.php on line 8. Line 8 is the line that starts with "$settingsnavnode->add(...". 

What it is obviously complaining about is that the line right above it isn't finding the Development node in the Site Administrator menu.

I've tried referring to the documentation at http://docs.moodle.org/dev/Navigation_API but there doesn't seem to be relevant information that would be helpful and I haven't found any similar examples implemented in Moodle's own source code.

Any suggestions on how to get it working again in Moodle 2.6 and Moodle 2.7 would be greatly appreciated.

Best regards,

Michael

Average of ratings: -
In reply to Michael Milette

Re: local_xxx_extends_settings_navigation's $nav->find fails whenever Site Administration menu isn't loaded

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
IIRC, the site administration tree is not loaded unless it is actually needed (for performance reasons). I would try to call the add() method only after a check the the 'development' node was actually found.