Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Customising the theme user menu: Difference between revisions

From MoodleDocs
m (Added link to spanish translation of page)
m (Text replacement - "<code php>" to "<syntaxhighlight lang="php">")
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:Themes}}{{Moodle 3.0}}
{{Template:Themes}}{{Moodle 3.0}}


As of 3.0, the [https://docs.moodle.org/29/en/Theme_settings#User_menu_items user menu] can be customised via an array of options, when it is called via <code>user_get_user_navigation_info()</code>.
As of 3.0, the [https://docs.moodle.org/29/en/Theme_settings#User_menu_items user menu] can be customised via an array of options, when it is called via <syntaxhighlight lang="php">user_get_user_navigation_info()</syntaxhighlight>.


Initially, only the size of the user's avatar picture can be changed (from the default of 35px square), using the <code>avatarsize</code> key with a new size.
Initially, only the size of the user's avatar picture can be changed (from the default of 35px square), using the <syntaxhighlight lang="php">avatarsize</syntaxhighlight> key with a new size.


<code php>
<syntaxhighlight lang="php">
user_get_user_navigation_info($user, $this->page, array('avatarsize' => 85));
user_get_user_navigation_info($user, $this->page, array('avatarsize' => 85));
</code>
</syntaxhighlight>


Unfortunately, to  change the one line that calls <code>user_get_user_navigation_info()</code>, the whole of <code>core_renderer::user_menu()</code> must be overridden:
Unfortunately, to  change the one line that calls <syntaxhighlight lang="php">user_get_user_navigation_info()</syntaxhighlight>, the whole of <syntaxhighlight lang="php">core_renderer::user_menu()</syntaxhighlight> must be overridden:


In '''/themes/<yourtheme>/renderers.lib'''...
In '''/themes/<yourtheme>/renderers.lib'''...
<code php>
<syntaxhighlight lang="php">
public function user_menu($user = null, $withlinks = null, $loginlogo = null) {
public function user_menu($user = null, $withlinks = null, $loginlogo = null) {


Line 27: Line 27:


}
}
</code>
</syntaxhighlight>


NB: The image size in the browser may be enforced via CSS, with the pixel size of the image rounded up to one of a few hard-coded sizes - see MDL-50420.
NB: The image size in the browser may actually be set via CSS, as the pixel size of the image is rounded up to one of a few hard-coded sizes - see MDL-50420.


==See also==
==See also==

Latest revision as of 13:31, 14 July 2021

Moodle 3.0


As of 3.0, the user menu can be customised via an array of options, when it is called via

user_get_user_navigation_info()

. Initially, only the size of the user's avatar picture can be changed (from the default of 35px square), using the

avatarsize

key with a new size.

user_get_user_navigation_info($user, $this->page, array('avatarsize' => 85));

Unfortunately, to change the one line that calls

user_get_user_navigation_info()

, the whole of

core_renderer::user_menu()

must be overridden:

In /themes/<yourtheme>/renderers.lib...

public function user_menu($user = null, $withlinks = null, $loginlogo = null) {

    // Lots of code copied from lib/outputrenderers.php...

    // Get some navigation opts.
    $opts = user_get_user_navigation_info($user, $this->page, array('avatarsize' => 85));

    $avatarclasses = "avatars";
    $avatarcontents = html_writer::span($opts->metadata['useravatar'], 'avatar current');
    $usertextcontents = $opts->metadata['userfullname'];

    // Lots more code copied from lib/outputrenderers.php...

}

NB: The image size in the browser may actually be set via CSS, as the pixel size of the image is rounded up to one of a few hard-coded sizes - see MDL-50420.

See also