Bob,
Thank-you so much for this. It's a huge help.
I'm having a couple of issues with the code. Here's the snippet, slightly revised due to some syntax errors in the original:
<?php
/*
DisplayRobustOptionsTV snippet - https://forums.modx.com/thread?thread=70082#dis-post-392829
Purpose - When creating a Template Variable that outputs "<option>" HTML tags (using the "Output Type" of "HTML" and the "Tag" set to "option"), you specify the input options as option1||option2||option3. This works, but the problem is that this only outputs a very simple <option> tag, with no value attribute.
This snippet enables you to add additional attributes like "value" to the <option> tag.
Snippet Call - [[!DisplayRobustOptionsTV? &tvName=`SomeTv` &fieldName=`bread` &size = `6`]]
*/
$tv = $modx->getObject('modTemplateVar', array('name' => $scriptProperties['tvName']));
if ($tv) { // make sure the TV exists
$output = '<select name="' . $scriptProperties['fieldName'] . '" size="' . $scriptProperties['size'] . '">' . "\n";
$options = explode('||', $tv->get('default_text'));
$selected = explode ('||', $tv->getValue($modx->resource->get('id')));
foreach($options as $option) {
$output .= '<option value= "' . $option . '"';
if (in_array($option, $selected)) {
$output .= ' SELECTED="SELECTED" ';
}
$output .= ' >' . $option . "\n";
}
$output .= '</select>';
return $output;
}
?>
This is outputting the following:
<select name="bread" size="6">
<option value="" selected="SELECTED">
</option></select>
For some reason the value/text are not being rendered. The TV I'm trying to render is set as a listbox multi-select with the following input option values:
"sourdough||baguette||mad river grain||bagel||whole wheat||light rye"
Here's the page it's appearing on -
http://future.thefutureforward.com/~moon/order-online.html
Also, not to throw a ton at you at once, but I'm actually trying to eventually arrive at something like this for output:
<select id="center" name="center">
<option value="grilled chicken breast">grilled chicken breast</option>
<option value="grilled salmon{p+2}">grilled salmon - add 2.00</option>
<option value="roast turkey{p+1}">roast turkey - add 1.00</option>
<option value="roast beef{p+2}">roast beef - add 2.00</option>
...
</select>
This is a Foxycart integration that mimics others that I've done in the past. The need for a solid way of producing configurable product options through the resource editor itself has always been a big challenge. The key elements for a Foxycart product option are simply the name of the option and then (generally) the price and weight difference if one exists. I'm hoping one day to be able to have a single TV that can handle editing these values by the user within the resource editor (rather than having to go into the template variable itself to edit). Also, having the ability to click a "+" to create another <option> within the resource editor would be amazing.
I've arrived at something that's getting closer to this by following along with Shaun's "Custom TV Output Type" tutorial -
http://rtfm.modx.com/display/revolution20/Adding+a+Custom+TV+Output+Type, But it's still a pretty far way off of what I want to achieve.
Not sure why I told you all of that, but just figured I would share!
Thanks again Bob.