However, in my generated yaml the file field is appearing like this:
filefield: >
filename.pdf
Which isn’t updating the structure (all the text fields etc. work as expected). When I update the structure through the panel, the field looks like this (which is what I guess I need my yaml to look like):
filefield:
- >
filename.pdf
My question is do I need to do something different with the array or the yaml, to get the code to match and the field to update? Thanks for any pointers
Sure,
This is an action triggered by a Uniform form submission. The submitted data then updates a structure on a page. This is the action overall (please see the addded Data:encode for $filename, this is what I have done following your advice above):
class TaskSubmissionAction extends Action
{
/**
* Action to process a task submission form, updating the task for the school
*/
public function perform() {
$taskautoid = $this->form->data('taskautoid');
$taskdetails = $this->form->data('taskdetails');
$information = $this->form->data('information');
$link = $this->form->data('link');
$filefield = $this->form->data('filefield');
$prefix = $this->form->data('prefix');
if(!empty($filefield['name'])){
$filename = $prefix . $filefield['name'];
}
$structure = kirby()->user()->school()->toPage()->$taskdetails()->yaml();
try {
foreach ($structure as $item){
// this is school's item
if ($item['questionautoid'] == $taskautoid){
$item['progress'] = 'pending';
$item['information'] = $information;
$item['link'] = $link;
if(!empty($filefield['name'])){
$item['filefield'] = Data::encode($filename, "yaml");
}
}
$updatedStructure[] = $item;
}
kirby()->user()->school()->toPage()->update([
$taskdetails => Data::encode($updatedStructure, "yaml"),
]);
} catch (\Exception $e) {
$this->fail($e->getMessage());
}
}
}
The other fields (link, information) are updating as expected. Structure field definition looks like this:
I am totally lost with this . This is what my array looks like:
[questionautoid] => h78df0bn
[nexttask] => false
[task] => Appoint a member of the SLT as leader of the project
[details] => Upload a letter from your Headteacher with the SLT designated lead on the project
[textbox] => false
[fileupload] => true
[linkbox] => false
[timetocomplete] => 1 hour
[textboxrequired] => false
[fileuploadrequired] => true
[linkboxrequired] => false
[information] =>
[link] =>
[filefield] => filename.pdf
[progress] => pending
And when I convert to yaml to use with $page->update() I get this:
questionautoid: h78df0bn
nexttask: 'false'
task: >
Appoint a member of the SLT as leader of
the project
details: >
Upload a letter from your Headteacher
with the SLT designated lead on the
project
textbox: 'false'
fileupload: 'true'
linkbox: 'false'
timetocomplete: 1 hour
textboxrequired: 'false'
fileuploadrequired: 'true'
linkboxrequired: 'false'
information: ""
link: ""
filefield: >
filename.pdf
progress: pending
All the fields apart from the filefield update successfully. When I save the structure through the panel I get the following, with the different treatment of the filefield:
questionautoid: h78df0bn
nexttask: 'false'
task: >
Appoint a member of the SLT as leader of
the project
details: >
Upload a letter from your Headteacher
with the SLT designated lead on the
project
textbox: 'false'
fileupload: 'true'
linkbox: 'false'
timetocomplete: 1 hour
textboxrequired: 'false'
fileuploadrequired: 'true'
linkboxrequired: 'false'
information: ""
link: ""
filefield:
- >
filename.pdf
progress: pending
If anyone has any thoughts it would be greatly appreciated, I am not sure what to try next
And I am handling updating the file in the same way as above. If anyone has a chance to take a look over the weekend that would be awesome, I am not sure where to go next
Thanks I really appreciate it! The files are uploaded to the “school” page the structure is on - that is handled by the standard Uniform uploadAction.
I am on Kirby 3.5.6, but can upgrade if that will help.
The broader picture is I am refactoring the site to manage this data on a school page, rather than a user account, hence having the working code for a user.
One thing that you should keep in mind is that since you are modifying your variable instead of adding to it, you should prepend an ampersand before your variable in the loop:
I have this run when I submit my form, so the trigger is the same. On submit I get the following saved:
Details:
-
info: Some text
filesfield: [ ]
I have also updated to the latest version of Kirby to see if that helped, but am getting the same result. I am not sure what to try with this next, and I am not getting any errors logged anywhere to go from
Ok that is interesting. If I add the file manually, then put the filename into the action, it works. Perhaps it is to do with how the Uniform actions work - I assumed they were sequential (I have the upload action before this one), but perhaps the file is not being fully written until after the action has run, hence the empty array. @mzur could that be the case?
No, actions are executed immediately when they are called in the code. Did you take the random filename prefix into account that can be added by the upload action? How does the actual code look that calls Uniform?