Multiple thread ids and ID Options xf

Allan

Well-known member
Hello,

I would like to display text in specific threads, so use this and I replace:
<xen:if is="in_array({$thread.thread_id}, array(x, y, z))">
with

<xen:if is="in_array({$thread.thread_id}, array({$xenOptions.tc_st_ids_block2}))">

My template:
Code:
<xen:if is="in_array({$thread.thread_id}, array({$xenOptions.tc_st_ids_block2}))">
My code here
</xen:if>

In Option to add-on, i have:
Screenshot_2.webp
Screenshot_3.webp
The problem is that my code is displayed only in the thread with ID 8 but not with ID 17

It does not take into account ", 17"

Someone would have any idea ?
 
Last edited:
If i put this, it work:

<xen:if is="in_array({$thread.thread_id}, array(8,17))">

If i put this, no:

<xen:if is="in_array({$thread.thread_id}, array({$xenOptions.tc_st_ids_block2}))">
 
What does $xenOptions.tc_st_ids_block2 look like if you print or dump it in a template?
 
Which template is this being added to?
I have create a new template tc_sidebar_test and i add <xen:include template="tc_sidebar_test" /> in thread_view template.

Not using $thread.thread_id it won't as it's not available in that template.

You can confirm that by following this guide: https://xenforo.com/community/resources/using-variables-in-templates.5034/
If i add this in thread_view template:
Code:
{xen:helper dump, $xenOptions.tc_st_ids_block2}

Result:
Screenshot_7.webp
 
Not using $thread.thread_id it won't as it's not available in that template.

You can confirm that by following this guide: https://xenforo.com/community/resources/using-variables-in-templates.5034/

That is not an answer to his question. For testing he can easily replace the variable with an actual number contained in the array to see if it works, regardless of the template being used. Awful lot of these type of "answers" going around here.

I haven't tested this, but I I doubt it doesn't work because the data type is set to a string, not an array. If you sent the option to a php function you can explode it into an array, in the template not so much. The array function in the template sees this: array[0] => "23,25"

But, what do I know, I'm probably wrong, but at least it is a little more than a link to a guide that doesn't answer the original question.
 
I have a feeling @Lawrence is right (although it wouldn't explain why the initial ID was working).

You can't explode it via the validation callback though (it doesn't let you change a string to an array).

Really, the only way to do it would be to use a listener to get the option, explode it into an array, then set it as a new parameter in the template...

Liam
 
@Allan My solution, if you do not want to use a callback: don't use a comma separated string for thread ids in your options, but rather have 5 (or whatever) integer boxes to allow for the inputting of the thread ids, and then in the appropriate template:

<xen:set var="$threadIds.1">{$xenOptions.threadId_1}</xen:set>
<xen:set var="$threadIds.2">{$xenOptions.threadId_2}</xen:set>
<xen:set var="$threadIds.3">{$xenOptions.threadId_3}</xen:set>
<xen:set var="$threadIds.4">{$xenOptions.threadId_4}</xen:set>
<xen:set var="$threadIds.5">{$xenOptions.threadId_5}</xen:set>

<xen:if is="in_array({$thread.thread_id}, {$threadIds})">hello</xen:if>
 
That is not an answer to his question. For testing he can easily replace the variable with an actual number contained in the array to see if it works, regardless of the template being used. Awful lot of these type of "answers" going around here.

I haven't tested this, but I I doubt it doesn't work because the data type is set to a string, not an array. If you sent the option to a php function you can explode it into an array, in the template not so much. The array function in the template sees this: array[0] => "23,25"

But, what do I know, I'm probably wrong, but at least it is a little more than a link to a guide that doesn't answer the original question.
I think the passive aggressive attitude here is somewhat uncalled for and unhelpful. Given the context of what Allan said, Brogan's response was correct and wasn't proposed as an actual solution. To me it felt like more of an aside point which was very valid. We're all just trying to help so I don't think there's a place for this sort of attitude. If you wish to discuss the further, please contact me privately.

As it happens, your solution seems valid, though if I may make a further suggestion...

I would suggest implementing the option similarly to the facebookAdmins option. It's fairly intuitive from a UI point of view; separate fields for each thread ID, but also cleaner from a code point of view. The values will be stored as a serialized string and automatically converted to an array which you can just use directly in the template as desired. It also solves problems with things like trailing commas which would result in some further weirdness.
 
@Allan My solution, if you do not want to use a callback: don't use a comma separated string for thread ids in your options, but rather have 5 (or whatever) integer boxes to allow for the inputting of the thread ids, and then in the appropriate template:

<xen:set var="$threadIds.1">{$xenOptions.threadId_1}</xen:set>
<xen:set var="$threadIds.2">{$xenOptions.threadId_2}</xen:set>
<xen:set var="$threadIds.3">{$xenOptions.threadId_3}</xen:set>
<xen:set var="$threadIds.4">{$xenOptions.threadId_4}</xen:set>
<xen:set var="$threadIds.5">{$xenOptions.threadId_5}</xen:set>

<xen:if is="in_array({$thread.thread_id}, {$threadIds})">hello</xen:if>
Any help is appreciated, thank you :)

I would suggest implementing the option similarly to the facebookAdmins option. It's fairly intuitive from a UI point of view; separate fields for each thread ID, but also cleaner from a code point of view. The values will be stored as a serialized string and automatically converted to an array which you can just use directly in the template as desired. It also solves problems with things like trailing commas which would result in some further weirdness.
Good idea, it's done :)

Screenshot_8.webp

But, how to call the id then?
 
@Chris D , i would like use several times this fonction:
Screenshot_8.webp

I create a new template (option_template_tcsidebarthreadsAdmins) with the same content as the option_template_facebookAdmins template for my Option.

I use the same system several times, but the button does not function to operate on one of two. I use several times the same template? (option_template_tcsidebarthreadsAdmins)

My option is correct ?
Screenshot_9.webp

Look the gif: https://gyazo.com/6d7a49b7fa5eee346c55ce2751aa6044
 
I wonder if I need to create several templates with the same content but a different name for each option?
 
I wonder if I need to create several templates with the same content but a different name for each option?
Yes. It's generally bad practice to copy entire pieces of code in such a way anyway.

You'll see when you dissect the code that the FieldAdder function looks at a specific element to know elements to target.

You could actually do it all with a single template, just the selector for the field adder would need to be different somehow... ;)
 
Yes. It's generally bad practice to copy entire pieces of code in such a way anyway.

You'll see when you dissect the code that the FieldAdder function looks at a specific element to know elements to target.

You could actually do it all with a single template, just the selector for the field adder would need to be different somehow... ;)
Strange, it don't work.

Example, i create 2 template:
- option_template_tcsidebarthreadsAdmins
- option_template_tcsidebarthreadsAdmins2

Same content for both templates:
Code:
<xen:controlunit label="{$preparedOption.title}">

    <xen:html>
        <ul>
            <xen:foreach loop="{$preparedOption.option_value}" value="$admin">
                <xen:if is="{$admin} !== ''">
                    <li class="fbAdmin"><xen:textbox name="{$fieldPrefix}[{$preparedOption.option_id}][]" value="{$admin}" placeholder="Thread ID" /></li>
                </xen:if>
            </xen:foreach>
            <li class="fbAdmin"><xen:textbox name="{$fieldPrefix}[{$preparedOption.option_id}][]" value="" placeholder="Thread ID" /></li>
        </ul>
        <div style="margin-bottom: 5px"><input type="button" value="Add Another Thread ID" class="button smallButton FieldAdder" data-source="li.fbAdmin" /></div>
        <input type="hidden" name="{$listedFieldName}" value="{$preparedOption.option_id}" />
        {xen:raw $editLink}
    </xen:html>

    <xen:hint>Add Threads IDs where to show this block</xen:hint>
    <xen:explain>{xen:raw $preparedOption.explain}</xen:explain>

</xen:controlunit>

My options:
option1.webp option2.webp

I made a mistake ?
 
Back
Top Bottom