News:

Wondering if this will always be free?  See why free is better.

Main Menu

Question about Hook for bbc code..

Started by Ricky., August 03, 2011, 09:41:21 AM

Previous topic - Next topic

Ricky.

Hi..

I am trying to understand hooks http://wiki.simplemachines.org/smf/Integration_hooks#integrate_bbc_buttons

Suppose I want to make package for following bbcode (taken from Subs.php) and it has


         array(
'tag' => 'url',
'type' => 'unparsed_content',
'content' => '<a href="$1" class="bbc_link" target="_blank">$1</a>',
'validate' => create_function('&$tag, &$data, $disabled', '
$data = strtr($data, array(\'<br />\' => \'\'));
if (strpos($data, \'http://\') !== 0 && strpos($data, \'https://\') !== 0)
$data = \'http://\' . $data;
'),
),


Here are the two integration hooks we need :


>> integrate_bbc_buttons

    * Called from: Subs-Editor.php, during create_control_richedit(), just after the $context['bbc_tags'] array has been filled with the information regarding bbcode buttons.
    * Purpose: allows modify the appearance of the bbcode buttons in the rich editor (i.e. the editor with all the controls)
    * Accepts: 1 function name.
    * Sends: $context['bbc_tags']


>> integrate_bbc_codes

    * Called from: Subs.php, after all the information regarding the default bbcode tags are loaded in the $codes array
    * Purpose: allows easily add or remove or change the behaviour of bbcode tags.
    * Accepts: 1 function name.
    * Sends: $codes


So above code will be required to integrate with hook : integrate_bbc_codes
I am using following file to get call integration with hook:

// If we have found SSI.php and we are outside of SMF, then we are running standalone.
if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('SMF'))
require_once(dirname(__FILE__) . '/SSI.php');
elseif (!defined('SMF')) // If we are outside SMF and can't find SSI.php, then throw an error
die('<b>Error:</b> Cannot install - please verify you put this file in the same place as SMF\'s SSI.php.');
if (SMF == 'SSI')
db_extend('packages');

// Define the hooks
$hook_functions = array(
'integrate_bbc_buttons' => 'text_button_test',
'integrate_bbc_codes' => 'text_test_codes',
);

// Adding or removing them?
if (!empty($context['uninstalling']))
$call = 'remove_integration_function';
else
$call = 'add_integration_function';

// Do the deed
foreach ($hook_functions as $hook => $function)
$call($hook, $function);

if (SMF == 'SSI')
   echo 'Congratulations! You have successfully installed this mod!';


So I am required to create function text_test_codes Which should have code I have given in first place.. Can someone guide me what exactly should be code for function to integrate custom bbc code using hooks?

Suki

You can take a look at: http://custom.simplemachines.org/mods/index.php?mod=2833

it shows a basic example on how to use the bbc hooks.

Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Ricky.

Hmm.. yap, that is a good example. Issue resolved.

Thank you!

Akyhne


emanuele

function text_button_test (&$bbc_buttons)
{
global $txt;
$bbc_buttons[] = array(
'image' => 'newtag_img',
'code' => 'tag',
'before' => '[tag]',
'description' => $txt['tag_description']
);
}

function text_test_codes (&$codes)
{
$codes[] = array(
'tag' => 'mytag',
'before' => '<span class="bbc_mytag">',
'after' => '</span>',
);

}

That should work for a simple example.
Of course the code has many different options that are described in Subs.php starting from the line:
/* The following bbc are formatted as an array, with keys as follows:


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Akyhne

I know how to make BBC tags, just not how to use hooks.

Example from the Wiki:
Quoteintegrate_pre_include
Called from: Load.php, just after $modSettings has been loaded, which is very, very early on in SMF processing.
Purpose: Allows you to load a single file of PHP source of your own code. Can be used for other integration functions, as it will be loaded on every single page load.
Accepts: A string containing a path to a file to load. Certain identifiers can be used which include: $boarddir translates to the absolute path to your forum, such as /var/www/smf
$sourcedir points to the Sources directory which normally would reside inside SMF's home directory

Sends: No parameters, just looks for the file and loads it if found. If the file does not exist, the function silently skips the call and no error is produced.
Example: To include a file called MyFile.php in Sources, the syntax would be $sourcedir/MyFile.php.
So I guess the the SMF software looks in the prefix_settings table in the DB to find any file it needs to include? So how does it find out what to include? There are usually dousins of variables in that table.
I guess it would be something like:



variablevalue
integrate_pre_include$sourcedir/myfile.php
But I'm only guessing.

Now, if I want to add a custom BBC code, then where and how do I place that? Would I put my function into the DB as well or in a separate file?

emanuele

If that's the case, then you should start reading the page from the beginning, where it says "Using Integration hooks" and "Adding to hooks". ;)

Then you can have a look at the page Add a custom action using integration hooks, that explains (or better tries to explain) in details how to add a new hook, and make so it executes the functions.
The principle is the same for all the hooks. :)


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Suki

There are lot of other mods who took mine as an example, feel free to search for any of those, as common sense dictates, the newer the mod the better are the chances of that mod for using hooks.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Akyhne

Well, I can't release my mod anyway, without using the "old fashioned" SMF search and add mod feature. So I'll just not use hooks.

Arantor

I'm curious, what are you trying to do that you can't do with hooks?

Akyhne

In this mod, as an example, editing the jquery scripts to the editor in SMF 2.1.
And adding a single line of at $txt string, instead of having the forum to load a new file, just for that.

I can see the point of using hooks, if you just want to add a bunch of code in one particular spot, but besides that, hooks are very limited to me. I wouldn't be able to use hooks much in any of my mods.

Arantor

I don't see the problem with adding jQuery to SMF 2.1... JavaScript is by nature extensible and you can extend it in JS in the browser itself. Or if you're adding to the buttons, there's a hook for that.

As far as the language string goes, there's two ways you can deal with that. You can use the language files (which means translation is a snap and you won't ever run into encoding issues like a lot of mods actually do, even mine in the past, you have to keep UTF-8 and non UTF-8 separate), or you can have all the UTF-8 strings in one file and just select the one you want out of an array.

e.g.

$mystrings = array(
  'english' => 'String in English',
  'french' => 'La String',
);

Then select the one you need based on the current language or falling back to English.

To be honest, I suspect you're just saying that because not using hooks is easier. It does take a little imagination to really make good use of the hooks.

Akyhne

Quote from: Arantor on October 07, 2012, 12:46:41 PM
To be honest, I suspect you're just saying that because not using hooks is easier.
Spot on!

Arantor

Ah, so you're admitting that it's better to be a lazy mod author, than it is for your users to have a better, more reliable experience.

Consider that if you spend a bit longer now, you will likely spend less time in future on support.

Akyhne

It's not about being lazy. I just don't understand how to integrate hooks.

Ok, now that I found a mod that shows the integration of a BBC, it doesn't seem so hard. But still hooks, as I understand, limits to be used in the exact spots only where I find them in the default code of SMF.

And take for instance my Avatar Verification mod that integrates a lot with the Profile-Modify.php, Register.php & Register.template.php files. How would you do that with hooks?

ascaland

Have you ever used hooks before? Either I'm misunderstanding this topic or seriously confused.

Akyhne

I haven't. If I had, I wouldn't be asking.

Arantor

I didn't say the hooks were perfect. Where there is a hook, there is no reason not to use them.

There aren't hooks for the CAPTCHA type setup in SMF at the present time (AFAIK)... but it isn't that hard to add such hooks.

ascaland


Akyhne

Is it even possible to have several mods installed that calls integrate_bbc_buttons? Doing it manually in MySql, gives an error wit duplicate keys in both SMF 2 & 2.1.

Advertisement: