• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Unable to Render Attachments

HeadHodge

Active member
#1
In my view I'm trying render a single post (an array of one).

Here is the code I'm using:
Code:
//*****************
//** Render Post **
//*****************
public function renderPost(&$posts, &$shared)
{
    $bbCodeParser = XenForo_BbCode_Parser::create(XenForo_BbCode_Formatter_Base::create('Base', ['view' => $this]));
    $bbCodeOptions = array(
            'states' => ['viewAttachments' => true],
            'contentType' => 'post',
            'contentIdKey' => 'post_id'
    );

    XenForo_ViewPublic_Helper_Message::bbCodeWrapMessages($posts, $bbCodeParser, $bbCodeOptions);

    $viewParams = array(
            'post'    => $posts[0],
            'member'    => $shared['member'],
    );


    $templateName = $shared['templatePrefix'].'_Detail';
    $content = $shared['public']->createTemplateObject($templateName, $viewParams)->render();
    return $content;
}
It renders the BBCodes and Images just fine, but won't render attachments. I'm probably just missing something simple, but I can't figure out what. Any ideas?

Here is a sample of one of my pages (you can see the attachments are rendered as links instead of images):

1.png

This is what it should look like:
2.png
 
Last edited:

Pierce

Well-known member
#3
ive looked and looked for "viewattachments" and that doesn't look valid.

The code check is this:

Code:
 if (empty($options['states']['attachments']) && !empty($message['attachments']))
i think its passing that which it later unsets the attachments in the post, which then gives you the view attachment.

Thats what I can read from the code.
 

HeadHodge

Active member
#4
ive looked and looked for "viewattachments" and that doesn't look valid.

The code check is this:

Code:
 if (empty($options['states']['attachments']) && !empty($message['attachments']))
i think its passing that which it later unsets the attachments in the post, which then gives you the view attachment.

Thats what I can read from the code.
Thanks for the replies!!

I finally figured it out. The attachement records need to be read and passed for the post.

Revised Code to make it work:
Code:
$attachmentModel = $shared['params']['controller']->getModelFromCache('XenForo_Model_Attachment');
$attachments = $attachmentModel->getAttachmentsByContentId('post', $thread['first_post_id']);

    $bbCodeParser = XenForo_BbCode_Parser::create(XenForo_BbCode_Formatter_Base::create('Base', ['view' => $this]));
    $bbCodeOptions = array(
            'contentType' => 'post',
            'contentIdKey' => 'post_id',
            'states' => [
                'viewAttachments' => true,
                'attachments' => $attachmentModel->prepareAttachments($attachments)
            ]
    );

    XenForo_ViewPublic_Helper_Message::bbCodeWrapMessages($threads, $bbCodeParser, $bbCodeOptions);
1.png