Search code examples
phpmoodleadministration

Moodle site administration custom links


I want to add custom links to an site administration folder, top level, to be something like this:

Site Administration
- My custom link 1
- My custom link 2
.. and then goes standard moodle
- Notification
- and so on...

So far, I was able to add custom link to a Main Navigation block, and inside of Administration block, but in Site Administration folder no way.Right now I am getting menu items from settings with $settings = get_config('local_custom_links');

And it works perfectly if you want to add links in Block Administration.., however if there is anyone who had success with adding links to an Site Administration folder, please help.
Here is my code:

function local_custom_links_extend_settings_navigation(settings_navigation $nav, context $context) {

    $settings = get_config('local_custom_links');
    if (!empty($settings->menuitems_site_administration) && $settings->enable_site_administration) {
        $menu = new custom_menu($settings->menuitems_site_administration, current_language());
        if ($menu->has_children()) {
            foreach ($menu->get_children() as $item) {
                custom_links_settings_item($item, 0, null, $nav);
            }
        }
    }

}

function custom_links_settings_item(custom_menu_item $menunode, $parent, $pmasternode, settings_navigation $nav) {

    global $PAGE, $CFG;

    static $submenucount = 0;

    if ($menunode->has_children()) {
        $submenucount++;
        $url = $CFG->wwwroot;
        if ($menunode->get_url() !== null) {
            $url = new moodle_url($menunode->get_url());
        } else {
            $url = null;
        }
        if ($parent > 0) {
            $masternode = $pmasternode->add($menunode->get_text(), $url, navigation_node::TYPE_CONTAINER);
        } else {
            $masternode = $nav->add($menunode->get_text(), $url, navigation_node::TYPE_CONTAINER);
        }
        foreach ($menunode->get_children() as $menunode) {
            lambda_custom_links_custom_menu_item($menunode, $submenucount, $masternode, $nav);
        }
    }
}

Solution

  • Create a settings.php file in your /local/custom_links folder with this

    defined('MOODLE_INTERNAL') || die;
    
    if ($hassiteconfig) {
        $ADMIN->add(
                'root', // Root is the top menu.
                new admin_externalpage(
                    'local_custom_links', // Unique name.
                    get_string('pluginmenu', 'local_custom_links'), // Human name.
                    new moodle_url('/local/custom_links/index.php'), // Link.
                   'local/custom_links:manage' // Capability if any.
               )
        );
    }