Jump to content

Add dynamic items in a select by FieldtypeOptions


gregory
 Share

Recommended Posts

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>";
}

 

Link to comment
Share on other sites

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>";
}

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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>";
}

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...