flangefrog Posted September 15, 2014 Report Posted September 15, 2014 The Module::arrayToModuleFields() function does not include selected_value or placeholder values from the array in the returned object.
Tyson Posted September 15, 2014 Report Posted September 15, 2014 I'm not sure what you mean. Can you provide an example? Module::arrayToModuleFields($arr, $fields, $vars) - first parameter, $arr, accepts an index, "attributes", representing an array of additional attributes. You can set additional attributes here, like 'placeholder'. e.g. $arr['field']['attributes']['placeholder'] = "My Placeholder". Whether a field option is selected is determined by the values set in $vars, where the keys in $vars match the keys in $arr.
flangefrog Posted September 16, 2014 Author Report Posted September 16, 2014 An example is the select field. If you look in ModuleFields::fieldSelect() it has a parameter especially for the selected_value. Therefore I assumed that I would be able to set this parameter in the array like this: 'country' => array( 'label' => Language::_("Webdrive.whois.Country", true), 'type' => "select", 'selected_value' => "NZ", 'options' => array( 'NZ' => "New Zealand" ) ), However with the current implementation of Module::arrayToModuleFields() the only way is to set the attribute_option for 'NZ'. What I am suggesting is to add $selected_value = isset($field['selected_value']) ? $field['selected_value'] : null; Under: $attributes = isset($field['attributes']) ? $field['attributes'] : array(); And then pass the selected_value to ModuleFields::fieldSelect() here: case "select": $field_label->attach($fields->fieldSelect($name, $options, isset($vars->{$name}) ? $vars->{$name} : null, $attributes)); break; All the code above is applicable to the placeholder attribute as well. Edit: Just checked and Module::arrayToModuleFields() does not support using option_attributes either, so currently there is no way to set the selected value of a select box in the first param $arr
Tyson Posted September 16, 2014 Report Posted September 16, 2014 Module::arrayToModuleFields is intended to be a general helper method for converting an array to module fields for all field types. It's not intended to support the use of every parameter that could be passed to every other module field call, such as option_attributes for select fields. You may also notice that it does not support attributes for labels, or to allow label tags to be preserved. Likewise, multi-select fields are not supported. If you needed to set those specific fields or attributes, you could create those as individual ModuleFields separately and pass them in the second parameter to Module::arrayToModuleFields. Otherwise, you could write a specific helper method for your module to aid in module field construction. Edit: Just checked and Module::arrayToModuleFields() does not support using option_attributes either, so currently there is no way to set the selected value of a select box in the first param $arr As I mentioned in my earlier post, the currently-selected value for a field should be set in the third parameter ($vars) passed into ModuleFields::arrayToModuleFields, and field attributes set via key "attributes" in $arr. e.g. // Fields $arr = array( 'country' => array( 'label' => Language::_("Webdrive.whois.Country", true), 'type' => "select", 'options' => array( 'NZ' => "New Zealand" ), 'attributes' => array( 'placeholder' => Language::_("Webdrive.whois.Country", true) ) ), ); // Selected values $vars = (object)array( 'country' => "NZ" ); $fields = $this->arrayToModuleFields($arr, null, $vars); Blesta Addons 1
flangefrog Posted September 17, 2014 Author Report Posted September 17, 2014 Ok, thanks for your reply. I'll use $vars or maybe override the function.
Tyson Posted September 17, 2014 Report Posted September 17, 2014 Sounds good. Going to close this thread as not a bug.
Recommended Posts