XF 2.1 BBcode with several options?

Myck

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>";
    }

}
 
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.webp


2.webp

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

3.webp
 
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.
 
Hey!!, thanks to your explanation I understood how it works and I have already created the BBcode I needed, but I still have a question.

Is it possible to create 2 $res? And if it is possible, how would it be done?

This currently works for me:

Code:
<?php

namespace MyAddon\Bbcode;

class BbcodeAudioPlayer {

    public static function sounds($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);
        // [sounds={text1A},{text1B},{text1C},{text1D}]{text1}[/sounds]
        @list($text1A, $text1B, $text1C, $text1D) = $toptions;
        if ($tmp = trim($text1A)) {
            $unoA = "$tmp";
        }
        if ($tmp = trim($text1B)) {
            $unoB = "$tmp";
        }
        if ($tmp = trim($text1C)) {
            $unoC = "$tmp";
        }
        if ($tmp = trim($text1D)) {
            $unoD = "$tmp";
        }
        return

But I want to add more than 2 $res, that is to say 2 URLs, I imagined something like this but logically it will not work:

Code:
<?php

namespace MyAddon\Bbcode;

class BbcodeAudioPlayer {

    public static function sounds($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);
        // [sounds={text1A},{text1B},{text1C},{text1D}]{text1},{text2}[/sounds]
        @list($text1A, $text1B, $text1C, $text1D) = $toptions;
        if ($tmp = trim($text1A)) {
            $unoA = "$tmp";
        }
        if ($tmp = trim($text1B)) {
            $unoB = "$tmp";
        }
        if ($tmp = trim($text1C)) {
            $unoC = "$tmp";
        }
        if ($tmp = trim($text1D)) {
            $unoD = "$tmp";
        }
        return
 
Back
Top Bottom