D
Deleted member 110473
Guest
Unmaintained - How to show a post in a page
Objective: I will teach you how to show a specific post in a XenForo page. I strongly recommend you to read my others tutorials to know better how XenForo works: Creating a add-on to insert tabs in profile page (using hooks) Creating a add-on...
xenforo.com
I found the above resource used for xf 1.x, and I noticed a lot of the calls don't transfer to xf 2.x now. I am a new developer, and would like some help. If anyone can point me to resources or changelogs I can look at that are for developers that sort help you figure out what became what, that will be great!
I don't have a lot of changes so far:
PHP:
<?php
/**
* How to show a post inside a page.
* This code have all the necessary to show a post inside a page. Just read the comments.
* Fuhrmann
*/
namespace Pages;
class ShowAPost
{
public static function showAPostInPage(
\XF\Pub\Controller\AbstractController $controller,
\XF\Mvc\Reply\AbstractReply &$response)
{
/* Set the ID of the post to be loaded */
$postId = 1;
/* Create a new ControllerHelper that will help us to get the post */
$ftpHelper = new XenForo_ControllerHelper_ForumThreadPost($controller);
/* Use the ControllerHelper to see if the post we want to get is viewable by the user browsing */
list($post, $thread) = $ftpHelper->assertPostValidAndViewable($postId);
/* If the post has attachments */
if ($post['attach_count'] > 0)
{
/* Let's get all the attachments of this post, if exists */
$attachmentModel = XenForo_Model::create('XenForo_Model_Attachment');
$attachments = $attachmentModel->getAttachmentsByContentId('post', $postId);
foreach ($attachments AS $attachment)
{
/* Insert into the post data the attachments */
$post['attachments'][$attachment['attachment_id']] = $attachmentModel->prepareAttachment($attachment)
;
}
}
/* These parameters will be used in our template. We need to pass them into the response view. The $post para
m will be used too in the XenForo_ViewPublic instance */
$viewParams = array (
'post' => $post,
'title' => $thread['title']
);
/* This way our post will be parsed and the aattachments too. :) */
$response = $controller->responseView('ShowAPost_View', $response->params['templateTitle'], $viewParams);
/* Return the response, finally!*/
return $response;
}
}