1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Multiple thread ids and ID Options xf

Discussion in 'XenForo Development Discussions' started by Allan, Feb 19, 2016.

  1. Allan

    Allan Well-Known Member


    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))">

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

    My template:
    <xen:if is="in_array({$thread.thread_id}, array({$xenOptions.tc_st_ids_block2}))">
    My code here
    In Option to add-on, i have:
    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: Feb 19, 2016
  2. Allan

    Allan Well-Known Member

    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}))">
  3. Brogan

    Brogan XenForo Moderator Staff Member

    What does $xenOptions.tc_st_ids_block2 look like if you print or dump it in a template?
  4. Allan

    Allan Well-Known Member

    On forum_list template, it work:



  5. Chris D

    Chris D XenForo Developer Staff Member

    Which template is this being added to?
  6. Brogan

    Brogan XenForo Moderator Staff Member

    Allan likes this.
  7. Allan

    Allan Well-Known Member

    I have create a new template tc_sidebar_test and i add <xen:include template="tc_sidebar_test" /> in thread_view template.

    If i add this in thread_view template:
    {xen:helper dump, $xenOptions.tc_st_ids_block2}
  8. Lawrence

    Lawrence Well-Known Member

    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.
  9. Liam W

    Liam W Well-Known Member

    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...

    Allan likes this.
  10. Lawrence

    Lawrence Well-Known Member

    @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>
    Allan likes this.
  11. Chris D

    Chris D XenForo Developer Staff Member

    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.
    Daniel Hood, Amaury and Allan like this.
  12. Allan

    Allan Well-Known Member

    Any help is appreciated, thank you :)

    Good idea, it's done :)


    But, how to call the id then?
  13. Allan

    Allan Well-Known Member

  14. Chris D

    Chris D XenForo Developer Staff Member

    Just like this. Replace "yourOptionId" with whatever the option ID of your option is.
    <xen:if is="in_array({$thread.thread_id}, {$xenOptions.yourOptionId})">
    Allan likes this.
  15. Allan

    Allan Well-Known Member

    Confirmed, it work perfectly, thanks Chris ;)
  16. Allan

    Allan Well-Known Member

    @Chris D , i would like use several times this fonction:

    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 ?

    Look the gif: https://gyazo.com/6d7a49b7fa5eee346c55ce2751aa6044
  17. Allan

    Allan Well-Known Member

    I wonder if I need to create several templates with the same content but a different name for each option?
  18. Chris D

    Chris D XenForo Developer Staff Member

    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... ;)
  19. Allan

    Allan Well-Known Member

    Strange, it don't work.

    Example, i create 2 template:
    - option_template_tcsidebarthreadsAdmins
    - option_template_tcsidebarthreadsAdmins2

    Same content for both templates:
    <xen:controlunit label="{$preparedOption.title}">
                <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>
                <li class="fbAdmin"><xen:textbox name="{$fieldPrefix}[{$preparedOption.option_id}][]" value="" placeholder="Thread ID" /></li>
            <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:hint>Add Threads IDs where to show this block</xen:hint>
        <xen:explain>{xen:raw $preparedOption.explain}</xen:explain>
    My options:
    option1.png option2.png

    I made a mistake ?
  20. Chris D

    Chris D XenForo Developer Staff Member

    Look at the data-source attribute in the button.
    Allan likes this.

Share This Page