1. 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

Discussion in 'XenForo Development Discussions' started by Matthew Hawley, Jun 13, 2013.

  1. Matthew Hawley

    Matthew Hawley Well-Known Member

    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!
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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"
     
  3. Matthew Hawley

    Matthew Hawley Well-Known Member

    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.
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    I'd need to see this file to identify the cause of that error:

    /library/FindPostsInThread/ControllerPublic/FindPostsInThread.php
     
  5. Matthew Hawley

    Matthew Hawley Well-Known Member

    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);       
            }
        }
                   
                }           
           
       
    
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    Sorry, maybe:

    /library/FindPostsInThread/ControllerPublic/Thread.php
     
  7. Matthew Hawley

    Matthew Hawley Well-Known Member

    I don't have that.
     
  8. Matthew Hawley

    Matthew Hawley Well-Known Member

    Man this is really hard. I just want to put a template where the thread tools and watch thread button are.
     
  9. Chris D

    Chris D XenForo Developer Staff Member

    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
     
  10. Chris D

    Chris D XenForo Developer Staff Member

    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. Matthew Hawley

    Matthew Hawley Well-Known Member

    I used the template from your ThreadWatchers addon.
     
  12. Chris D

    Chris D XenForo Developer Staff Member

    Well my ThreadWatchers add-on extends controllers and models... so most of that code isn't necessary for what you want to do.
     
  13. Matthew Hawley

    Matthew Hawley Well-Known Member

    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
     
  14. Chris D

    Chris D XenForo Developer Staff Member

    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. Matthew Hawley

    Matthew Hawley Well-Known Member

    I followed lawrences thread http://xenforo.com/community/threads/creating-an-addon.5416/. except at the end which i didn't need.
     
  16. Chris D

    Chris D XenForo Developer Staff Member

    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. Matthew Hawley

    Matthew Hawley Well-Known Member

    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. Matthew Hawley

    Matthew Hawley Well-Known Member

    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?
     
  19. Chris D

    Chris D XenForo Developer Staff Member

    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 $hookParamsXenForo_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. Matthew Hawley

    Matthew Hawley Well-Known Member

    Okay that worked. But the code isn't showing up on the thread.
     

Share This Page