XF 2.0 Problem to display value with my widget

SyTry

Well-known member
Hello,

I make an add-on and I have a widget with most followers (top 5) but it won't work :
192791

I use this in my template :
HTML:
<xf:foreach loop="$users" value="$users">
    {$user.sc_most_followers|number}
</xf:foreach>
This in my XF\Widget :
PHP:
    public function render()
    {
        $finder = \XF::finder('XF:User');
        $users = $finder->where('sc_user_follow', '>', 0)
            ->where('is_banned', false)
            ->order('sc_user_follow', 'DESC');

        $option = \XF::options();
        $viewParams = [
            'enable' => $option->scMostFollowersEnable,
            'followersLimit' => $option->followersLimit,
            'users' => $users
        ];

        return $this->renderer('sc_mostFollowersWidget', $viewParams);
    }
I have this in my XF\Entity :
PHP:
    public static function getStructure(Structure $structure)
    {
        $structure = parent::getStructure($structure);

        $structure->columns['sc_user_follow'] = ['type' => self::UINT, 'default' => 0, 'forced' => true, 'changeLog' => false];

        return $structure;
    }
And this in my Setup.php :
PHP:
    public function installStep1()
    {
        $sm = $this->schemaManager();

        $sm->alterTable('xf_user', function(Alter $table)
        {
            $table->addColumn('sc_user_follow', 'int')->setDefault(0);
            $table->addKey('sc_user_follow', 'most_followers');
        });
    }
Do you have an idea why my widget don't work ? Maybe my $users variable in XF\Widget :unsure:
More :
Thank you !

Regards, SyTry
 

SyTry

Well-known member
can you post the whole template?
Hello @nocte !

This is my template sc_mostFollowersWidget :
HTML:
<xf:if is="{$enable}">
    <div class="block"{{ widget_data($widget) }}>
        <div class="block-container">
            <h3 class="block-minorHeader">
                <i class="fa fa-{$xf.options.dntIcon}" style="padding-right:3px;"></i><a href="{{ link('members/&key=most_followers') }}">
                    {{ phrase('sc_most_followers') }}
                </a>
            </h3>
            <ul class="block-body">
            <xf:foreach loop="$users" value="$user">
                <li class="block-row">
                    <div class="contentRow">
                        <div class="contentRow-figure">
                            <xf:avatar user="$user.user" size="s" />
                        </div>
                        <div class="contentRow-main">
                            <div class="contentRow-extra contentRow-extra--largest">
                                {$user.sc_most_followers|number}
                            </div>
                            <div class="contentRow-header">
                                <xf:username user="$user.user" rich="true" />
                            </div>

                            <div class="contentRow-lesser">
                                <xf:usertitle user="$user.user" />
                            </div>
                        </div>
                    </div>
                </li>
            </xf:foreach>
            </ul>
        </div>
    </div>
</xf:if>
 

SyTry

Well-known member
Frist: Replace all $user.user with just $user.
Done, now I have this :
193384

EDIT : Template public:sc_mostFollowersWidget: Accessed unknown getter 'sc_most_followers' on XF:User[2] (src\XF\Mvc\Entity\Entity.php:178)
 

nocte

Well-known member
fine. Now replace:
PHP:
{$user.sc_most_followers|number}
with:
PHP:
{{$user.sc_most_followers|number}}
 

nocte

Well-known member
This in my XF\Widget :
is that the function you have right now? Just wonder why it does work without a ->fetch().. You should also limit the query, e.g. ->limit(10) - otherwise you would fetch all users who have a followers count!
 

SyTry

Well-known member
Oops, you're right : sc_most_followers is my phrase, not my column.. 😂

Now it work :
193385

Thank you boss ! :D
is that the function you have right now? Just wonder why it does work without a ->fetch().. You should also limit the query, e.g. ->limit(10) - otherwise you would fetch all users who have a followers count!
Yes I have this :
PHP:
<?php

namespace SyTryC\MostFollowersMember\XF\Widget;

use XF\Widget\AbstractWidget;

class MostFollowers extends AbstractWidget {

    protected $defaultOptions = [
        'followersLimit' => 5
    ];

    public function render()
    {
        $finder = \XF::finder('XF:User');
        $usersFollow = $finder->where('sc_user_follow', '>', 0)
            ->where('is_banned', false)
            ->order('sc_user_follow', 'DESC')
            ->limit($this->options['followersLimit']);

        $option = \XF::options();
        $viewParams = [
            'enableFollow' => $option->scMostFollowersEnable,
            'followersLimit' => $options->followersLimit,
            'usersFollow' => $usersFollow
        ];

        return $this->renderer('sc_mostFollowersWidget', $viewParams);
    }

    public function verifyOptions(\XF\Http\Request $request, array &$options, &$error = null)
    {
        $options = $request->filter([
            'followersLimit' => 'uint'
        ]);

        if ($options['followersLimit'] < 1)
        {
            $options['followersLimit'] = 1;
        }

        return true;
    }
}
 
Last edited:
Top