Unable to get template to show on forum index

Brogan

XenForo moderator
Staff member
#1
I've been struggling with this for a few days now but just can't seem to get it to work.

Basically I am trying to get a template to show on the forum index, based on a global permission.
Something like this:
upload_2013-11-13_8-39-40.png

The template is cta_featuredthreads_block and the contents are wrapped in: <xen:if is="{$canViewFeaturedThreads}">...</xen:if>.
The permissions is set to allow:
upload_2013-11-13_8-41-19.png

If I change the xen:if in the template to <xen:if is="{$visitor.permissions.ctaFt.ctaFtViewFeaturedThreads}"> then it works, so I know my permission and template modification are all working.

This is what my library\CTA\FeaturedThreads\Listener.php looks like:
Code:
<?php

class CTA_FeaturedThreads_Listener
{

    public static function extendForumController ($class, array &$extend)
    {
        if ($class == 'XenForo_ControllerPublic_Forum')
        {
            $extend[] = 'CTA_FeaturedThreads_ControllerPublic_Forum';
        }
    }
}

And my library\CTA\FeaturedThreads\Model\Featured.php:
Code:
<?php

class CTA_FeaturedThreads_Model_Featured extends XenForo_Model
{

    public function canViewFeaturedThreads(array $viewingUser = null)
    {
        $this->standardizeViewingUserReference($viewingUser);

        if (XenForo_Permission::hasPermission($viewingUser['permissions'], 'ctaFt', 'ctaFtViewFeaturedThreads'))
        {
            return true;
        }

        return false;
    }
}

Finally, this is my library\CTA\FeaturedThreads\ControllerPublic\Forum.php:
Code:
<?php

class CTA_FeaturedThreads_ControllerPublic_Forum extends XFCP_CTA_FeaturedThreads_ControllerPublic_Forum
{

    public function actionIndex()
    {
        $parent = parent::actionIndex();

        $forum = $parent->params['forum'];

        $featureThreadModel = XenForo_Model::create('CTA_FeaturedThreads_Model_Featured');

        $parent->params['canViewFeaturedThreads'] = $featureThreadModel->canViewFeaturedThreads($forum);

        return $parent;
    }
}

If I had any hair I would have ripped it out by now...

Does anyone have any clues as to why it's not working?

Thanks.
 

Brogan

XenForo moderator
Staff member
#2
So after a bit of digging (and help from Chris) I managed to sort it.

I edited the code event listener to make it generic and edited the listener function to extendControllers (to cater for additional functions).

The main issue though was $forum = $parent->params['forum']; in my controller.
I have been struggling with that from the start, I just couldn't work out what that should be.

Of course, it shouldnt even be there, so I removed that line and edited
$parent->params['canViewFeaturedThreads'] = $featureThreadModel->canViewFeaturedThreads($forum); to remove the forum record, like so:
$parent->params['canViewFeaturedThreads'] = $featureThreadModel->canViewFeaturedThreads();

And now it works! :D
 

Shelley

Well-known member
#4
I've been struggling with this for a few days now but just can't seem to get it to work.



If I had any hair I would have ripped it out by now...
:LOL:

I ought to learn also how to build add-ons but my patience is far too short and know I'd be a bull in a china shop. Good to hear you got it sorted. :)
 

Marcus

Well-known member
#5
Some suggestions to your code:

In the Controller you could use the function getModelFromCache instead of ::create.

if ($class == 'XenForo_ControllerPublic_Forum') could be just removed, instead use this as hint in the acp listener: XenForo_ControllerPublic_Forum
 

Brogan

XenForo moderator
Staff member
#6
Thanks Marcus.

What are the benefits?

Edit: Just read the text in the ACP:
When certain events are triggered, they will be triggered with a hint. This hint can be used to limit your listener to only being executed when it's needed, increasing performance. If an event is triggered with a hint, the potential hint values will be listed in the event description above. If you specify a hint here, your listener will only be run if your hint matches the hint provided when the event is triggered.
I'll go ahead and make those changes :)
 
Last edited:

Brogan

XenForo moderator
Staff member
#7
In the Controller you could use the function getModelFromCache instead of ::create.
That was a tough one.
After lots of help from @Chris Deeming I finally managed to get that working.

Oh and I now know what $this-> is for :D

For anyone who's interested, I changed this:
Code:
$featureThreadModel = XenForo_Model::create('CTA_FeaturedThreads_Model_Featured');
to this:
Code:
$featureThreadModel = $this->getModelFromCache('CTA_FeaturedThreads_Model_Featured');
 
Last edited:

tyteen4a03

Well-known member
#8
That was a tough one.
After lots of help from @Chris Deeming I finally managed to get that working.

Oh and I now know what $this-> is for :D

For anyone who's interested, I changed this:
Code:
$featureThreadModel = XenForo_Model::create('CTA_FeaturedThreads_Model_Featured');
to this:
Code:
$featureThreadModel = $this->getModelFromCache('CTA_FeaturedThreads_Model_Featured');
I'm interested in how you managed to code XF addons without knowing about $this. (No offense, genuinely interested)
 
Top