XF 2.2 Get data in $thread with node_id

mcatze

Well-known member
Hi, its me.. again.

I've try to build a little addon for default cover images in article threads. In every node option when article thread is set, you can upload a default image. It is stored per data assets and the url is set in db table xf_node.

Bildschirmfoto 2021-06-08 um 16.58.28.webp

My problem now is the way how to get this image in the thread template per node_id. I tried to extend the XF/Repository/Thread.php and find the imagePath per XF:Thread where node_id == $nodeId. But i am stucked.

PHP:
<?php

namespace XT\CoverArticleThread\XF\Repository;

use XF\Mvc\Entity\Finder;
use XF\Mvc\Entity\Repository;

class Thread extends XFCP_Thread
{
    public function findThreadsByNodeId($nodeId)
    {
        /** @var \XF\Finder\Thread $finder */
        $finder = $this->finder('XF:Thread');
        $finder
            ->with('Node')
            ->where('node_id', '=', $node_id);
       
        return $finder;
    }
}

Can someone give me a hint how to solve this problem?
 
you can access it directly via the Fourm which is usually fetched with the thread.
(fixed after Kirbys correction)
PHP:
$thread->Forum->Node->cover_image
This works, because the forum inherits from the AnstractNode which has a relation to the node entity.
 
Last edited:
I don't get it. I tried it directly in the template, but got an error. Try it with $thread.Forum.title and got the node_title, same with node_id.

How should i set the new var xt_coverImg_path as available param in the template?

Thanks for helping me to understand these relations.
 
PHP:
$thread->Forum->cover_image
Almost ;)

XF\Entity\Forum inherits from AbstractNode, not Node.

AbstractNode does define a relation Node to access the node entity.

So
PHP:
$thread->Forum->Node->cover_image
should work and can be accessed in templates directly.
 
Top Bottom