Jump to content

Trying to get a module's InputfieldSelectMultiple options


Jo J
 Share

Recommended Posts

I am trying to get the "text Content" (not the value) portion of an InputfieldSelectMultiple property I created for an installed module.

The selected options are saved in PW somewhere. How do I get at it? viz:

<select name="fieldA">
  <option selected value=0>Content 1</option>
  <option value=1>Content 2</option>
  <option selected value=2>Content 3</option>
</select>

I know its an array and that i'd have to access the options in a loop. But I can't seem to make the right call to any of the methods that return an array.

So, here's an example of many that i tried and failed:

$module = wire('modules')->get('ImportMyModule');
$field = $module->get('fieldA');
$options = $field->getOptions();
foreach($options as $key => $val) {
   echo "key: ", $key, " val: ", $val; 
}

I can't figure out to get at just the "text Content" for now , then the selected one's after. (in the above sample "Content 1" and "Content 3")

It'll save me a bunch of if's. Thanks forum!

Link to comment
Share on other sites

If you are creating an input within a module, then you add the options via the API.

You get the data that is stored by the module using the getModuleInfo() method and then you would see what the module's settings are.

If you look at the Settings Factory module, you'll see how you can use many different inputfields, and how the module provides the stored data back as an array or object.

 

Link to comment
Share on other sites

It's hard to tell without knowing what type of module it is you installed. Is it a module you wrote? Either way, 'labels' of inputs (your  Content 1, 2 and 3) are not themselves stored. It is the values that are stored.

If your module is a Process Module, selected values will be stored in the module's config. You can retrieve them using getModuleConfigInputfields(). There is a newer method that does the same thing but I cannot remember its name at the moment. The methods will return an array, $data, whose indexes are your properties, e.g. $data['fieldA']. In the database, these are stored in the modules table, in the row for your module, in its data column as a JSON string.

If your module is a Fieldtype, the name of the input is a property of your $field. So, you access the value as $field->fieldA. 

As mentioned earlier, labels are build on the fly as per the options you pass the module.

Link to comment
Share on other sites

Good to know. Thanks.

I didn't think to look in the database, and yes, I do see the value in it now but without the labels as you explained.

My module implements a WireData. I just use it to store parameters and install some custom classes required for a nightly cron job.  I can access the values by $module->get('fieldA'), but I was hoping to get the label too to include in the cron job.

I'll probably change the input type to something else that saves both the label and the value. Thanks much all!

Link to comment
Share on other sites

9 hours ago, Jo Justo said:

I'll probably change the input type to something else that saves both the label and the value.

Hmm, maybe Fieldtype Options, although it stores the labels separately. Alternatively, you can have your value be a 'label|value' combo. Or save the labels to some file and read that using PHP. Just random ideas...

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

×
×
  • Create New...