gregory Posted February 8, 2022 Posted February 8, 2022 Hi guys. How can I recall in a select the values of a FieldtypeOptions (tipologia in my example)? Thanks foreach($page->tipologia as $item) { $selected = $item->name == $input->item ? " selected='selected' " : ''; echo "<option$selected value='{$item->name}'>{$item->title}</option>"; }
Zeka Posted February 8, 2022 Posted February 8, 2022 Hi @gregory Something like this $field = $fields->get('tipologia'); $all_options = $field->type->getOptions($field); $selected_option = $all_options->get("title=" $input->item); // or id=some_id foreach($all_options as $item) { $selected = $item->id === $selected_option->id ? " selected='selected' " : ''; echo "<option$selected value='{$item->id}'>{$item->title}</option>"; }
gregory Posted February 9, 2022 Author Posted February 9, 2022 Hi @Zeka your suggestion works. $field = $fields->get('tipologia'); $all_options = $field->type->getOptions($field); $selected_option = $all_options->get("title="); // or ? foreach($all_options as $item) { $selected = $item->id === $selected_option->id ? " selected='selected' " : ''; echo "<option$selected value='{$item->id}'>{$item->title}</option>"; } Is there a way to visualize the translated values? Thanks
Zeka Posted February 9, 2022 Posted February 9, 2022 @gregory 1 hour ago, gregory said: Is there a way to visualize the translated values? Not sure that I got it. Do yo meen to output translated values? If so, AFAIC it should work without any aditions to the code and will output values for current user language, but if you want to output values in the specific langauge you can change user langauge before gettting options and then change it back to current langauge. $field = $fields->get('tipologia'); $currentLanguage = wire()->user->language; wire()->user->language = wire()->languages->get('another langauge name'); $all_options = $field->type->getOptions($field); wire()->user->language = $currentLanguage; $selected_option = $all_options->get("title="); // or ? foreach($all_options as $item) { $selected = $item->id === $selected_option->id ? " selected='selected' " : ''; echo "<option$selected value='{$item->id}'>{$item->title}</option>"; }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now