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

I get an error with this template

Matthew Hawley

Well-known member
#1
Okay, so I get this error:

Fatal error: Cannot redeclare class FindPostsInThread_Listener in /home/xxx/public_html/xxx/store/library/FindPostsInThread/ControllerPublic/FindPostsInThread.php on line 4

Code:
<?php

class FindPostsInThread_Listener
{
    public static function extendThreadController($class, array &$extend)
    {
        if ($class == 'XenForo_ControllerPublic_Thread')
        {
            $extend[] = 'FindPostsInThread_ControllerPublic_Thread';
        }      
    }
  
    public static function extendThreadModel($class, array &$extend)
    {
        if ($class == 'XenForo_Model_Thread')
        {
            $extend[] = 'FindPostsInThread_Model_Thread';
        }      
    }
  
    public static function templateCreate($templateName, array &$params,

XenForo_Template_Abstract $template)
    {
        if ($templateName == 'thread_view')
        {
            $template->preloadTemplate('find_posts_in_thread_view');
        }
    }  
  
    public static function templateHook($hookName, &$contents, array $hookParams,
XenForo_Template_Abstract $template)
    {
        if ($hookName == 'thread_view_pagenav_before')
        {
            $params = $template->getParams();
            $params += $hookParams;
          
            $contents .= $template->create('find_posts_in_thread_view',
$params);      
        }
    }
              
            }
Any suggestions? Thanks!
 

Chris D

XenForo developer
Staff member
#2
You have two files that begin with

PHP:
<?php

FindPostsInThread_Listener
Judging from the error, this file: /library/FindPostsInThread/ControllerPublic/FindPostsInThread.php

Contains "class FindPostsInThread_Listener" whereas that file should contain instead "class FindPostsInThread_ControllerPublic_FindPostsInThread"
 

Matthew Hawley

Well-known member
#3
Okay that took away the error but now I get another error on the thread page.

The controller XenForo_ControllerPublic_Thread does not define an action called Index.
 

Chris D

XenForo developer
Staff member
#4
I'd need to see this file to identify the cause of that error:

/library/FindPostsInThread/ControllerPublic/FindPostsInThread.php
 

Matthew Hawley

Well-known member
#5
Code:
<?php
 
class FindPostsInThread_ControllerPublic_FindPostsInThread
{
    public static function extendThreadController($class, array &$extend)
    {
        if ($class == 'XenForo_ControllerPublic_Thread')
        {
            $extend[] = 'FindPostsInThread_ControllerPublic_Thread';
        }       
    }
   
    public static function extendThreadModel($class, array &$extend)
    {
        if ($class == 'XenForo_Model_Thread')
        {
            $extend[] = 'FindPostsInThread_Model_Thread';
        }       
    }
   
    public static function templateCreate($templateName, array &$params,

XenForo_Template_Abstract $template)
    {
        if ($templateName == 'thread_view')
        {
            $template->preloadTemplate('find_posts_in_thread_view');
        }
    }   
   
    public static function templateHook($hookName, &$contents, array $hookParams,
XenForo_Template_Abstract $template)
    {
        if ($hookName == 'thread_view_pagenav_before')
        {
            $params = $template->getParams();
            $params += $hookParams;
           
            $contents .= $template->create('find_posts_in_thread_view',
$params);       
        }
    }
               
            }
 

Chris D

XenForo developer
Staff member
#9
Hmm.

Well that might be the problem.

You know the class names, e.g.
FindPostsInThread_ControllerPublic_Thread

Well they need to correspond to actual file names. You basically replace the _ with / and at the end place a .php

So if you have a class name defined of FindPostsInThread_ControllerPublic_Thread then the filename should br FindPostsInThread/ControllerPublic/Thread.php
 

Chris D

XenForo developer
Staff member
#10
Then why do you have all of this:

PHP:
    public static function extendThreadController($class, array &$extend)
    {
        if ($class == 'XenForo_ControllerPublic_Thread')
        {
            $extend[] = 'FindPostsInThread_ControllerPublic_Thread';
        }     
    }
 
    public static function extendThreadModel($class, array &$extend)
    {
        if ($class == 'XenForo_Model_Thread')
        {
            $extend[] = 'FindPostsInThread_Model_Thread';
        }     
    }
 
#11
Then why do you have all of this:

PHP:
    public static function extendThreadController($class, array &$extend)
    {
        if ($class == 'XenForo_ControllerPublic_Thread')
        {
            $extend[] = 'FindPostsInThread_ControllerPublic_Thread';
        }    
    }

    public static function extendThreadModel($class, array &$extend)
    {
        if ($class == 'XenForo_Model_Thread')
        {
            $extend[] = 'FindPostsInThread_Model_Thread';
        }    
    }
I used the template from your ThreadWatchers addon.
 

Chris D

XenForo developer
Staff member
#12
Well my ThreadWatchers add-on extends controllers and models... so most of that code isn't necessary for what you want to do.
 
#13
If I change the file to Thread.php I get this.

Fatal error: Class 'FindPostsInThread_ControllerPublic_FindPostsInThread' not found in /home/mcwh/public_html/letspwn.com/store/library/XenForo/FrontController.php on line 408
 

Chris D

XenForo developer
Staff member
#14
I think the thing you need to do is start from the beginning.

Referring to other people's code is a good idea. But you need to be understanding what it is you're doing instead of just hacking bits of code in and out.

You're trying to run a marathon before you can walk.

Have you looked at any tutorials?

@Fuhrmann has a great tutorial in the Resource Manager and @Kier has a great video tutorial on template hooks (which is roughly what you want to be doing).
 
#15
I think the thing you need to do is start from the beginning.

Referring to other people's code is a good idea. But you need to be understanding what it is you're doing instead of just hacking bits of code in and out.

You're trying to run a marathon before you can walk.

Have you looked at any tutorials?

@Fuhrmann has a great tutorial in the Resource Manager and @Kier has a great video tutorial on template hooks (which is roughly what you want to be doing).
I followed lawrences thread http://xenforo.com/community/threads/creating-an-addon.5416/. except at the end which i didn't need.
 

Chris D

XenForo developer
Staff member
#16
Nothing wrong with that tutorial, but I think the one by @Fuhrmann might be a good start for what you want to do.

Lawrence's tutorial goes straight in to extending controllers and I don't think that's what you need to do right now.
 
#17
Okay so I followed Kiers video and my template looks exactly like his except for some things, but I get this error when I save the code event listener.

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/xxx/public_html/xxx/store/library/FindPostsInThread/Listener.php on line 9

Code:
<?php

class FindPostsInThread_Listener
{
    public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'thread_view_pagenav_before')
        {
            $content .= '<xen:if is="{$hasPosted}"><label><a href="{xen:link search/search, '', 'type=post', 'thread_id={$thread.thread_id}', 'users={$visitor.username}'}">Your Posts</a></label><xen:else /></xen:if>';
        }
    }
}
 
#18
Okay so its an error with

Code:
<xen:if is="{$hasPosted}"><label><a href="{xen:link search/search, '', 'type=post', 'thread_id={$thread.thread_id}', 'users={$visitor.username}'}">Your Posts</a></label><xen:else /></xen:if>
I replaced all that with hello, and it saved. and i put back the code above and I got an error. I do I put the above code in the template?
 

Chris D

XenForo developer
Staff member
#19
You're close.

You can't use XenForo template syntax in PHP.

You need to create a new template in the Master Style called something like "find_posts_in_thread_your_posts" (choose your add-on from the Add-on drop down).

Inside it, put the desired template code.

Then your Listener should look like this:

PHP:
<?php

class FindPostsInThread_Listener
{
    public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'thread_view_pagenav_before')
        {
            $content .= $template->create('find_posts_in_thread_your_posts', $template->getParams());
        }
    }
}
See, you're injecting an actual XenForo template into an existing template (sort of like using <xen:include template ) instead of typing the template code directly in.
 
#20
You're close.

You can't use XenForo template syntax in PHP.

You need to create a new template in the Master Style called something like "find_posts_in_thread_your_posts" (choose your add-on from the Add-on drop down).

Inside it, put the desired template code.

Then your Listener should look like this:

PHP:
<?php

class FindPostsInThread_Listener
{
    public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'thread_view_pagenav_before')
        {
            $content .= $template->create('find_posts_in_thread_your_posts', $template->getParams());
        }
    }
}
See, you're injecting an actual XenForo template into an existing template (sort of like using <xen:include template ) instead of typing the template code directly in.
Okay that worked. But the code isn't showing up on the thread.