XF 2.0 $this->filter('var', 'int'); in widgets

AndrewSimm

Well-known member
I have some variables that I am getting from the address bar. These works perfectly in the controller. On the same page I have some widgets that display some statistics based on the variables from the address bar. Looking at my code below I think the issue is I can't figure out how to get parameterbag into the method, but I am not sure.

PHP:
<?php

namespace CIS\Backend\Widgets;

use \XF\Widget\AbstractWidget;
use XF\Mvc\ParameterBag;

class Rating extends AbstractWidget
{
    public function render()
    {

        //Page title
        $title = "Rating";

        //Get Class
        $get_class = $this->filter('class', 'int');

Error

ErrorException: Fatal Error: Call to undefined method CIS\Backend\Widgets\Rating::filter() src/addons/CIS/Backend/Widgets/Rating.php:17
 
Widgets won't have access to the request object by default. You would access this via $this->app->request().

Generally speaking, there isn't an expectation that a widget would require values from the query string. It's certainly quite dangerous in terms of inputs potentially meaning multiple things. If you want to do something like this, ideally it would be done by passing context variables through the widget position.
 
Widgets won't have access to the request object by default. You would access this via $this->app->request().

Generally speaking, there isn't an expectation that a widget would require values from the query string. It's certainly quite dangerous in terms of inputs potentially meaning multiple things. If you want to do something like this, ideally it would be done by passing context variables through the widget position.

How is it any more dangerous than getting values from the query string and using them in the page page controller?
 
Widgets are designed to sit on arbitrary pages, so you don't know the inputs they may expect and use. If you and the controller use the same input, unexpected things may happen.
 
Widgets are designed to sit on arbitrary pages, so you don't know the inputs they may expect and use. If you and the controller use the same input, unexpected things may happen.

These particular widgets are only going to sit on the 2 pages related to them. Basically I have a list of players and a roster list and has you filter the list it gives information based off of your filter. For instance if I filter players to only show Quarterbacks the widgets will filter down as well. So let's say the list is 10 Quarterbacks. The rating widget might show that 4 were 3 star rating, 4 were 4 star rating and 2 were 5 star rating. The main controller and the widgets will have the exact same parameters entered into the queries, with the widgets showing grouped information.
 
I understand the problem while trying to add pagination to a page with an widget.
The widget should never be in a normal page where we also have a pagination, i guess.

Should i add the pagination to the widget or to the page? (pages/test)?
And how can i use
$page = $this->filterPage();
to my widget/name/render()?

I will do the same addon tomorrow again without a widget; but i really would like to know how to do a pagination to /pages and/or widgets inside such a page.

I have seen a solution in recent posts from sirupo, but i would like to solve it with one file instead. But i have no idea how to get the ?page=x from the url.

Maybe with a GLOBAL?
 
Top Bottom