cb2004 Posted June 1, 2015 Share Posted June 1, 2015 I have created a field using the relatively new field type 'Options'. I created my options which are Yes and No. These were then assigned: 1=Yes 2=No It doesn't appear to be well documented yet, but how would I update this field type using the api? I have tried numerous routes with no joy yet. I am going to keep trying and post here if I crack it. Link to comment Share on other sites More sharing options...
cstevensjr Posted June 1, 2015 Share Posted June 1, 2015 Can you please post any code that you have. Then someone can look it over and help you out. Link to comment Share on other sites More sharing options...
cb2004 Posted June 1, 2015 Author Share Posted June 1, 2015 Well I am adding a page like this: $page = new Page(); $page->addStatus(Page::statusHidden); $page->name = time(); $page->parent = $pages->get("1015"); $page->template = 'contact'; $page->title = $form[contact_name]; $page->form_date = time(); $page->form_text_1 = $form[business_name]; $page->form_textarea_1 = $form[address]; $page->form_text_2 = $form[tel]; $page->form_email = $form[email]; $page->form_url = $form[website]; $page->save(); Now I would also like to set the radio option selected from my form into PW, but I am not sure what format it is to set the options. https://processwire.com/api/modules/select-options-fieldtype/ Link to comment Share on other sites More sharing options...
cb2004 Posted June 1, 2015 Author Share Posted June 1, 2015 My field is called form_type and as said has the options 1=Yes, 2=No. Tried so many routes now but no joy. Link to comment Share on other sites More sharing options...
cb2004 Posted June 2, 2015 Author Share Posted June 2, 2015 Ok, I am having to store the results in PW as a text input for now as all the methods I can think of in the short time of using PW have not worked. Hopefully Ryan will expand on the documentation a little more in the future or pop in here at some point. Still cracking on with PW and loving it. Link to comment Share on other sites More sharing options...
kixe Posted June 2, 2015 Share Posted June 2, 2015 (edited) API to change field settings: $field = $fields->get(164); $value = "newoption\n1=oldoption1\n2=oldoption2"; $module = $modules->get('FieldtypeOptions'); $result = $module->manager->setOptionsString($field, $value, true); var_dump($result); /** * description of function setOptionsString() found in Class SelectableOptionManager.php * Set an options string * * Should adhere to the format * * One option per line in the format: 123=title or 123=value|title * where 123 is the option ID, 'value' is an optional value, * and 'title' is a required title. * * For new options, specify just the option title * (or value|title) on its own line. Options should * be in the desired sort order. * * @param Field $field * @param string $value * @param bool $allowDelete Allow removed lines in the string to result in deleted options? * If false, no options will be affected but you can call the getRemovedOptionIDs() method * to retrieve them for confirmation. * @return array containing ('added' => cnt, 'updated' => cnt, 'deleted' => cnt, 'marked' => cnt) * note: 'marked' means marked for deletion * */ Edited June 3, 2015 by kixe 4 Link to comment Share on other sites More sharing options...
cb2004 Posted June 3, 2015 Author Share Posted June 3, 2015 Thanks kixe, I will give it a try at some point and feedback. Link to comment Share on other sites More sharing options...
Soma Posted June 3, 2015 Share Posted June 3, 2015 kixie, he's not looking to modify the field options but to save an options selected in his form. It should be simply adding the key (id) to your field: $page->form_type = 1; or using the value (title) $page->form_type = "Yes"; or if it's a multiple options you set an array using id's: $page->form_type = array(1,2); or titles $page->form_type = array("Yes","No"); 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted June 3, 2015 Share Posted June 3, 2015 @Soma Directly setting the value doesn't work, even though I would like it to do so. $field = $fields->get("optionField"); $options = $field->type->getOptions($field); $page->optionField = $options->get("value=myValue")->id; 1 Link to comment Share on other sites More sharing options...
Soma Posted June 3, 2015 Share Posted June 3, 2015 For me it works. Link to comment Share on other sites More sharing options...
Soma Posted June 3, 2015 Share Posted June 3, 2015 @Soma Directly setting the value doesn't work, even though I would like it to do so. $field = $fields->get("optionField"); $options = $field->type->getOptions($field); $page->optionField = $options->get("value=myValue")->id; I'm not sure what that code should do at all Link to comment Share on other sites More sharing options...
LostKobrakai Posted June 3, 2015 Share Posted June 3, 2015 That's how you would set the a options field if you want to define it by a value – myValue in this example. Otherwise you have to use the id. Link to comment Share on other sites More sharing options...
Soma Posted June 3, 2015 Share Posted June 3, 2015 I'm not sure how that's relevant as he wants to know how to save the selected option, anyway... $options->get("value=myValue")->id; doesn't return a the key for me but $options->get("title=myValue")->id; does Edit: or $options->get("value|title=myValue")->id; so it's a bug? Link to comment Share on other sites More sharing options...
Soma Posted June 3, 2015 Share Posted June 3, 2015 Ah, it's hard/confusing to understand that documentation, even for me. As you can see from my previous post: value would be = title and key = id. So I get now that a custom "value" could be set using "id=value|title", I didn't understand that at first. So setting it that way, it still works fine for me. $anredeField = $fields->options_anrede; $opts = $anredeField->type->getOptions($anredeField); $id = $opts->get("value=m")->id; $page->options_anrede = $id; ... The options config: 1=f|Frau 2=m|Herr So again it's just assign the ID to the $page->youroptionsfield and save. Link to comment Share on other sites More sharing options...
kixe Posted June 4, 2015 Share Posted June 4, 2015 The Field Value of Type FieldtypeOptions is a WireArray. Easy to manipulate as Soma described above.Furthermore you can use all the methods supported by WireArray as described here:http://processwire.com/api/modules/select-options-fieldtype/#manipulating-options-on-a-pageand herehttp://processwire.com/api/arrays/ In case of updating existing page don't forget to set of(false) $page->of(false); $page->form_type = 'Yes'; $page->save(); 1 Link to comment Share on other sites More sharing options...
ryan Posted June 7, 2015 Share Posted June 7, 2015 When it comes to output, options fields are designed to mimic the behavior of Page fields. So $option->title is the primary field you would access from each option when doing output, i.e. echo $page->myfield->title; Or if a multi-value field: foreach($page->myfield as $item) { echo "<li>$item->title</li>"; } Other properties you can access are 'id' and 'value'. In most cases, you will just be accessing the 'title' though. The 'value' property is only used if you defined separate values for your options, i.e. "id=value|title". When it comes to manipulating values, it's really simple. Just make sure that output formatting is OFF ahead of time (like Kixe mentioned above). This is true for any ProcessWire field where you intend to manipulate values. If output formatting is ON, then it's simply not going to work because the Page is not in a state to have it's values manipulated. So for those of you above where it didn't work, chances are your $page output formatting was on. Turn it off like this: $page->of(false); Assign the selected option title you want to to field directly (as a string value). This is just like previous examples on this page: $page->myfield = 'Yes'; If you want multiple selected options, then assign an array of option titles, i.e. $page->myfield = array('Yes', 'Maybe'); You can also use a string to set multiple titles (separated by a pipe "|"): $page->myfield = "Yes|Maybe"; You can also set by the option's 'id' or 'value' properties, using the same syntax as above. Note that when assigning positive numbers, they are always assumed to be the 'id'. So if your option 'title' or 'value' (if used) needs to be numeric, you might want to specifically assign your 'id' properties to be that same number to reduce confusion, i.e. "1=1". If using separate values and titles, it will look for a matching title first. If/when you want to save the changes, then follow it up with: $page->save(); // this $page->save('myfield'); // or this, if you don't need to save anything else 3 Link to comment Share on other sites More sharing options...
LostKobrakai Posted June 8, 2015 Share Posted June 8, 2015 You can also set by the option's 'id' or 'value' properties, using the same syntax as above. If that's the case then there's a bug somewhere, because it's not working for me if I use values. $job->of(false); $job->bp_jobstatus = "archive"; $job->save("bp_jobstatus"); $job->of(true); var_dump($job->bp_jobstatus->value); $field = $fields->get("bp_jobstatus"); $options = $field->type->getOptions($field); var_dump($options->explode(array("id", "value", "title"))); Outputs this: null array (size=7) … 6 => array (size=3) 'id' => int 6 'value' => string 'archive' (length=7) 'title' => string 'Archiv' (length=6) … An this php error message: Warning: Illegal offset type in /Users/Benni/Projekte/A-03-2015_made_full/03_Programmierung/www/wire/modules/Fieldtype/FieldtypeOptions/SelectableOptionManager.php on line 132 Link to comment Share on other sites More sharing options...
kixe Posted June 9, 2015 Share Posted June 9, 2015 I couldn't reproduce this result (NULL) and Error. Normally you get NULL if the option doesn' exist. The Error looks strange, since 'value' has the expected type (string). How did you edit the options, via API or in the Fieldsettings? I assume your $job variable is an instance of $page?In the fieldsettings (options) you should find.6=archive|ArchivNo idea ... Link to comment Share on other sites More sharing options...
LostKobrakai Posted June 9, 2015 Share Posted June 9, 2015 I've set it up via the field settings and the line looks exactly like in your screenshot (though the yes/no example wasn't mine). And yeah $job is a page. It's directly from an project I'm working on. Link to comment Share on other sites More sharing options...
LostKobrakai Posted June 9, 2015 Share Posted June 9, 2015 I posted an PR which fixes the issue. Currently the core doesn't save the string value before checking by "title" and overwriting the string. The next check for possible "value" matches failed therefore. https://github.com/ryancramerdesign/ProcessWire/pull/1226 2 Link to comment Share on other sites More sharing options...
cb2004 Posted June 9, 2015 Author Share Posted June 9, 2015 Will look into this when I get a moment. Sure was acting like a bug to me when I tried a load of different ways so I will report back when i can. Link to comment Share on other sites More sharing options...
Soma Posted June 9, 2015 Share Posted June 9, 2015 For me it works fine even without turning outputformatting off. Link to comment Share on other sites More sharing options...
Soma Posted June 9, 2015 Share Posted June 9, 2015 I just experience something else. In backend when I have single value select, once I have selected an option I can't go back and select the first bank value to "remove", it just doesn't save it and it's still previous value selected. Why is there a blank value if I can't save it? 2 Link to comment Share on other sites More sharing options...
kixe Posted June 10, 2015 Share Posted June 10, 2015 Sometimes this happens sometimes not. Sometimes a second value (multiple) is stored after changing back to the blank value in the single select. The behaviour you described happend after setting options with api. Not if I cleaned everything, saved the field and added new options. Looks a bit buggy ...Everything seems to work correctly if value AND title is assigned. Link to comment Share on other sites More sharing options...
thetuningspoon Posted July 28, 2015 Share Posted July 28, 2015 I'm also getting the "Warning: Illegal offset type" line 132 error when I try to output the title after saving a blank. PW 2.6.3 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