Jump to content
Jo Justo

Trying to get a module's InputfieldSelectMultiple options

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!

Share this post


Link to post
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.

 

Share this post


Link to post
Share on other sites

Thanks for the quick response.

I tried to var_dump the getModuleInfo() but it just gives the module's overview info. I was trying to get deeper, at the Inputfields.

Share this post


Link to post
Share on other sites

sorry meant module config data

be sure to look at the api docs

also make sure to have tracy debygger installed

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
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...

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By kixe
      In a project I had to add the attribute 'disable' to some options of a select field (page reference) to show them but make them unselectable. Since I could not find an integrated solution, I wrote a tiny module and a hook. This could also be a POC to similar needs.
      Install module Add the Module to InputfieldPage in the module settings to make it selectable as Inputfield for page reference fields Create a hook in ready.php to manipulate attributes of the <option> tag for specified items
      Module
      <?php namespace ProcessWire; /** * POC Inputfield Select Hook Add Option -- Replacement for InputfieldSelect * * Selection of a single value from a select pulldown. Same like InputfieldSelect * except this version provides hookable addOption() function which allows to modify * attributes of the <option> tag (i. e. 'disabled' to show items but disallow to select them * * @author Christoph Thelen aka @kixe * */ class InputfieldSelectHookAddOption extends InputfieldSelect { /** * Return information about this module * */ public static function getModuleInfo() { return array( 'title' => __('Select Hookable Add Option', __FILE__), // Module Title 'summary' => __('Selection of a single value from a select pulldown. Same like InputfieldSelect. except this version provides hookable addOption() function which allows to modify attributes of the <option> tag (e.g. \'disabled\' to show items in dropdown but disallow to select', __FILE__), // Module Summary 'version' => 100, ); } /** * Hook in here to modify attributes */ public function ___addOptionAttributes($value, $label) { return array(); } /** * @see InputfieldSelect::addOption() * */ public function addOption($value, $label = null, array $attributes = null) { if (!is_array($attributes)) $attributes = array(); $attributes = array_merge($attributes, $this->addOptionAttributes($value, $label)); return parent::addOption($value, $label, $attributes); } }
      Hook
      /** * This example hook modifies the attributes of the selectable options of a Pagereference field named 'test'. * The selectable pages have the template 'test' assigned which includes a checkbox 'disallow'. * The attribute 'disabled' will be added to the selectable page if the user does not have the role 'user-extended' and 'disallow' is set. * */ $wire->addHookAfter('InputfieldSelectHookAddOption::addOptionAttributes', function($e) { // quick exit if ($e->object->name != 'test') return; if ($this->wire('user')->isSuperuser()|| $this->wire('user')->hasRole('user-extended')) return; // disable items (pages) in select $restrictedPageIDs = $this->wire('pages')->find('template=test,disallow=1')->each('id'); if (in_array($e->arguments[0], $restrictedPageIDs)) $e->return = array('disabled' => 'disabled'); });  
    • By louisstephens
      I have been following @bernhard's great tutorial on creating a Dashboard and everything has very easy to understand so far. However, I need to add a select field that a template is using (so users can create a new page from the dashboard), but I am really at a complete lost on how to achieve this. I have been googling, but can't really find anything on the matter.
      $field = $this->modules->get('InputfieldSelect'); $field->name = "Select Manufacturer"; $field->findPagesSelector = "template=basic-page"; //not needed for select field $field->labelFieldName = "title"; $field->name = "Manufacturer Select"; $field->columnWidth = 34; $fieldset->add($field); I have the above code, which was originally an InputfieldPage, but since this is an options field, I have changed it. Has anyone actually gotten a select from a template (and its' values) in a module?
    • By Valery
      Hello everybody,
      I am playing with the InputfieldSelectMultiple module, and I was wondering how to set the "selected" attribute for an Option?
      I have the following code:
      $field = $modules->get("InputfieldSelectMultiple"); $field->label = "Multiple select"; $field->attr('id+name', 'options'); $field->setAttribute('multiple', 'multiple'); $field->setAttribute('size', '8'); $field->addOption('121', '121-22', array('selected' => 'selected')); Which gives me this:
      <option selected='selected' selected="selected" value='121'>121-22</option> While it technically makes an option selected, this also produces redundant HTML code which does not validate. So, what's the right way to use addOption() so that the output is more like <option selected value=...> ?
      Thanks.
×
×
  • Create New...