Jump to content

How do you update field type 'Options' using the API?


cb2004
 Share

Recommended Posts

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

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

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

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 by kixe
  • Like 4
Link to comment
Share on other sites

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");
  • Like 1
Link to comment
Share on other sites

@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;
  • Like 1
Link to comment
Share on other sites

@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

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

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

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-page
and here
http://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();
  • Like 1
Link to comment
Share on other sites

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
  • Like 3
Link to comment
Share on other sites

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

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|Archiv
post-1246-0-90095000-1433834081_thumb.jp

No idea ...

Link to comment
Share on other sites

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?

  • Like 2
Link to comment
Share on other sites

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

  • 1 month later...

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