XF 2.3 What is the recommended way to get parent_node_id inside category_view or pass it correctly to a widget?

Motobaka

Active member

Issue: parent_node_id Not Available in category_view

The main problem is that parent_node_id is not available in category_view, which prevents XenForo from detecting if a category belongs to a specific parent category.

Attempts to Obtain parent_node_id:

  1. Directly in category_view ({$category.parent_node_id}) → Error because parent_node_id does not exist in XF:Category.
  2. Fetching it from the database within category_view → XenForo does not allow executing SQL queries inside templates.
  3. Passing node_id to the widget and fetching parent_node_id in cashwidget.php → Error because category.node_id was not being passed correctly.

Question:

What is the recommended way to get parent_node_id inside category_view or pass it correctly to a widget?
 
In the category_view template, $category refers to an \XF\Entity\Category object. This object has a relation, Node, which has the property $parent_node_id:

HTML:
{{ $category.Node.parent_node_id }}
 
How to pass category_id, category_title, and parent_node_id from category_view to a widget?

Message:
I am trying to pass information from a category (category_view) to a custom widget in XenForo.

I can successfully retrieve the following values in category_view:

  • category_id
  • category_title
  • parent_node_id
However, when I attempt to pass these values to the widget using context or options, the widget does not receive them.

What I have tried:

  1. Using context:

HTML:
<xf:widget key="widget" context="{ 'category_id': {$category.node_id}, 'category_title': {$category.title}, 'parent_node_id': {$category.Node.parent_node_id} }" />

  1. Using options:

HTML:
<xf:widget key="widget" options="{ 'category_id': {$category.node_id}, 'category_title': {$category.title}, 'parent_node_id': {$category.Node.parent_node_id} }" />
The widget does not receive these values either.

  1. I tried printing $context and $options inside the widget, but both appear empty.
Question:
What is the correct way to pass data from category_view to a custom widget in XenForo? Am I using context and options incorrectly, or is there a better approach?

Any guidance would be greatly appreciated.
 
I'd recommend just passing the entire category via context.

Template:
HTML:
<xf:widget key="some_widget" context-category="{$category}" />

Widget:
PHP:
/** @var \XF\Entity\Category $category */
$category = $this->contextParams['category'];
$nodeId = $category->node_id;
$title = $category->title;
$parentNodeId = $category->Node->parent_node_id;
 
Widget still not receiving category_id, category_title, and parent_node_id from category_view

After reviewing the previous responses and trying context-category="{$category}", the widget is still not receiving the data.

What we have tried so far:

  1. Passing the entire category with context-category:


HTML:
<span><span>&lt;<span>xf:widget</span> <span>key</span>=<span>"widget_1"</span> <span>context-category</span>=<span>"{$category}"</span> /&gt;</span><br></span>

The widget still shows "Not received" for category_id, category_title, and parent_node_id.

  1. Trying to access the data in the widget:

PHP:
<span><span>/** <span>@var</span> \XF\Entity\Category $category */</span><br><span>$category</span> = <span>$this</span>-&gt;contextParams[<span>'category'</span>] ?? <span>null</span>;<br></span>

Still not receiving the values correctly.

  1. Passing values individually with context:

HTML:
<span><span>&lt;<span>xf:widget</span> <span>key</span>=<span>"widget_1"</span><br>    <span>context</span>=<span>"{ <br>        'category_id': {$category.node_id}, <br>        'category_title': {$category.title}, <br>        'parent_node_id': {$category.Node.parent_node_id} <br>    }"</span> /&gt;</span><br></span>

But $context-&gt;contextParams['category_id'] still shows "Not received".

Question:
If context-category="{$category}" is not working to pass the entity to the widget, is there another recommended way to correctly pass category_id, category_title, and parent_node_id?

Any suggestions would be greatly appreciated.
 
Still not receiving the values correctly.
In what sense? If you dump($category) in the widget, is it null? What if you {{ dump($category) }} in the template?

  1. Passing values individually with context:
You would need to use separate context- attributes as I've shown in my example, but the result won't yield any different results than passing the $category whole anyway.
 
Back
Top Bottom