Moodle 4.4 developer update
This page highlights the important changes that are coming in Moodle 4.4 for developers.
Core changes
Dependency Injection
Support for PSR-11 compatible Containers has been introduced and can be accessed via the \core\di
class. Read the full documentation for information on how to use Moodle's DI infrastructure.
String formatting
Deprecation of format_* parameters
A number of legacy features and ways of calling format_text
, and format_string
now emit deprecation notices. These were all deprecated a long time ago but did not emit:
- Changes to
format_string
:- The
$options
array must not be a\context
or\core\context
object. This was never supported and behaviour was previously undetected - Unknown values passed to the
$options
parameter will now emit appropriate debugging notices
- The
- Changes to
format_text
:- The fourth parameter,
$courseiddonotuse
now emits a deprecation notice if provided. This was deprecated for Moodle 2.0 in MDL-22001 - The
smiley
option now emits a deprecation notice. This was deprecated for Moodle 2.0. - The
nocache
option now emits a deprecation notice. This was deprecated for Moodle 2.3 in MDL-34347 - The use of
FORMAT_WIKI
as a source format now throws an exception. Previously it added debugging information to the rendered content. This was deprecated in Moodle 1.5. - Unknown values passed to the
$options
parameter will now emit appropriate debugging notices
- The fourth parameter,
New \core\formatting class
A new \core\formatting
class has been introduced with new format_string()
, and format_text()
methods.
These methods no longer user the array $options
style configuration, instead expanding options into method parameters.
The existing \format_string()
, and \format_text()
methods remain in place, and are not currently deprecated. At this time you do not need to migrate to the APIs.
namespace core;
class formatting {
public function format_string(
?string $string,
bool $striplinks = true,
?context $context = null,
bool $filter = true,
bool $escape = true,
): string;
public function format_text(
?string $text,
string $format = FORMAT_MOODLE,
?context $context = null,
bool $trusted = false,
?bool $clean = null,
bool $filter = true,
bool $para = true,
bool $newlines = true,
bool $overflowdiv = false,
bool $blanktarget = false,
bool $allowid = false,
): string;
}
If choosing to use these new methods you must use Dependency Injection either via injection into the class, or via the \core\di
container. You must not instantiate the \core\formatting
class directly.
- Usage via Dependency Injection
- Usage via the \core\di container
namespace core;
class example {
public function __construct(
protected readonly \core\formatting $formatter,
) {
}
public function do_something(string $input): string {
return $this->formatter->format_string($input, ...);
}
}
$formatter = \core\di::get(\core\formatting::class);
echo $formatter->format_string($input, ...);
- Recommended usage
- Alternative approach
It is strongly recommended that you make use of named parameters for all but the first argument when calling them, rather than using positional arguments.
$formatter = \core\di::get(\core\formatting::class);
$formatter->format_string(
"The content to be formatted",
context: \core\context\course::instance($courseid),
filter: false,
);
$formatter->format_text(
"The content to be formatted",
context: \core\context\course::instance($courseid),
filter: false,
blanktarget: true,
);
The use of positional arguments to the format methods is strongly discouraged.
$formatter = \core\di::get(\core\formatting::class);
$formatter->format_string(
"The content to be formatted",
true,
\core\context\course::instance($courseid),
false,
);
$formatter->format_text(
"The content to be formatted",
FORMAT_MOODLE,
\core\context\course::instance($courseid),
false,
null,
false,
true,
true,
false,
true,
);
Enrolment
Support for multiple instances in csv course upload
It is now possible to upload a CSV file with multiple enrol instances of the same type in same course. This is useful for example when you want to enrol users in a course using two different cohorts.
To support this, a new method has been added to allow the UI to locate existing enrolment instances:
/**
* Finds matching instances for a given course.
*
* @param array $enrolmentdata enrolment data.
* @param int $courseid Course ID.
* @return stdClass|null Matching instance
*/
public function find_instance(
array $enrolmentdata,
int $courseid,
) : ?stdClass;
If your enrolment plugins supports multiple instances within the same course, you should implement this method.
Format of the CSV file
Bootstrap preparations for version 5
Some of the Bootstrap 4 classes will be deprecated or dropped in its version 5. To prepare for this, some of the current Bootstrap 4 classes usages have been replaced with version 5 compatible classes. This will help us to upgrade to Bootstrap 5 in the future.
Badges
- Badge colour class helpers
.badge-*
have been replaced with background utilities (use.bg-primary
instead of.badge-primary
) combined with the corresponding text colour classes (.text-dark
or.text-white
) to meet accessibility contrast. - The
.badge-pill
class has been replaced with.rounded-pill
<span class="badge badge-danger badge-pill">Error badge</span>
<span class="badge bg-danger text-white rounded-pill">Error badge</span>
Media
The .media
component has been replaced with utility classes.
<div class="media">
<div class="media-left">
[...]
</div>
<div class="media-body">
[...]
</div>
</div>
<div class="d-flex">
<div class="flex-shrink-0">
[...]
</div>
<div class="flex-grow-1 ml-3">
[...]
</div>
</div>