trim settings.php setting - custom moodle block

trim settings.php setting - custom moodle block

Joseph Malmsten -
回帖数: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. 

回复Joseph Malmsten

Re: trim settings.php setting - custom moodle block

Tim Hunt -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 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.

回复Tim Hunt

Re: trim settings.php setting - custom moodle block

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.

回复Joseph Malmsten

Re: trim settings.php setting - custom moodle block

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