XF 2.1 No data in widgets contextParams when called from template

Christer L

Member
If I add a widget (I wrote) by selecting one or more positions for it (in the sidebar for example), it has access to a protected variable contextParams. In it I can find information about the current forum or thread, depending on where widget displays.

But if I add the same widget using <xf:widget> in a template (as I want it above a thread list for example, not in sidebar) it does not have any data in contextParams. Is there a reason for this, or a bug?

If no bug, can I find the current forum id or thread id somewhere else from my widget code?
 
The concept of context parameters is something that is opt-in and so depends on those context params being explicitly defined.

For example the thread_view_sidebar position explicitly has context-thread passed in, which is the current thread.

You can use context params for directly invoked widgets too using the <xf:widget> tag.

You would just, for example, add:

Code:
<xf:widget key="your_widget" context-thread="{$thread}" />

And then your widget will have access to the thread context param where you can get the current thread_id, node_id and so on.
 
Excellent, thank you for quick answer. I had tried params to the tag, but did not know about the "context-" prefix.

BTW, when using PHP callback widget - can that PHP method access the contextParams somehow? The variable is protected in AbstractWidget and I could not find a public access method for that method to use. So I had to create a widget as an addon, just go get access to it.
 
I'll add a new public getContextParams method to the AbstractWidget class in a future XF version. We're planning a release tomorrow so this would be in version 2.1.10.

For now, although we wouldn't usually recommend it, you can do the following:

PHP:
$reflection = new ReflectionClass($widget); // the PhpCallback widget object is passed into the first argument of the PHP callback
$property = $reflection->getProperty('contextParams'); // get the contextParams property
$property->setAccessible(true); // set to be accessible

$contextParams = $property->getValue($widget);
 
We're planning a release tomorrow so this would be in version 2.1.10.
Oops! :ROFLMAO:
 
At the time I wrote that post I knew we were releasing 2.1.9 today.

2.1.10 will be released in the coming weeks That version would have been called 2.1.9 had the PayPal issue not come up yesterday so nothing has been postponed.
 
Back
Top Bottom