XF 2.0 HTML instead of BBCode in Post

LPH

Well-known member
The following code creates a thread from an external site:

Code:
/** @var \XF\Service\Thread\Creator $creator */
        $creator = \XF::service( 'XF:Thread\Creator', $forum );

        $creator->setContent( $title, $message );
        $creator->setPrefix( $prefixId );
        $creator->setTags( $tags );
        $creator->save();

An HTML in the $message is being returned as HTML instead of converting to BBCode. Does anyone have any suggestions?
 
We only support BB code in posts. If you have HTML, it would need to be converted to BB code (or you'd need custom changes to support HTML, though generally not recommended for security reasons).
 
  • Like
Reactions: LPH
This was the old way to convert:

Code:
$body .= "\n" . XenForo_Helper_String::autoLinkBbCode( XenForo_Html_Renderer_BbCode::renderFromHtml( nl2br( $content ) ) );

Is there an equivalent in XenForo 2?

Looking in \XF\BbCode\Renderer\Html.php
 
\XF\Html\Renderer\BbCode is essentially identical to the XenForo_Html_Renderer_BbCode class from XF1.
 
  • Like
Reactions: LPH
@Mike,

This code appears to work but doesn't have the XenForo_Helper_String::autoLinkBbCode component from the similar method in XF1. Is it the autoLinkBbCode necessary?

Code:
$message = get_post_field( 'post_content', $post->ID );

$message = \XF\Html\Renderer\BbCode::renderFromHtml( nl2br( $message ) );

Also, I'm getting an Unnecessary fully qualified name using the above. Is there a better way to write the line?
 
Also, I'm getting an Unnecessary fully qualified name using the above. Is there a better way to write the line?

That message tells you that you could import the BbCode class instead. Your code would then look like:
Code:
<?php

namespace My\Custom\Namespace;

use \XF\Html\Renderer\BbCode;

class someClass extends someOtherClass {
    public function someFunction ($someArgument) {
        // Some other code...
        $message = get_post_field( 'post_content', $post->ID );
        $message = BbCode::renderFromHtml( nl2br( $message ) );
    }
}
 
Last edited:
Is it the autoLinkBbCode necessary?
It was never really necessary -- it just autolinked URLs. It likely doesn't make sense if you're being given HTML. It's generally more complex to do that now as it's part of the BB code processor system (which makes it significantly more flexible, though with a more boilerplate). You can see an example in the message preparer service if you want to see how we do it there.
 
  • Like
Reactions: LPH
You can see an example in the message preparer service if you want to see how we do it there.

Thank you. I really appreciate this type of guidance. It helps me go beyond my current challenge.

That message tells you that you could import the BbCode class instead.

Thank you. You are using import instead of use. What are the differences? A quick google search isn’t bringing up an answer. Do you have a link to a resource? The PHP manual pages on namespace stick with use.
 
Okay... the above code converts HTML to BbCode... how do we convert BbCode to HTML, in php?
 
Okay... the above code converts HTML to BbCode... how do we convert BbCode to HTML, in php?

This is how I do the conversion. Maybe there is a better method:

Code:
$reply = \XF::app()->finder('XF:Post')->where('post_id', $non_wp_postId )->fetchOne();
$comment_content = \XF::app()->templater()->fn( 'bb_code', [ $reply['message'], $reply, 'escaped'] );
 
Thank you. You are using import instead of use. What are the differences? A quick google search isn’t bringing up an answer. Do you have a link to a resource? The PHP manual pages on namespace stick with use.

It's use, sorry. That was python coming through, lol. use basically import and phpStorm will offer you to "import" the class if you let it auto-resolve your message.
 
  • Like
Reactions: LPH
It's use, sorry. That was python coming through, lol. use basically import and phpStorm will offer you to "import" the class if you let it auto-resolve your message.

Yes. Import through the use statement was a nicer way to do the code.
 
Top Bottom