Note:

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

Output components: Difference between revisions

From MoodleDocs
m (Text replacement - "<code php>" to "<syntaxhighlight lang="php">")
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Work in progress}}
{{Work in progress}}


A list of renderable components to be used with [[Output renderers||output renderers]].
A list of renderable components to be used with [[Output renderers|output renderers]].


== User-related components ==
== User-related components ==
=== user_picture ===
=== user_picture ===
An avatar for a given '''user''' object, which can be rendered after being constructed directly:
An avatar for a given '''user''' object, which can be rendered after being constructed directly:
<code php>
<syntaxhighlight lang="php">
$userpic1 = new user_picture($USER);
$userpic1 = new user_picture($USER);
$OUTPUT->render($userpic1);
$OUTPUT->render($userpic1);
</code>
</syntaxhighlight>
Or by using a helper method in the core renderer (preferred):
Or by using a helper method in the core renderer (preferred):
<code php>
<syntaxhighlight lang="php">
$options = array( "visibletoscreenreaders" => false );
$options = array(  
    'visibletoscreenreaders' => false,
    'includefullname' => true,          // New in Moodle 3.4. Setting to true will render the user's full name beside it. Defaults to false.
);
$userpic2 = $OUTPUT->user_picture($USER, $options);
$userpic2 = $OUTPUT->user_picture($USER, $options);
</code>
</syntaxhighlight>
The user object should have the following fields:  
The user object should have the following fields:  
<code php>
<syntaxhighlight lang="php">
id
id
picture
picture
Line 22: Line 25:
firstname
firstname
lastname
lastname
</code>
</syntaxhighlight>
If the object does not have these fields, the db will be queried with significant performance implications. A list of these fields can be obtained from user_picture::fields() for the purposes of getting values ahead of time if necessary.
If the object does not have these fields, the db will be queried with significant performance implications. A list of these fields can be obtained from user_picture::fields() for the purposes of getting values ahead of time if necessary.


The following options can be set on the user_picture, either by changing each field after construction or by passing in an associative array as the second argument.
The following options can be set on the user_picture, either by changing each field after construction or by passing in an associative array as the second argument.
<code php>
<syntaxhighlight lang="php">
$userpic->courseid = $this->page->course->id; // course id of user profile in link
$userpic->courseid = $this->page->course->id; // course id of user profile in link
$userpic->size = 35; // size of image
$userpic->size = 35; // size of image
Line 34: Line 37:
$userpic->class = "userpicture"; // image class attribute
$userpic->class = "userpicture"; // image class attribute
$userpic->visibletoscreenreaders = true; // whether to be visible to screen readers
$userpic->visibletoscreenreaders = true; // whether to be visible to screen readers
</code>
</syntaxhighlight>

Latest revision as of 13:39, 14 July 2021

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.


A list of renderable components to be used with output renderers.

User-related components

user_picture

An avatar for a given user object, which can be rendered after being constructed directly:

$userpic1 = new user_picture($USER);
$OUTPUT->render($userpic1);

Or by using a helper method in the core renderer (preferred):

$options = array( 
    'visibletoscreenreaders' => false,
    'includefullname' => true,          // New in Moodle 3.4. Setting to true will render the user's full name beside it. Defaults to false.
);
$userpic2 = $OUTPUT->user_picture($USER, $options);

The user object should have the following fields:

id
picture
imagealt
firstname
lastname

If the object does not have these fields, the db will be queried with significant performance implications. A list of these fields can be obtained from user_picture::fields() for the purposes of getting values ahead of time if necessary.

The following options can be set on the user_picture, either by changing each field after construction or by passing in an associative array as the second argument.

$userpic->courseid = $this->page->course->id; // course id of user profile in link
$userpic->size = 35; // size of image
$userpic->link = true; // make image clickable - the link leads to user profile
$userpic->popup = false; // open in popup
$userpic->alttext = true; // add image alt attribute
$userpic->class = "userpicture"; // image class attribute
$userpic->visibletoscreenreaders = true; // whether to be visible to screen readers