Advertisement:

Author Topic: Using hooks from a theme?  (Read 1206 times)

Offline Bloc

  • Full Member
  • ***
  • Posts: 415
  • Gender: Male
Using hooks from a theme?
« on: August 11, 2017, 03:19:05 AM »
I am working on a new theme for SMF 2.1 beta3/4 and contemplating whether I should make a specific mod to add hooks - or whether hooks can be added from inside the theme itself, in a correct fashion?

My concerns are 1) once a hook is added through the theme(meaning only the theme holds the function/file) , will other themes error out when trying to access said function? and 2) is there a way to check whther a hook has been added(through the theme) so it won't do the adding it each and every time the theme is used?

Using a separate mod solves both these things..but I wonder if it can be done safely just from the theme too.

Anyone has some insights?


@live627 posted an example some time ago:
Complete template override? Easy. I have some code I wrote several years ago to do this very thing:

Code: [Select]
function template_init()
{
add_integration_function('integrate_menu_buttons', 'fixSubTemplates', false);
}

function fixSubTemplates()
{
global $context;

$new_sub_template = empty($context['sub_template']) ? 'main_override' : $context['sub_template'] . '_override';

if (is_callable('template_' . $new_sub_template))
$context['sub_template'] = $new_sub_template;
}

The hook should be late enough in the runtime where you know the templates will have been already loaded, otherwise the overrides won't work.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 68,065
    • Arantor on GitHub
Re: Using hooks from a theme?
« Reply #1 on: August 11, 2017, 03:21:15 AM »
It kinda depends on what you're trying to do as to whether hooks will be called at the right time or not.
To assume is to hope that those who came before had the presence of mind and capacity to implement the dreams of those who would come after.

You either die a hero or live long enough to see yourself become the villain. It seems you have chosen which, and now I must do the same.

Offline Bloc

  • Full Member
  • ***
  • Posts: 415
  • Gender: Male
Re: Using hooks from a theme?
« Reply #2 on: August 11, 2017, 04:45:37 AM »
Intially I am looking to add to the posting screen: "integrate_post_end" and "integrate_post2_end" to do some swapping between input fields and the body of the text(unless I can find a way to do that just by javascript).

But the most important goal is to integrate into "integrate_pre_parsebbc" and "integrate_bbc_codes" to add a single BBC tag that will be invisible in normal posts, but can be targeted by the theme. My idea is to store theme specifc values about the post in this bbc tag, but without showing it on posts(hidden by css classes). The theme will use this to display extra info, and/or display things differently. The advantage is that once you switch to another theme the info isn't gone - but has this extra BBC tag around it so you know what is what.

Offline Bloc

  • Full Member
  • ***
  • Posts: 415
  • Gender: Male
Re: Using hooks from a theme?
« Reply #3 on: August 11, 2017, 05:34:35 AM »
Looking at the "call_integration_hook" function it seems I have a few options..one is to NOT make it permanent - that would tie in neatly with only running it from the theme and it also seems the hook isn't run if the function doesn't exist.  Should perhaps solve both my intial concerns then. :)