XF 2.0 Attachment Count Icon to forum_view

Idhae

Active member
Hello,
im trying to do something like the vBulletin Stock feature. i put an attachment icon to these threads who include an attachment and count them.

vBulletin:
1531218662366.webp

Xenforo2:
1531218846775.webp


So far so good, but i did this so ->
i extend "XF\Entity\Thread" with a new "getter attach_count" and a "repository" to get the count for each thread

as next i have done a TMS on "thread_list_macros" and call to the getter $thread.attach_count and it works fine but i have logically for each thread one query...thats really bad. On a "forum_view" with 20 threads per page it calls 20querys only for the "count".

I now try to figure out how i can do this in "XF\Pub\Controller\Forum actionForum" with one query and save it in $threads so i could use it then in the template.

PHP:
<?php
namespace MY\ADDON\XF\Pub\Controller;

use XF\Mvc\ParameterBag;

class Forum extends XFCP_Forum
{
    public function actionForum(ParameterBag $params)
    {
        $parent = parent::actionForum($params);

        $threads = $parent->getParam('threads');
        $threadsIds = $threads->pluckNamed('thread_id');
        
        if($threadsIds){
              $threadsRepo = $this->repository('MY\ADDON:Thread');
              $attachCounts = $threadsRepo->findAttachmentCountsInThreads($threadsIds);

              foreach ($threads as $key => $thread) {
                      $thread->attachCount = $attachCounts[$key]['attach_count'];
              }
              $parent->setParam('threads', $threads);
         }
         return $parent;
    }
}

repository:
PHP:
<?php

namespace MY\ADDON\Repository;

use XF\Mvc\Entity\Repository;

class Thread extends Repository
{
    public function findAttachmentCountsInThreads($threadsIds)
    {
        $counts = $this->db()->fetchAllKeyed('
           SELECT thread_id, SUM(attach_count) AS attach_count
           FROM xf_post
           WHERE thread_id IN (' . implode(',', $threadsIds) . ')
           GROUP BY thread_id
          ', 'thread_id');

        return $counts;
    }
}



I hope someone help or give an idea how i could do this.

EDIT: now it works
 
Last edited:
Extend \XF\Entity\Thread
PHP:
<?php

namespace MY\ADDON\XF\Entity;

use XF\Mvc\Entity\Structure;

class Thread extends XFCP_Thread
{
   public $attachCount;
}

Edit: without structure no need to install
 
Last edited:
Now you can access the count in the "thread_list_macros" {$thread.attach_count}

I did it like that with a TMS:

Template: thread_list_macros

Find:
HTML:
<div class="structItem-cell structItem-cell--main" data-xf-init="touch-proxy">

Replace:

HTML:
<div class="structItem-cell structItem-cell--main" data-xf-init="touch-proxy">
    <xf:if is="{$thread->attachCount} > 0">
        <ul class="structItem-statuses">     
            <li>
                <i class="structItem-status structItem-status--attachmentIcon" aria-hidden="true" title="{$thread->attachCount} {{ $thread->attachCount > 1 ? ' Attachments' : ' Attachment'}}"></i>
                <span class="u-srOnly">{$thread->attachCount} {{ $thread->attachCount > 1 ? ' Attachments' : ' Attachment'}}</span>
            </li>
        </ul>
    </xf:if>
 
Last edited:
Top Bottom