XF 2.2 Multiple Display Condition for a widget

smozgur

Well-known member
Hello,

Here is the scenario. I want to show the "XFES: Similar Threads" widget in a "Question" type forum only. Here is the condition which works well:

PHP:
$context.thread.discussion_type == 'question'

I also created a custom user field to allow users to hide the widget as a preference. Instead of creating a template modification to check the user preference (which works without problem), I wanted to use the widget display condition as I can access the Profile entity in the widget context. So here is the condition for that - bc_hide_similar_threads is the custom user field id:

PHP:
!$xf.visitor.Profile.custom_fields.bc_hide_similar_threads

Both conditions work great separately. So I was too sure that it will work if I combined them by using AND, which is an obvious thing! Anyway, I created an HTML widget to make sure with the following template:

PHP:
{{dump($context.thread.discussion_type == 'question' AND !$xf.visitor.Profile.custom_fields.bc_hide_similar_threads)}}

And this returns the correct boolean value as expected. So I should be able to use the following as the widget display condition:

PHP:
$context.thread.discussion_type == 'question' AND !$xf.visitor.Profile.custom_fields.bc_hide_similar_threads

But it doesn't work!

Could you please show me what I am missing in this?

Thanks.
 
Solution
Thank you, @Mouth.

I can't believe this was never documented anywhere. The actual context is injected into the widget template, but into the script rendering the template. I spent hours on this by thinking that I was doing/thinking something extremely wrong. Unbelievable.

Anyway, just for future readers who might need to do the same thing, we can use either $xf.reply.view or $xf.reply.template to see if it is a question type thread or not. I would prefer comparing with a real entity property but obviously we should live with this when we deal with widget conditions.

Display Condition
PHP:
$xf.reply.view == 'XF:Thread\ViewTypeQuestion' AND !$xf.visitor.Profile.custom_fields.bc_hide_similar_threads

Thanks again.
Thank you, @Mouth.

I can't believe this was never documented anywhere. The actual context is injected into the widget template, but into the script rendering the template. I spent hours on this by thinking that I was doing/thinking something extremely wrong. Unbelievable.

Anyway, just for future readers who might need to do the same thing, we can use either $xf.reply.view or $xf.reply.template to see if it is a question type thread or not. I would prefer comparing with a real entity property but obviously we should live with this when we deal with widget conditions.

Display Condition
PHP:
$xf.reply.view == 'XF:Thread\ViewTypeQuestion' AND !$xf.visitor.Profile.custom_fields.bc_hide_similar_threads

Thanks again.
 
Solution
Back
Top Bottom