XF 2.0 Cant get Callback with returning data to work.

kaso

New member
Hello

I have followed many threads, but I can't seem to make a PHP callback that returns data properly. I keep getting the following error: An exception occurred: [TypeError] Argument 1 passed to Site\Pages\Providers::getData() must be an instance of XF\Pub\Controller\AbstractController, string given in src/addons/Site/Pages/Providers.php on line 6 What am I doing wrong?

Callback:
Code:
<?php

namespace Site\Pages;

class Providers {
    public static function getData(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply) {
        if ($reply instanceof \XF\Mvc\Reply\View) {
            $providers = \XF::db()->fetchAll("
                SELECT
                    t.thread_id,
                    t.view_count,
                    t.title,
                    p.post_id,
                    p.message,
                    p.post_date,
                    u.user_id,
                    u.username,
                    u.custom_title,
                    u.avatar_date,
                    g.banner_css_class,
                    g.banner_text
                FROM (
                    SELECT th.thread_id, th.title, th.view_count
                    FROM xf_thread AS th
                    INNER JOIN xf_thread_prefix AS px ON th.prefix_id=px.prefix_id
                    WHERE px.prefix_id=1
                ) AS t
                INNER JOIN xf_post AS p ON t.thread_id=p.thread_id
                INNER JOIN xf_user AS u ON p.user_id=u.user_id
                INNER JOIN xf_user_group AS g ON u.user_group_id=g.user_group_id
                WHERE p.position=0
                ORDER BY p.post_date DESC
            ");

            $reply->setParam('providers', $providers);
        }
    }
}

Template:
Code:
<xf:callback class="Site\Pages\Providers" method="getData"></xf:callback>
<xf:foreach loop="$providers" value="$provider">
     Admin={$provider.provider_key}:WhiteList // {$provider.user_id} {$provider.username}<br>
</xf:foreach>
 
Did you manage to solve this? I am using a similar code from some other post and running into the same error.
 
<xf:callback /> is not meant to make arbitrary data available in a template, it's meant to print a string output by your callback. Not sure where that code was copy-pasted from, but those function arguments aren't supposed to be there either, as the error suggests.

If you need the data available in the template itself, create a code event listener for templater_template_pre_render.
 
Back
Top Bottom