XF 2.1 BBcode with several options?

Myck

New member
Hello I am trying to create a bbcode with several options but this for me is very complicated, I have already reviewed all the posts in the forum and I managed to complete a code that does not work for me, please help me to finish it, it could help me by checking where I am failing.

When creating the bbcode I also used the PHP callback and created a file called BbcodeParser.php in the path MyAddon / Bbcode /, in short I followed all the steps to create a bb code with callback, but when I use the code bb in a thread does not work.

BBcode label

Code:
[mp3 = {text2}, {text3}] {text} [/ mp3]

It would look like this:

[mp3 = 1, Sample text] URL text [/ mp3]


PHP callback

Code:
<?php
  
namespace MyAddon\Bbcode;

class BbcodeParser {

    public static function mp3($tagChildren, $tagOption, $tag, array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
    {
        $res = $renderer->renderSubTree($tagChildren, $options);
        $toptions = trim(strip_tags($tag['option']));
        $toptions = preg_split("|,|", $toptions, -1, PREG_SPLIT_NO_EMPTY);
        // [mp3={text2},{text3}]{text}[/mp3]
        @list($text2, $text3) = $toptions;
        if ($tmp = trim($text2)) {
            $two = "$tmp";
        }
        if ($tmp = trim($text3)) {
            $three = "$tmp";
        }
        return "<mp3 {$two} {$three}>{$res}</mp3>";
    }

}
 

Myck

New member
Your implementation is correct, however, it won't work if you enter it as in your sample. Drop the spaces around the equal sign and also after the backslash in the closing tag. Like the following:

Sorry I think I pasted the sample wrong when I created the post but actually this is how I'm testing but I have the same problem.

I have tried the following samples but without any success:

Code:
[mp3=1, Text]https://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3[/mp3]

[mp3=1, Text]https://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3 [/mp3]

[mp3=1,Text] https://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3 [/mp3]

[mp3=1,Text]https://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3[/mp3]

When I create the BBcode it looks like this:

1.PNG


2.PNG

I already disabled the advanced option (Disable automatic link) but I still have the problem, this is what I get:

3.PNG
 

smozgur

Active member
Again, it is working exactly how it is supposed to be.
However, you are using PHP Callback, and HTML replacement has nothing to do with in that case.

I can't see how you'd like to use the returned values from the callback by looking at your HTML template, but following is basically what you need to do.

Forget HTML replacement box.
Create a new public template called bbcode_mp3 (or name however you like). I will use the following sample template by following your code:
HTML:
<ul>
    <li>
        This is parameter two: {$two}
    </li>
    <li>
        This is parameter three: {$three}
    </li>
    <li>
        This is parameter res: {$res}
    </li>
</ul>
See how I am using the parameters that your PHP callback generates. So you can replace with your own template HTML code accordingly.

Change your code to use this custom template as shown below instead of returning a text value:
PHP:
public static function mp3($tagChildren, $tagOption, $tag, array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
{
    $res = $renderer->renderSubTree($tagChildren, $options);
    $toptions = trim(strip_tags($tag['option']));
    $toptions = preg_split("|,|", $toptions, -1, PREG_SPLIT_NO_EMPTY);
    // [mp3={text2},{text3}]{text}[/mp3]
    @list($text2, $text3) = $toptions;
    if ($tmp = trim($text2)) {
        $two = "$tmp";
    }
    if ($tmp = trim($text3)) {
        $three = "$tmp";
    }
    
    /* Instead of returning a text */
    // return "<mp3 {$two} {$three}>{$res}</mp3>";
    
    /* We create the template and pass the parameters*/
    $templater = $renderer->getTemplater();
    $customTemplate = $templater->renderTemplate('public:bbcode_mp3', [
        'two' => $two,
        'three' => $three,
        'res' => $res,
    ], true);

    return $customTemplate;
}

It is exactly your existing code, except I removed return "<mp3 {$two} {$three}>{$res}</mp3>";, called the custom template I just created, injected the parameters generated in the callback function, and finally returned it as the rendered BB code HTML.

That's it. I hope it helps.
 
Top