Is it possible for a local plugin to add an element in the admin block?

Is it possible for a local plugin to add an element in the admin block?

by Julen Pardo -
Number of replies: 5

Hi all,

I want to add a link in the course administration block, as a new item, inside the "Course administration" folder:

Course administration

(Sorry for that arrow).

I could add it to the navigation block (which is not bad, but if it is possible to add to the admin block, better smile ), following the Navigation API, with the following callback:

function local_localplugin_extend_navigation($navigation) {
    $pluginname = 'I\'m in navigation!';
    $url = new moodle_url('/local/localplugin/index.php');
    $navigation->add($pluginname, $url, navigation_node::TYPE_SETTING);
}

But I'm not sure of how to proceed with the administration block. I've tried also with $ADMIN->add function, but it adds the element in the site administration, and only for existing folders.


Thanks in advance and kind regards,

Julen

Average of ratings: -
In reply to Julen Pardo

Re: Is it possible for a local plugin to add an element in the admin block?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

It's a pretty simple variation of the function you've already found - local_PLUGINNAME_extend_settings_navigation($navigation, $context)

Average of ratings: Useful (2)
In reply to Davo Smith

Re: Is it possible for a local plugin to add an element in the admin block?

by Julen Pardo -

Hi Davo,

Many thanks for your quick response.

I changed the function name, adding _settings_ in between, and also the $context parameter, but it doesn't work.

Do I have to do something else?


Many thanks,

Julen

In reply to Julen Pardo

Re: Is it possible for a local plugin to add an element in the admin block?

by Juho Jaakkola -

More hands-on instructions:

  1. Open up your favorite IDE, and search for the string "Course admiminstration" from the translations file (lang/en/moodle.php)
  2. Check the translation key of the string (It is "courseadministration")
  3. Now search that translation key from Moodle's source code
  4. One of the search results is this:
  • $coursenode = $this->add(get_string('courseadministration'), null, self::TYPE_COURSE, null, 'courseadmin');
Now you know that the name of the parent node is "courseadmin" and it's type is "TYPE_COURSE"

Add the function local_PLUGINNAME_extends_settings_navigation() into the lib.php file of your local plugin
Find the correct parent node from the $navigation parameter with this code:

  • $parent = $navigation->find('courseadmin', navigation_node::TYPE_COURSE);
Add your own menu item to it:
  • $parent->add(get_string('something', 'local_PLUGINNAME'), '/local/PLUGINNAME/file.php', navigation_node::TYPE_SETTING);


Final result:

/**
 * Add the menu node "something" under the course settings menu
 */
function local_PLUGINNAME_extend_settings_navigation($navigation) {
    $parent = $navigation->find('courseadmin', navigation_node::TYPE_COURSE);
    $parent->add(get_string('something', 'local_something'), '/local/PLUGINNAME/foo.php', navigation_node::TYPE_SETTING);
}

(I havent't tested the code, so it may have some mistakes.)

Average of ratings: Useful (2)
In reply to Juho Jaakkola

Re: Is it possible for a local plugin to add an element in the admin block?

by Juho Jaakkola -

Great, it seems the editor messed up the numbers of the steps 7-8. I hope you can still make sense of it.

In reply to Juho Jaakkola

Re: Is it possible for a local plugin to add an element in the admin block?

by Julen Pardo -

Hi Juho,

Now works like a charm, many thanks! How to add the element to the parent element was what I didn't found.


Thanks to both you,

Julen