Jump to content

[Solved] Can't Get Select Field Option -> Method FieldtypeSelect::getOptions does not exist or is not callable in this context


prestoav
 Share

Recommended Posts

Hi everyone,

PW v 3.0.165

I have a select field called 'listing_type' with three options. I'm trying get all values for the select to show on a page.

First I have this in the template that grabs the field:

// Get Listing Types
$field = $fields->get('listing_type');
$fieldtype = $field->getFieldtype();
echo "Field title: " . $field->name . " Fieldtype: " . $fieldtype . "<br />";

This seems to work and I get "Field title: listing_type Fieldtype: FieldtypeSelect" on the page exactly as I'd expect. However, as soon as I add the getOptions line to get the field like this:

$field = $fields->get('listing_type');
$fieldtype = $field->getFieldtype();
echo "Field title: " . $field->name . " Fieldtype: " . $fieldtype . "<br />";
$listTypes = $field->type->getOptions($field);

I get this error:

Error: Exception: Method FieldtypeSelect::getOptions does not exist or is not callable in this context (in wire/core/Wire.php line 544)

Looking the exception report this seems to be the issue:

Wire->__call('getOptions', Array)

Anyone have any ideas why I'm getting this error?

Thanks in advance.
 

Link to comment
Share on other sites

OK, I think I have an idea why might this me an issue.

The field uses the older 'FieldTypeSelect' field type rather than 'FieldTypeSelectOptions'. Maybe the getOptions() method is not available for the 'FeildTypeSelect' type?

Is there a way to migrate from one to another would anyone know? In admin for this field it's not possible to change the type to Select Options but I can create a new field of type 'Select Options'.

Or does anyone know the method for the 'FieldTypeSelect' type to retrieve the options?

Thank you!

Link to comment
Share on other sites

OK, I think I found a way in, that is to read the DB more directly for the possible values. I noticed from the original thread about the FieldTypeSelect module @ryan mentioned it stored the options as non-relational values. So, with a bit or trial and error I managed to get the values out. Note that they are stored as a single text string with options separated by the "\n" that comes from the admin input field (I'm guessing). 

So to get an array of the values for the Select filed called 'listing_types' you can do this:

// First let's get an array of the possible values
$field = $fields->get('listing_type');
$lts = explode("\n", $field->data["select_options"]);

// Now lets show them
foreach ($lts as $lt) {
	echo $lt . "<br />";
}

Note also that, as Ryan points out, if the select values in the field have been changed at any time then there may be pages with this field set to values that no longer appear in the array above. You could, I supposed, look at all the set values for pages and build an array that way but this solution works for me.

I hope this is helpful.

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...