XF 2.0 Templater fn BBCode

LPH

Well-known member
I'm not quite understanding what is being returned -- in terms of why the BBCode link isn't shown.

In the about for the user, there is a sentence with a link to a URL. This link is not showing on the external page when the following code is used to pull the $user['about']

PHP:
$author_id = get_the_author_meta( 'id', $user_id );

$user = \XF::app()->finder('XF:UserProfile')->where('user_id', $author_id)->fetchOne();

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

My understanding is that the fn is followed by name, arguments, escaped. Am I doing something wrong or simply expecting something that isn't possible?

Update: Oh Geez. Never mind. It's late. Yes. The BBCode is showing. :cool:
 
I'm getting an error on the following line:

PHP:
$comment_content = \XF::app()->templater()->fn( 'bb_code', [ $reply['message'], $reply, 'escaped'] );

An exception occurred: [ErrorException] [E_WARNING] Illegal offset type

After the bb_code name should be an array. I'm not certain but maybe something is wrong in the array.
 
A dump of the variable $reply shows the following:

Post {#2961 ▼
#noWPUpdate: false
-_uniqueEntityId: 68
#rootClass: "XF\Entity\Post"
#_useReplaceInto: false
#_newValues: []
#_values: array:18 [▼
"post_id" => 54
"thread_id" => 40
"user_id" => 1
"username" => "LPH"
"post_date" => 1531536106
"message" => "This is a xenforo reply."
"ip_id" => 310
"message_state" => "visible"
"attach_count" => 0
"position" => 1
"likes" => 0
"like_users" => "a:0:{}"
"warning_id" => 0
"warning_message" => ""
"last_edit_date" => 0
"last_edit_user_id" => 0
"edit_count" => 0
"embed_metadata" => "[]"
]
#_relations: array:1 [▼
"WPComment" => null
]
#_previousValues: []
#_options: []
#_deleted: false
#_readOnly: false
#_writePending: false
#_writeRunning: false
#_errors: []
#_cascadeSave: []
#_behaviors: null
}
 
From Templater.php, there are 5 parameters to render.

PHP:
return $this->app->bbCode()->render($bbCode, $type, $context, $content, $options);
 
Always use \XF::app()->bbCode()->render(...) directly. No need to launch the full templater.
  • bbCode: The BB Code string you want to render
  • type: The renderer you want to use, can pick any from XF/BbCode/Renderer/, but normally 'html' is what you want
  • context: The entity type string you're passing next, for example 'post', if you're passing an XF:Post. If you're not rendering an XF entity, leave it null.
  • content: The actual entity, or null.
  • options: Can be left away, until you actually have a reason to pass something in here.
If you just want to render a BB Code string, do \XF::app()->bbCode()->render('[B]My BB Code string[/B], 'html', null, null).
 
Thank you @Lukas W. . The illegal offset error remains:

PHP:
$comment_content = \XF::app()->bbCode()->render($reply['message'], 'html', null, null);

I think it is the $reply['message'] that is tossing the error. The dump shown above suggests it is okay but I'm not sure.
 
The complete error message including the code triggering it would be helpful I guess. Usually, when you hit illegal offset, this just means you're trying to access something with the wrong type. $reply->message should return a string, but you could give it a shot and try (string)$reply->message
 
The complete error message including the code triggering it would be helpful I guess. Usually, when you hit illegal offset, this just means you're trying to access something with the wrong type. $reply->message should return a string, but you could give it a shot and try (string)$reply->message

I'm discovering that the code does work locally. It fails on a live server. I'll try your suggestions. :D
 
Back
Top Bottom