XF 2.2 fn deprecated to fnBbCode

LPH

Well-known member
PhpStorm returns a message that the fn method is deprecated and so the following code needs to be updated:

PHP:
return XF::app()->templater()->fn( 'bb_code', [ $user['about'], $user, 'escaped' ] );

The templater.php file shows several different fnBbCode options.

Code:
        'bb_code'               => 'fnBbCode',
        'bb_code_snippet'       => 'fnBbCodeSnippet',
        'bb_code_type'          => 'fnBbCodeType',
        'bb_code_type_snippet'  => 'fnBbCodeTypeSnippet',

I've tried this code but it clearly isn't correct and states items are missing. The 'bb_code' is $bbCode.

Code:
return XF::app()->templater()->fnBbCode( 'bb_code', $user [ $user['about'] ]);

1641157154526.png

Can someone please explain to me the parameters? Maybe there is a file in XF that uses the fnBbCode and so I can use it as a reference.
 
Per the comment in \XF\Template\Templater::fn, just use func instead:

PHP:
return \XF::app()->templater()->func('bb_code', [$user['about'], $user, 'escaped']);
 
Code:
An exception occurred: [TypeError] Illegal offset type in src/XF/Container.php on line 237

XF\Container->create() in src/XF/SubContainer/BbCode.php at line 163
XF\SubContainer\BbCode->rules() in src/XF/SubContainer/BbCode.php at line 209
XF\SubContainer\BbCode->render() in src/XF/Template/Templater.php at line 2379
XF\Template\Templater->fnBbCode() in src/XF/Template/Templater.php at line 1128
XF\Template\Templater->func() in /home/nginx/domains/tuxreports.com/public/wp-content/plugins/xenword/src/Includes/Authors/Author.php at line 124

This is what happened when PHP was updated to 8.0.14.
 
OK. Here is from community/src/XF/EditHistory/Post.php starting at line 75

PHP:
    public function getHtmlFormattedContent($text, Entity $content = null)
    {
        return \XF::app()->templater()->func('bb_code', [$text, 'post', $content]);
    }

And here was what was tried
PHP:
return \XF::app()->templater()->func('bb_code', [$user['about'], $user, 'escaped']);

I'm not sure I get the meaning of what is in the square brackets. Is escaped and $user inverted?
 
Ah, I should have reviewed the arguments more closely. The second argument to func is an array of arguments that will be passed to the template function. You can look at fnBbCode to see what arguments it takes. The first two arguments are passed automatically, so you do not need to consider them.

PHP:
public function fnBbCode(
    $templater,
    &$escape,
    $bbCode,
    $context,
    $content,
    array $options = [],
    $type = 'html'
);

That leaves you with:
  1. $bbCode -- the string of BB code you want to parse
  2. $context -- the rendering context, typically the content type and maybe a sub-context separated by a colon
  3. $content -- the content entity or user entity of the string being rendered
  4. $options -- an array of renderer options (defaults to an empty array)
  5. $type -- the type of renderer to use (defaults to the HTML renderer)
Only the first three of these arguments are required. In your case, I would presume this corresponds to:

PHP:
\XF::app()->templater()->func('bb_code', [$user['about'], 'user:about', $user]);
 
Top Bottom