XF 2.1 get data from Listener to template callback

shqawe

Member
i'm trying to make to make addon to get last 10 threads for user

so what i did is:

i create code event listener as below
Listen to event: templater_template_pre_render
Event hint: public:Last_ten_user_threads
Execute callback : class=LastTenUserThreads\Listener::getData

and my listener file is:


PHP:
namespace LastTenUserThreads;

use XF\Mvc\Entity\Entity;
use XF\Template\Templater;

class Listener
{
/**
 * Undocumented function
 *
 * @param Templater $templter
 * @param [type] $type
 * @param [type] $template
 * @param array $params
 * @return void
 */
    public static function getdata(
        Templater $templater, &$type, &$template, array &$params
        )
    {
        $visitor = \XF::visitor();
        $finder = \XF::finder('XF:Thread');
        $last10threads = $finder->where('user_id','=', $visitor->user_id)
                                    ->order('thread_id', 'DESC')
                                    ->limit(10)
                                     ->fetch();

        foreach ($last10threads as $last10thread) {
            $params [] = $last10thread;
        }


my template which is Last_ten_user_threads:

[CODE=html]<xf:callback class="LastTenUserThreads\Listener" method="getData"></xf:callback>
<xf:foreach loop="$params" value="$param">
     <div class="container_last10">
         <div class="userthread">
             <a>{$param.title}</a>
         </div>
    </div>
</xf:foreach>

return $params;
}
}[/CODE]

so when i run this i got this error:

PHP:
An exception occurred: [TypeError] Argument 1 passed to LastTenUserThreads\Listener::getdata() must be an instance of XF\Template\Templater, string given in src\addons\LastTenUserThreads\Listener.php on line 20

    LastTenUserThreads\Listener::getdata()
    call_user_func() in src/XF/Template/Templater.php at line 1389
    XF\Template\Templater->callback() in internal_data/code_cache/templates/l2/s1/public/Last_ten_user_threads.php at line 6
    XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 1301
    XF\Template\Templater->renderTemplate() in src/XF/Template/Templater.php at line 1374
    XF\Template\Templater->includeTemplate() in internal_data/code_cache/templates/l2/s1/public/message_macros.php at line 112
    XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 701
    XF\Template\Templater->callMacro() in internal_data/code_cache/templates/l2/s1/public/post_macros.php at line 59
    XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 701
    XF\Template\Templater->callMacro() in internal_data/code_cache/templates/l2/s1/public/thread_view.php at line 513
    XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 1301
    XF\Template\Templater->renderTemplate() in src/XF/Template/Template.php at line 24
    XF\Template\Template->render() in src/XF/Mvc/Renderer/Html.php at line 48
    XF\Mvc\Renderer\Html->renderView() in src/XF/Mvc/Dispatcher.php at line 418
    XF\Mvc\Dispatcher->renderView() in src/XF/Mvc/Dispatcher.php at line 400
    XF\Mvc\Dispatcher->renderReply() in src/XF/Mvc/Dispatcher.php at line 360
    XF\Mvc\Dispatcher->render() in src/XF/Mvc/Dispatcher.php at line 53
    XF\Mvc\Dispatcher->run() in src/XF/App.php at line 2177
    XF\App->run() in src/XF.php at line 390
    XF::runApp() in index.php at line 20

so can someone tell what's wrong please.
 
Back
Top Bottom