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

Unable to get template to show on forum index

Discussion in 'XenForo Development Discussions' started by Brogan, Nov 13, 2013.

  1. Brogan

    Brogan XenForo Moderator Staff Member

    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:

    The template is cta_featuredthreads_block and the contents are wrapped in: <xen:if is="{$canViewFeaturedThreads}">...</xen:if>.
    The permissions is set to allow:

    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:
    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:
    class CTA_FeaturedThreads_Model_Featured extends XenForo_Model
        public function canViewFeaturedThreads(array $viewingUser = null)
            if (XenForo_Permission::hasPermission($viewingUser['permissions'], 'ctaFt', 'ctaFtViewFeaturedThreads'))
                return true;
            return false;

    Finally, this is my library\CTA\FeaturedThreads\ControllerPublic\Forum.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?

    Shelley likes this.
  2. Brogan

    Brogan XenForo Moderator Staff Member

    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
    Adam Howard, Marcus and CyclingTribe like this.
  3. CyclingTribe

    CyclingTribe Well-Known Member

    Glad you got it sorted ... and you posted the solution; which is always appreciated. (y)
    Adam Howard likes this.
  4. Shelley

    Shelley Well-Known Member


    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. :)
    FredC likes this.
  5. Marcus

    Marcus Well-Known Member

    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 likes this.
  6. Brogan

    Brogan XenForo Moderator Staff Member

    Thanks Marcus.

    What are the benefits?

    Edit: Just read the text in the ACP:
    I'll go ahead and make those changes :)
    Last edited: Nov 13, 2013
    Marcus likes this.
  7. Brogan

    Brogan XenForo Moderator Staff Member

    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:
    $featureThreadModel = XenForo_Model::create('CTA_FeaturedThreads_Model_Featured');
    to this:
    $featureThreadModel = $this->getModelFromCache('CTA_FeaturedThreads_Model_Featured');
    Last edited: Nov 14, 2013
  8. tyteen4a03

    tyteen4a03 Well-Known Member

    I'm interested in how you managed to code XF addons without knowing about $this. (No offense, genuinely interested)
  9. Brogan

    Brogan XenForo Moderator Staff Member

    Lots of trial and error. And swearing.
    Mostly swearing.

    I started learning php and developing about a week ago.

    I still have no idea what most of it means but I'm picking it up.
    Steve F, iguanairs, RobinHood and 2 others like this.

Share This Page