Juergen Posted February 16, 2015 Share Posted February 16, 2015 Hello at all, I use the new select options fieldtype and I want to output all avaliable options on a frontend template. In my case I want to use it as a list of all kinds of events (internal event, external event,....). With the API calls provided in the documentation I am only be able to output the selected option (fe internal event) but not the whole list. Is there a way to ouput all options fe in a foreach loop? Best regards Jürgen Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 16, 2015 Share Posted February 16, 2015 As it's stated in the blogpost you linked: take a look here https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/modules/Fieldtype/FieldtypeOptions/FieldtypeOptions.module#L438 Link to comment Share on other sites More sharing options...
Juergen Posted February 16, 2015 Author Share Posted February 16, 2015 I found the sequence but I dont understand how to output it at the frontend! /** * Get all options available for the given field * * @param id|string|Field $field Field name, id or object * @return SelectableOptionArray * @throws WireException * */ public function getOptions($field) { return $this->manager->getOptions($field = $this->_getField($field)); } Maybe in a way like this?? $options= $fields->get("fieldname")->????; Link to comment Share on other sites More sharing options...
diogo Posted February 16, 2015 Share Posted February 16, 2015 Using the example on that page: $page->getOptions('countries') Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 16, 2015 Share Posted February 16, 2015 @diogo This doesn't work. I've got it to at least error on the function which gets the options from the database with this. $fields->get("countries")->type->getOptions($page->fields->countries) But there seems to be something wrong in getOptions as I get this error, which is strange as the Argument is supposed to be coming from a function which does not return anything. Argument 1 passed to SelectableOptionManager::getOptions() must be an instance of Field Link to comment Share on other sites More sharing options...
Soma Posted February 16, 2015 Share Posted February 16, 2015 It need a field as object not as string. Link to comment Share on other sites More sharing options...
diogo Posted February 16, 2015 Share Posted February 16, 2015 Soma: * @param id|string|Field $field Field name, id or object Link to comment Share on other sites More sharing options...
Soma Posted February 16, 2015 Share Posted February 16, 2015 Ok I was just quoting the error Link to comment Share on other sites More sharing options...
diogo Posted February 16, 2015 Share Posted February 16, 2015 Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 16, 2015 Share Posted February 16, 2015 $page->fields->countries instanceof Field // true That shouldn't be the problem, but there's this in the module: protected function _getField($field) { if(!$field instanceof Field) { $_field = $field; $field = $this->wire('fields')->get($field); if(!$field) throw new WireException("Unknown field: $_field"); } if(!$field->type instanceof FieldtypeOptions) { throw new WireException("Field $field->name is not of type FieldtypeOptions"); } } public function getOptions($field) { return $this->manager->getOptions($field = $this->_getField($field)); } _getField() should error if it's not a field, but the error comes from SelectableOptionManager::getOptions() which is after the _getField() call. So the input is right, but _getField() just doesn't return anything to be used later, or do I miss something? Link to comment Share on other sites More sharing options...
Juergen Posted February 17, 2015 Author Share Posted February 17, 2015 Maybe this could be a solution posted by Ryan: https://processwire.com/talk/topic/9018-new-options-fieldtype/ I havent tested it, because I am not at home. If it works I write a feedback here. Best regards Jürgen Link to comment Share on other sites More sharing options...
Juergen Posted February 17, 2015 Author Share Posted February 17, 2015 Damn, Ryans solution doesnt work! $options = $fieldtypes->get('FieldtypeOptions')->getOptions('YOURFIELDNAME'); foreach($options as $option) { echo $option->title; } OR $field = $fields->get('YOURFIELDNAME'); $options = $field->type->getOptions($field); It produces also the error Link to comment Share on other sites More sharing options...
BitPoet Posted February 17, 2015 Share Posted February 17, 2015 _getField() should error if it's not a field, but the error comes from SelectableOptionManager::getOptions() which is after the _getField() call. So the input is right, but _getField() just doesn't return anything to be used later, or do I miss something? _getField definitely needs to return $field. Tested by manually adding "return $field;" in line 427. Pull request sent through github. https://github.com/ryancramerdesign/ProcessWire/pull/931 1 Link to comment Share on other sites More sharing options...
Juergen Posted March 17, 2015 Author Share Posted March 17, 2015 Issue was fixed by Ryan. $options = $fieldtypes->get('FieldtypeOptions')->getOptions('YOURFIELDNAME'); foreach($options as $option) { echo $option->id; echo $option->value; echo $option->title; } This output all your options (id, value or title). This piece of code doesnt work multilingual. It only outputs the default language. If you have a multilingual site you have to output value and title in the specific language. //get value and title in different languages; if ($user->language->name != 'default') { $title = "title{$user->language}"; $value = "value{$user->language}"; } else { $title = 'title'; $value = 'value'; } $options = $fieldtypes->get('FieldtypeOptions')->getOptions('YOURFIELDNAME'); foreach($options as $option) { echo $option->id; echo $option->$value; echo $option->$title; }; Hope this is useful for others! 7 Link to comment Share on other sites More sharing options...
Missariella Posted September 23, 2017 Share Posted September 23, 2017 I am trying to get the multilingual drop-down output to work. However i am failing. I am using the newest process wire version 3.062 with multilingual profile installed. I am playing with the code and rewrote the code also. but i get the same output. Only the English drop down list and behind the language code of the selected language. ---- This is the code i am using now. $lan = $user->language; $options = $fieldtypes->get('FieldtypeOptions')->getOptions('YOURFIELDNAME'); foreach($options as $option) { echo $option->title{$lan} }; I am searching for multilingual fields. https://processwire.com/api/multi-language-support/multi-language-fields/ And came accros this. $page->of(false); getLanguageValue Do I have to use this as well? I am a bit confused how to use that. Please advice Thank you Link to comment Share on other sites More sharing options...
Juergen Posted September 23, 2017 Author Share Posted September 23, 2017 Hello @Missariella, however, I am using the latest dev version (3.0.76) and its working. First of all I recommend you to create a global translation piece of code. Put it somewhere in a file that is loaded everytime (fe. a function.php), so the translation code will be availiable everywhere. You can put it also in the template file where you want to output the select options, but loading it in general makes more sense in this case, because you can use it in other templates too. Here is the code: //get labels, title, values and descriptions in different languages if ($user->language->name != 'default') { $title = "title{$user->language}"; $value = "value{$user->language}"; $label = "label{$user->language}"; $description = "description{$user->language}"; $notes = "notes{$user->language}"; } else { $title = 'title'; $value = 'value'; $label = 'label'; $description = 'description'; $notes = 'notes'; } This little code snippet makes titles, values, labels, descriptions and notes available in all languages by only calling "$title" instead of "title" (or "$label" instead of "label" and so on...). So it makes your life much easier Afterwards creating a multilanguage select can be done like this: $options = $fieldtypes->get('FieldtypeOptions')->getOptions($fields->get('YOURFIELDNAME')); foreach ($options as $value) { echo $value->$title; echo $value->$label; echo $value->$description; } This outputs title, label and description in the user language. This is the code that I am using on my projects without any problems. 3 Link to comment Share on other sites More sharing options...
Missariella Posted September 23, 2017 Share Posted September 23, 2017 Thank you for the quick reply. Okay, i solved the problem. I didn't put the brackets around $option->$title. Now i did and it's working fine Link to comment Share on other sites More sharing options...
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