Jump to content

Select Options Fieldtype: Possible to output all available options in template?


Juergen
 Share

Recommended Posts

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

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

@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

$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

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

_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

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

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!

  • Like 7
Link to comment
Share on other sites

  • 2 years later...

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

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.

 

  • Like 3
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...