Plugin help text: how to introduce a parameter?

Plugin help text: how to introduce a parameter?

by Pat Sej -
Number of replies: 8

Hello!

When added to a course as an activity, a module I'm working on displays help when clicking on the question mark close to its title. It looks like it's handled by Moodle. It just requires to set a $string['modulename_help'].

I would like to modify the module's modulename_help to introduce a variable information {$a}.
Like that:

"This module is awesome. Check how it works <a href="{$a}">here</a>"

Is there a way to make it work? If yes, how can I pass a value ?

I hope my explanations are clear enough.

Thanks in advance!

Cheers,

Patrice

Average of ratings: -
In reply to Pat Sej

Re: Plugin help text: how to introduce a parameter?

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi Patrice,

https://docs.moodle.org/dev/String_API

Run-time parameters

Strings can define placeholders like {$a} or {$a->foobar}. These placeholders are replaced with a value passed to the get_string() function call.

// Strings defined in the language file.
$string['greeting'] = 'Dear {$a}';
$string['info'] = 'There are {$a->count} new messages from {$a->from}.';

// Passing values for the placeholders.
echo get_string('greeting', 'tool_example', 'Mr. Anderson');
echo get_string('info', 'tool_example', ['count' => 42, 'from' => 'Mr. Smith']);


is this what you looking for ?

Dominique.
In reply to Dominique Palumbo

Re: Plugin help text: how to introduce a parameter?

by Pat Sej -
Thanks Dominique, unfortunately, the modulename_help string is a bit special as I don't call myself the get_string.
It looks like it is called from course/lib.php (I've checked very quickly by looking for modulename_help in moodle's Github).
$defaultmodule->help = get_string('modulename_help', $modname);
Is there a way to override this behaviour by passing a parameter to the get_string in the plugin (changing Moodle's core code is not an option) ?
In reply to Pat Sej

Re: Plugin help text: how to introduce a parameter?

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

If you are not able to change the code that is calling get_string() to add the parameter you want to pass, then there is no way to make a parameter magically appear in the string when it is output.

The best you can do would be to hard-code the URL into the language string, e.g.

"Check how it works <a href="https://www.example.com/">here</a>"


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

Re: Plugin help text: how to introduce a parameter?

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

Additionally to what Davo says, for cases when you need to point the user to a location with more options, there is support for https://docs.moodle.org/dev/String_API#More_help_links

Average of ratings: Useful (1)
In reply to David Mudrák

Re: Plugin help text: how to introduce a parameter?

by Pat Sej -
Very interesting, thanks for the tip!
In reply to Davo Smith

Re: Plugin help text: how to introduce a parameter?

by Pat Sej -
Yes, there nothing is magical smile Thx
In reply to Pat Sej

Re: Plugin help text: how to introduce a parameter?

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers

It's a dirty trick,  I'm not proud of it, because it's dependant on Moodle version and theme.

But if it give you some ideas...

echo'<a class="btn btn-link p-0" role="button" data-container="body"
    data-toggle="popover" data-placement="right"
    data-content="'.get_string('namingscheme_help', 'mod_mymod').' "
    data-html="true" tabindex="0" data-trigger="focus"
    data-original-title="" title="">
    <i class="icon fa fa-question-circle text-info fa-fw " title="'.get_string('help').'" aria-label="'.get_string('help').'"></i>
</a>';

It'll be better if it use Moodle  HTML api.

Dominique.

In reply to Dominique Palumbo

Re: Plugin help text: how to introduce a parameter?

by Pat Sej -
Thx Dominique!