How to have an arraya as option

Robert9

Well-known member
Can I have an array in options to use it directly in a template?

Or is it needed to have a comma list 1,2,3 and explode it first?

Or can I have a comma list and explode it in a template?

In other words:

I want to save 1,2,3 in option and use this as an array in a template to do something like

Code:
<xf:if is="in_array($value, $xf.options.r9_tff_values)"
 
Last edited:
I'm not sure what you're trying to accomplish but, based on the example you gave, you could send it to the template via a viewParam...something like:

PHP:
$array = ['1', '2', '3'];

$viewParams = [
    'values' => $array
];
return $this->view('SomeAddon\Action', 'some_template_name', $viewParams);

Then, in your template use something like this:

HTML:
<xf:foreach loop="$values" value="$value">
    <xf:if is="in_array($value, $xf.options.r9_tff_values)">
        <div>$value</div>
    </xf:if>
</xf:foreach>
 
Thank you, but ...

I need this array as xf option to use it in a template.

What I have found is complicated.
I have to add a class where I define my values, then I send them to a template,
and the option gets the template to show me for example checkboxes to save my values as option in an array.

Maybe there is an easier way, but I have not found any examples.


New option for the add-on,
fetch values from a thread_field with checkboxes,
show them as multi select field in add-on options, save the values, use them in a template.
 
Thank you, but ...

I need this array as xf option to use it in a template.

What I have found is complicated.
I have to add a class where I define my values, then I send them to a template,
and the option gets the template to show me for example checkboxes to save my values as option in an array.

Maybe there is an easier way, but I have not found any examples.


New option for the add-on,
fetch values from a thread_field with checkboxes,
show them as multi select field in add-on options, save the values, use them in a template.

Oops, sorry about that. I must have misinterpreted what you were asking.
 
Not sure if this is what you want ... but smth. like this could work (if you don't need validation which is highly recommended!)

A thread field with a checkbox
r9_checkbox-1.PNG

and an option using a template
r9_threadfield_option.png

with this code
Code:
<xf:formrow
        label="{$option.title}"
        hint="{$hintHtml}"
        explain="{$explainHtml}"
        rowclass="{$rowClass}">
        <xf:hiddenval name="options_listed[]">{$option.option_id}</xf:hiddenval>
        <xf:set var="$checkboxField" value="{{ $xf.app.get('customFields.threads').get('r9_checkbox_field') }}" />
        <xf:checkboxrow name="options[{$option.option_id}][]" rowtype="fullWidth noPadding" value="{$option.option_value}">
                <xf:options source="$checkboxField.field_choices" />
        </xf:checkboxrow>
</xf:formrow>

would look like this
1699302220498.png

If you want a multiselect in the option instead, just modify the template code as desired.
 
Ähem ...

I want an option defined in options.xml
This option should save an array.

Anyway, I have solved it.

My option looks like that:

Code:
   <option option_id="r9_bff_fields" edit_format="callback" data_type="array" advanced="0">
    <default_value>[]</default_value>
    <edit_format_params>Robert9\BetterFieldFilter\Option\ThreadField::renderSelectMultiple</edit_format_params>
    <sub_options>*</sub_options>
    <relation group_id="r9_bff" display_order="20"/>
  </option>

In Robert\BetterFieldFilter\Option\ThreadField::renderSelectMultiple

i fetch values from one thread custom field, throw it in a template and let it show in the addon's option page.
Now i can save an array. Now I can change the values for my field and have them here. Now I have an array for a template.


But, if someone has an idea how to make this fast and dirty, you are welcome.

Finally i just want to save something like:

24,red
25,blue
26,green

I can save this in a text-field, but I want to use it as an array directly in a template!
Yes, I can call this in a class and explode it. But, i repeat it:

Is there a way to have an add-on option and use it as an array in a template, without any step between?
It should be something like:

Code:
   <option option_id="r9_abc_fields" edit_format="lined_commalist_to_array" data_type="array" advanced="0">
    <default_value>[]</default_value>
    <edit_format_params>24,red
25,blue
26,green</edit_format_params>
    <sub_options>*</sub_options>
    <relation group_id="r9_abc" display_order="20"/>
  </option>

And this should be saved as an array.
No class, no larifari, just save some values and use them in a template as an array.


Or it should be something like this:

option_field:24/blue,25/red,26/green

template_special_syntax: set $didel = explode two times by , and then by "/" to have an array!
 
I want an option defined in options.xml
This option should save an array.
That's what I've shown you :)
<edit_format_params>Robert9\BetterFieldFilter\Option\ThreadField::renderSelectMultiple</edit_format_params>
I don't know what Robert9\BetterFieldFilter\Option\ThreadField::renderSelectMultiple does (as you are not showing that), but assuming you just want to fetch the values of a choice thread field - I've shown you how to do this with just a template.
Finally i just want to save something like:

24,red
25,blue
26,green
That's a different development goal than you stated in your previous post:

New option for the add-on,
fetch values from a thread_field with checkboxes,
show them as multi select field in add-on options, save the values, use them in a template.

AFAIK there are no build-in classes to split / parse arbitrary strings from a (textbox / textarea) option into an array (and convert that array back to text for display / editing).
(Though I agree that this would be handy every now and then)
 
Top Bottom