Reading setting variables of my mods with integration hooks

Started by davidhs, March 01, 2021, 08:04:10 AM

Previous topic - Next topic

davidhs

Some of my mods read setting variables and transform their values before store in the global variable $context. For example, I attach the file of two of my mods:
- https://custom.simplemachines.org/mods/index.php?mod=3818 uses Themes-CustomFieldFilterPost.php some time ago.
- https://custom.simplemachines.org/mods/index.php?mod=3703 does not use a file (writes code in Subs.php), but now I am updating and it will uses Themes-CustomBlocks.php.

I have some questions:

1. What global variable can I use for store these setting values?

I use $context (see attached files) but now It has emerged me a doubt.

Can be more correct to use $modSettings? I want to store settings variables of mods.

2. What integration hook can I use for load and transform these setting variables?

I want to use a hook that automatically calls my function to load my setting variables. In SMF 2.0.x series I only find two hooks:

  • integrate_pre_load: Called in Load.php, at end of the reloadSettings function. This function loads $modSettings and it can be the correct hook to load, but sometimes I need other variables ($board, $topic) in the transform process and there are not created at this point.
  • integrate_load_theme: Called in Load.php, at end of the loadTheme function. I use this hook because at this point I can use variables as $board or $topic. But I think this is not the correct use for this hook.

Is there another hook can I use? Should a new hook be created for this?




Before using hooks (and some of my mod do this, still), the setting variables was load and transformed in Subs.php, at beginning of template_header function, after call to setupThemeContext function. But I prefer use hooks.

Suki

Hooks are just points in time, their name  indicates where they are called but it doesn't mean they have to be used just for it.

$modSettings is used for settings values comming from the DB, if you want to transform them then it would be better to use $context['myModLogic'] where "myModLogic" is a unique key for you mod. Use the $modSettings value, apply your logic and store the result on your $context key
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

davidhs

Quote from: Suki on March 01, 2021, 10:28:46 AM
Hooks are just points in time, their name  indicates where they are called but it doesn't mean they have to be used just for it.

$modSettings is used for settings values comming from the DB, if you want to transform them then it would be better to use $context['myModLogic'] where "myModLogic" is a unique key for you mod. Use the $modSettings value, apply your logic and store the result on your $context key
Thanks. Then I do the things well.

Advertisement: