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

Unable to Render Attachments

Discussion in 'XenForo Development Discussions' started by HeadHodge, Jun 21, 2016.

  1. HeadHodge

    HeadHodge Member

    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: Jun 21, 2016
  2. Pierce

    Pierce Active Member

  3. Pierce

    Pierce Active Member

    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.
     
  4. HeadHodge

    HeadHodge Member

    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
     
    Pierce likes this.

Share This Page