pppws Posted September 22, 2017 Share Posted September 22, 2017 hey there, i have a selectfield with 9 options. each options represents a status. when a user hits a button on the frontend i want to add +1 to the current status. e.g. the current status is 4. the user hits the save button, i want to resave the page with the new status of 5. i know how to save the page. the problem is to update the status itself. i can't figure out how to convert the select option (in my case. $status = $page->progress_status; ) into an integer value to add +1. Link to comment Share on other sites More sharing options...
abdus Posted September 22, 2017 Share Posted September 22, 2017 If you mean what I think you mean, then you can hook before InputfieldSelect::renderValue and change option labels depending on a field value. Haven't tried this, but it's a good start. $this->addHookBefore('InputfieldSelect::renderValue', function (HookEvent $e) { /** @var $select InputfieldSelect */ /** @var $page Page */ $select = $e->object; $page = $select->hasPage(); $options = $select->getOptions(); $options['myOption'] = $options['myOption'] . " -- " . $page->myStatusField; $select->set('options', $options); }); 1 Link to comment Share on other sites More sharing options...
pppws Posted September 22, 2017 Author Share Posted September 22, 2017 do hooks work for page editing on the frontend via the api? i want to change the status (value of the selectfield) when submitting a form. Link to comment Share on other sites More sharing options...
abdus Posted September 22, 2017 Share Posted September 22, 2017 Where's the form? Backend? Frontend? The hook I've written works (not tested) on the backend. Achieving it on the frontend is a bit more convoluted. If you go a bit more into detail, I can help you more, though. Link to comment Share on other sites More sharing options...
pppws Posted September 22, 2017 Author Share Posted September 22, 2017 okay, i try to be more precise the form is on the frontend. the user can fill out several fields and save them to the page he's editing (still on the frontend). each time the user hits the save/submit button i want to change add 1 to the status. the states are something like this: 1 – created 2 – saved once 3 – saved twice 4 – saved thrid time etc. as mentioned above i already know how to fill the field via inputs fields. but right know i'm struggling to add 1 to the status. my status field is called: progress_status. when i'v alread tried the things below, but the fail due to array errors or some other errors i can't figure out: $status = $page->progress_status; $newstatus = $status++; foreach ($status as $stat) { $newstatus = $stat+1; } foreach ($status as $stat) { $newstatus = $status[$stat]++; } Link to comment Share on other sites More sharing options...
abdus Posted September 22, 2017 Share Posted September 22, 2017 Hmm. To save/increment the status you can use: $page->of(false); $page->progress_status = $page->progress_status + 1; $page->save(); Link to comment Share on other sites More sharing options...
pppws Posted September 22, 2017 Author Share Posted September 22, 2017 hm. nah. it's not working :/. when i try: $newstatus = $page->progress_status + 1; this error is returned Quote Notice: Object of class ProcessWire\SelectableOptionArray could not be converted to int it works with: $page->progress_status->id Link to comment Share on other sites More sharing options...
abdus Posted September 22, 2017 Share Posted September 22, 2017 Ah, I think you're looking for this: https://processwire.com/api/modules/select-options-fieldtype/#manipulating-options-on-a-page-from-the-api But it wont be as simple as $page->progress_status += 1, because FieldtypeSelect doesn't work like that. Link to comment Share on other sites More sharing options...
Robin S Posted September 23, 2017 Share Posted September 23, 2017 2 hours ago, pppws said: the user can fill out several fields and save them to the page he's editing (still on the frontend). each time the user hits the save/submit button i want to change add 1 to the status. the states are something like this: 1 – created 2 – saved once 3 – saved twice 4 – saved thrid time Not sure why you are using an Options field for this. I think you want an integer field named "number_of_saves" or whatever. Then you increment the number with each save originating from the front-end. 1 Link to comment Share on other sites More sharing options...
pppws Posted September 23, 2017 Author Share Posted September 23, 2017 5 hours ago, Robin S said: Not sure why you are using an Options field for this. I think you want an integer field named "number_of_saves" or whatever. Then you increment the number with each save originating from the front-end. because the admin should be able to change the status in the backendend too – and it's more comfortable to handle this with a select field. 8 hours ago, abdus said: Ah, I think you're looking for this: https://processwire.com/api/modules/select-options-fieldtype/#manipulating-options-on-a-page-from-the-api But it wont be as simple as $page->progress_status += 1, because FieldtypeSelect doesn't work like that. do you think my $page->progress_status = $page->progress_status->id +1; solution will cause any trouble? Link to comment Share on other sites More sharing options...
abdus Posted September 23, 2017 Share Posted September 23, 2017 1 hour ago, pppws said: do you think my $page->progress_status = $page->progress_status->id +1; solution will cause any trouble? What happens when you add new options to that field? Or remove one? Or decide to create another select field before you change the old one? In the database, all select options are stored under a single table, this means ids may get intertwined with multiple option fields, they are not necessarily ordered neatly as "options of select1", "options of select 2" etc. Also, old ids aren't reused, instead new ids are assigned. All these condition are an invitation to undefined behavour. Ids aren't meant to be made arithmetic upon, they are what they are, identifiers and nothing more. If you need a field that can be incremented, use an integer field instead, you'll save much headache later. If you want to sanitize & limit the range of values, use field settings or hook into Page::saveReady and perform your sanitization there. Link to comment Share on other sites More sharing options...
BitPoet Posted September 23, 2017 Share Posted September 23, 2017 For completeness' sake, even though I agree that using a field that is limited in range may not be the best idea, here's how it should work without abusing ids: // Retrieve possible options through the field's typ $field = $fields->get("progress_status"); $progress_options = $field->type->getOptions($field); // Find the correct SelectableOption object by its desired value and assign it $page->progress_status = $progress_options->get("value=" . ($page->progress_status->value + 1)); Each SelectableOption object is made up of a (random and unique) id, a value (that's what you search for and assign) and a label (what the user sees in the select/radio/checkbox input). 1 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