trim settings.php setting - custom moodle block

trim settings.php setting - custom moodle block

by Joseph Malmsten -
Number of replies: 3

Hi, 

My company has a moodle block where we have a string folderId setting that contains a unique key that maps to a folder in our product. The key is stored as "xxxxx-xxxxx-xxxxx-xxxxx".

I got a request to automatically trim any input into this field so that if a user inputs
 "   xxxxx-xxxxx-xxxxx-xxxxx-xxxxx   "
   or any beginning/ending whitespace combination will get saved as
"xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
and still work properly.

I was hoping there was a config_save like function that gets hit any time the global block settings are saved, but it looks like this was removed in Moodle 2.5. Is there any replacement function or alternate method I can user to override and trim this setting, I would prefer to do this in one place instead of finding all places this setting is used and sanitizing it there. 

Thanks,
Joe M. 

Average of ratings: -
In reply to Joseph Malmsten

Re: trim settings.php setting - custom moodle block

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You should be using admin settings, defined through a settings.php file in your block https://docs.moodle.org/dev/Admin_settings, the same as for any other plugin.

If you need custom behaviour, then you can make your own subclass of admin_setting_configtext with the extra logic.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: trim settings.php setting - custom moodle block

by Joseph Malmsten -

Thanks!

I was able to make a new class that extended the admin_setting_configtext_with_maxlength to trim the input before validating and writing.

Average of ratings: Useful (1)
In reply to Joseph Malmsten

Re: trim settings.php setting - custom moodle block

by Arshad Syed -
Hi joseph malmsten,


how you extend class of settings.php file , can you explain in brief. Please  i also have similar requirement.

Thanks