XF 2.2 Option via template

TheSkullKid

Active member
Is it possible to define / add a PHP Callback option in a template, so that I can use the forum selection ?
I only found on how to add checkbox, radiorow, textbox
Thanks for your answers
 
I think you can't do that directly as far as I remember. May be possible. But off the top of my head, you can create a helping method like xf used to do and register it on the listeners and can do the callback in that method.
 
Assuming you only want to select viewable forums (and not pages, categories, etc.):
Code:
<xf:select name="node_id">
    <xf:foreach loop="$xf.app.em.getRepository('XF:Node').getNodeOptionsData(true, ['Forum'], null, true)" value="$node">
        <xf:option value="{$node.value}" disabled="{$node.disabled}">{$node.label}</xf:option>
    </xf:foreach>
</xf:select>
 
Last edited:
Thanks a lot
@Kirby
Code:
<xf:select name="node_id">
    <xf:foreach loop="$xf.app.em.getRepository('XF:Node').getNodeOptionsData(true, ['Forum'], null, true)" value="$node">
        <xf:option value="$node.value" {{ $selection ? 'selected=selected' : 'disabled="{$node.disabled}'}}">{$node.label}</xf:option>
    </xf:foreach>
</xf:select>

Why do I get an error: "
Syntax error - Template name

Code:
<xf:select name="node_id">
    <xf:foreach loop="$xf.app.em.getRepository('XF:Node').getNodeOptionsData(true, ['Forum'], null, true)" value="$node">
        <xf:if is"$selection">
          <xf:option value="$node.value" selected="selected">{$node.label}</xf:option>
         <xf:else />
          <xf:option value="$node.value" disabled="{$node.disabled}">{$node.label}</xf:option>
         </xf:if>
    </xf:foreach>
</xf:select>

Expected valid expression. - Template name
 
Last edited:
I am not totally sure what you are trying to do, but assuming that $selection is the int value of the selected node id
Code:
<xf:select name="node_id" value="{$selection}">
    <xf:foreach loop="$xf.app.em.getRepository('XF:Node').getNodeOptionsData(true, ['Forum'], null, true)" value="$node">
        <xf:option value="{$node.value}" disabled="{$node.disabled}">{$node.label}</xf:option>
    </xf:foreach>
</xf:select>

Your approaches would also work:
Code:
<xf:select name="node_id">
    <xf:foreach loop="$xf.app.em.getRepository('XF:Node').getNodeOptionsData(true, ['Forum'], null, true)" value="$node">
        <xf:option value="{$node.value}" selected="{{ $node.value === $selection }}" disabled="{$node.disabled}">{$node.label}</xf:option>
    </xf:foreach>
</xf:select>

Code:
<xf:select name="node_id">
    <xf:foreach loop="$xf.app.em.getRepository('XF:Node').getNodeOptionsData(true, ['Forum'], null, true)" value="$node">
        <xf:if is="$node.value === $selection">
            <xf:option value="{$node.value}" disabled="{$node.disabled}" selected="{{ true }}">{$node.label}</xf:option>
        <xf:else />
            <xf:option value="{$node.value}" disabled="{$node.disabled}">{$node.label}</xf:option>
         </xf:if>
    </xf:foreach>
</xf:select>

But IMHO both are more complicated than necessary.
 
Last edited:
Why more complicated than necessary?
Well ... because it is more complicated / complex than necessary (in my mind).
  1. Your template code is longer which makes it more difficult to read / understand for a human and slightly more work (though probably not even measurable) to parse
  2. The generated PHP code gets longer / more complex (especially variant 2)
The purpose of <xf:select> param value is to pass the selected value(s) to "automagically" set the corresponding option selected, so why do you want to reinvent the wheel (by doing that manually)? :)
 
The purpose of <xf:select> param value is to pass the selected value(s) to "automagically" set the corresponding option selected, so why do you want to reinvent the wheel (by doing that manually)? :)
Good question, this was the first I've thought about it and tried but it was not working with the value.
Maybe small typo.

Thanks a again for you help this is much appreciated.
Richardo
 
Top Bottom